mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: vfmt most of os
, add it to v test-cleancode
This commit is contained in:
@ -30,15 +30,11 @@ fn test_open_file() {
|
||||
assert err == 'No such file or directory'
|
||||
os.File{}
|
||||
}
|
||||
mut file := os.open_file(filename, 'w+', 0o666) or {
|
||||
panic(err)
|
||||
}
|
||||
mut file := os.open_file(filename, 'w+', 0o666) or { panic(err) }
|
||||
file.write_str(hello)
|
||||
file.close()
|
||||
assert hello.len == os.file_size(filename)
|
||||
read_hello := os.read_file(filename) or {
|
||||
panic('error reading file $filename')
|
||||
}
|
||||
read_hello := os.read_file(filename) or { panic('error reading file $filename') }
|
||||
assert hello == read_hello
|
||||
os.rm(filename)
|
||||
}
|
||||
@ -50,16 +46,12 @@ fn test_open_file_binary() {
|
||||
assert err == 'No such file or directory'
|
||||
os.File{}
|
||||
}
|
||||
mut file := os.open_file(filename, 'wb+', 0o666) or {
|
||||
panic(err)
|
||||
}
|
||||
mut file := os.open_file(filename, 'wb+', 0o666) or { panic(err) }
|
||||
bytes := hello.bytes()
|
||||
file.write_bytes(bytes.data, bytes.len)
|
||||
file.close()
|
||||
assert hello.len == os.file_size(filename)
|
||||
read_hello := os.read_bytes(filename) or {
|
||||
panic('error reading file $filename')
|
||||
}
|
||||
read_hello := os.read_bytes(filename) or { panic('error reading file $filename') }
|
||||
assert bytes == read_hello
|
||||
os.rm(filename)
|
||||
}
|
||||
@ -84,13 +76,10 @@ fn test_open_file_binary() {
|
||||
// assert line1 == 'line 1\n'
|
||||
// assert line2 == 'line 2'
|
||||
// }
|
||||
|
||||
fn test_create_file() {
|
||||
filename := './test1.txt'
|
||||
hello := 'hello world!'
|
||||
mut f := os.create(filename) or {
|
||||
panic(err)
|
||||
}
|
||||
mut f := os.create(filename) or { panic(err) }
|
||||
f.write_str(hello)
|
||||
f.close()
|
||||
assert hello.len == os.file_size(filename)
|
||||
@ -132,9 +121,7 @@ fn test_write_and_read_string_to_file() {
|
||||
hello := 'hello world!'
|
||||
os.write_file(filename, hello)
|
||||
assert hello.len == os.file_size(filename)
|
||||
read_hello := os.read_file(filename) or {
|
||||
panic('error reading file $filename')
|
||||
}
|
||||
read_hello := os.read_file(filename) or { panic('error reading file $filename') }
|
||||
assert hello == read_hello
|
||||
os.rm(filename)
|
||||
}
|
||||
@ -177,13 +164,9 @@ fn test_write_and_read_bytes() {
|
||||
|
||||
fn test_create_and_delete_folder() {
|
||||
folder := './test1'
|
||||
os.mkdir(folder) or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir(folder) or { panic(err) }
|
||||
assert os.is_dir(folder)
|
||||
folder_contents := os.ls(folder) or {
|
||||
panic(err)
|
||||
}
|
||||
folder_contents := os.ls(folder) or { panic(err) }
|
||||
assert folder_contents.len == 0
|
||||
os.rmdir(folder)
|
||||
folder_exists := os.is_dir(folder)
|
||||
@ -199,9 +182,7 @@ fn walk_callback(file string) {
|
||||
|
||||
fn test_walk() {
|
||||
folder := 'test_walk'
|
||||
os.mkdir(folder) or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir(folder) or { panic(err) }
|
||||
file1 := folder + os.path_separator + 'test1'
|
||||
os.write_file(file1, 'test-1')
|
||||
os.walk(folder, walk_callback)
|
||||
@ -213,15 +194,9 @@ fn test_cp() {
|
||||
old_file_name := 'cp_example.txt'
|
||||
new_file_name := 'cp_new_example.txt'
|
||||
os.write_file(old_file_name, 'Test data 1 2 3, V is awesome #$%^[]!~⭐')
|
||||
os.cp(old_file_name, new_file_name) or {
|
||||
panic('$err: errcode: $errcode')
|
||||
}
|
||||
old_file := os.read_file(old_file_name) or {
|
||||
panic(err)
|
||||
}
|
||||
new_file := os.read_file(new_file_name) or {
|
||||
panic(err)
|
||||
}
|
||||
os.cp(old_file_name, new_file_name) or { panic('$err: errcode: $errcode') }
|
||||
old_file := os.read_file(old_file_name) or { panic(err) }
|
||||
new_file := os.read_file(new_file_name) or { panic(err) }
|
||||
assert old_file == new_file
|
||||
os.rm(old_file_name)
|
||||
os.rm(new_file_name)
|
||||
@ -272,37 +247,19 @@ fn test_cp_r() {
|
||||
// fileX -> dir/fileX
|
||||
// NB: clean up of the files happens inside the cleanup_leftovers function
|
||||
os.write_file('ex1.txt', 'wow!')
|
||||
os.mkdir('ex') or {
|
||||
panic(err)
|
||||
}
|
||||
os.cp_all('ex1.txt', 'ex', false) or {
|
||||
panic(err)
|
||||
}
|
||||
old := os.read_file('ex1.txt') or {
|
||||
panic(err)
|
||||
}
|
||||
new := os.read_file('ex/ex1.txt') or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir('ex') or { panic(err) }
|
||||
os.cp_all('ex1.txt', 'ex', false) or { panic(err) }
|
||||
old := os.read_file('ex1.txt') or { panic(err) }
|
||||
new := os.read_file('ex/ex1.txt') or { panic(err) }
|
||||
assert old == new
|
||||
os.mkdir('ex/ex2') or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir('ex/ex2') or { panic(err) }
|
||||
os.write_file('ex2.txt', 'great!')
|
||||
os.cp_all('ex2.txt', 'ex/ex2', false) or {
|
||||
panic(err)
|
||||
}
|
||||
old2 := os.read_file('ex2.txt') or {
|
||||
panic(err)
|
||||
}
|
||||
new2 := os.read_file('ex/ex2/ex2.txt') or {
|
||||
panic(err)
|
||||
}
|
||||
os.cp_all('ex2.txt', 'ex/ex2', false) or { panic(err) }
|
||||
old2 := os.read_file('ex2.txt') or { panic(err) }
|
||||
new2 := os.read_file('ex/ex2/ex2.txt') or { panic(err) }
|
||||
assert old2 == new2
|
||||
// recurring on dir -> local dir
|
||||
os.cp_all('ex', './', true) or {
|
||||
panic(err)
|
||||
}
|
||||
os.cp_all('ex', './', true) or { panic(err) }
|
||||
}
|
||||
|
||||
fn test_tmpdir() {
|
||||
@ -313,9 +270,7 @@ fn test_tmpdir() {
|
||||
os.rm(tfile) // just in case
|
||||
tfile_content := 'this is a temporary file'
|
||||
os.write_file(tfile, tfile_content)
|
||||
tfile_content_read := os.read_file(tfile) or {
|
||||
panic(err)
|
||||
}
|
||||
tfile_content_read := os.read_file(tfile) or { panic(err) }
|
||||
assert tfile_content_read == tfile_content
|
||||
os.rm(tfile)
|
||||
}
|
||||
@ -339,12 +294,8 @@ fn test_make_symlink_check_is_link_and_remove_symlink() {
|
||||
symlink := 'tsymlink'
|
||||
os.rm(symlink)
|
||||
os.rm(folder)
|
||||
os.mkdir(folder) or {
|
||||
panic(err)
|
||||
}
|
||||
folder_contents := os.ls(folder) or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir(folder) or { panic(err) }
|
||||
folder_contents := os.ls(folder) or { panic(err) }
|
||||
assert folder_contents.len == 0
|
||||
os.system('ln -s $folder $symlink')
|
||||
assert os.is_link(symlink) == true
|
||||
@ -381,12 +332,8 @@ fn test_symlink() {
|
||||
$if windows {
|
||||
return
|
||||
}
|
||||
os.mkdir('symlink') or {
|
||||
panic(err)
|
||||
}
|
||||
os.symlink('symlink', 'symlink2') or {
|
||||
panic(err)
|
||||
}
|
||||
os.mkdir('symlink') or { panic(err) }
|
||||
os.symlink('symlink', 'symlink2') or { panic(err) }
|
||||
assert os.exists('symlink2')
|
||||
// cleanup
|
||||
os.rm('symlink')
|
||||
@ -485,9 +432,7 @@ fn test_write_file_array_bytes() {
|
||||
arr[i] = 65 + byte(i)
|
||||
}
|
||||
os.write_file_array(fpath, arr)
|
||||
rarr := os.read_bytes(fpath) or {
|
||||
panic(err)
|
||||
}
|
||||
rarr := os.read_bytes(fpath) or { panic(err) }
|
||||
assert arr == rarr
|
||||
// eprintln(arr.str())
|
||||
// eprintln(rarr.str())
|
||||
@ -508,7 +453,7 @@ fn test_write_file_array_structs() {
|
||||
|
||||
fn test_stdout_capture() {
|
||||
/*
|
||||
mut cmd := os.Command{
|
||||
mut cmd := os.Command{
|
||||
path:'cat'
|
||||
redirect_stdout: true
|
||||
}
|
||||
@ -518,5 +463,5 @@ for !cmd.eof {
|
||||
println('line="$line"')
|
||||
}
|
||||
cmd.close()
|
||||
*/
|
||||
*/
|
||||
}
|
||||
|
Reference in New Issue
Block a user