os: vfmt os_test.v

pull/4110/head
Alexander Medvednikov 2020-03-24 16:18:37 +01:00
parent e012966291
commit 32c8eb6490
3 changed files with 186 additions and 180 deletions

View File

@ -278,7 +278,7 @@ const (
) )
struct Foooj { struct Foooj {
a [N]int a [5]int // N
} }
fn test_fixed() { fn test_fixed() {

View File

@ -9,50 +9,46 @@ fn testsuite_end() {
} }
fn test_open_file() { fn test_open_file() {
filename := './test1.txt' filename := './test1.txt'
hello := 'hello world!' hello := 'hello world!'
os.open_file(filename, "r+", 0o666) or { os.open_file(filename, 'r+',0666) or {
assert err == "No such file or directory" assert err == 'No such file or directory'
} }
mut file := os.open_file(filename, 'w+',0666) or {
mut file := os.open_file(filename, "w+", 0o666) or { panic(err) } panic(err)
file.write(hello) }
file.close() file.write(hello)
file.close()
assert hello.len == os.file_size(filename) assert hello.len == os.file_size(filename)
read_hello := os.read_file(filename) or {
read_hello := os.read_file(filename) or { panic('error reading file $filename')
panic('error reading file $filename') }
} assert hello == read_hello
assert hello == read_hello os.rm(filename)
os.rm(filename)
} }
fn test_create_file() { fn test_create_file() {
filename := './test1.txt' filename := './test1.txt'
hello := 'hello world!' hello := 'hello world!'
mut f := os.create(filename) or { panic(err)} mut f := os.create(filename) or {
panic(err)
}
f.write(hello) f.write(hello)
f.close() f.close()
assert hello.len == os.file_size(filename) assert hello.len == os.file_size(filename)
os.rm(filename) os.rm(filename)
} }
fn test_write_and_read_string_to_file() { fn test_write_and_read_string_to_file() {
filename := './test1.txt' filename := './test1.txt'
hello := 'hello world!' hello := 'hello world!'
os.write_file(filename, hello) os.write_file(filename, hello)
assert hello.len == os.file_size(filename) assert hello.len == os.file_size(filename)
read_hello := os.read_file(filename) or {
read_hello := os.read_file(filename) or { panic('error reading file $filename')
panic('error reading file $filename') }
} assert hello == read_hello
assert hello == read_hello os.rm(filename)
os.rm(filename)
} }
// test_write_and_read_bytes checks for regressions made in the functions // test_write_and_read_bytes checks for regressions made in the functions
@ -93,183 +89,196 @@ fn test_write_and_read_bytes() {
} }
*/ */
fn test_create_and_delete_folder() { fn test_create_and_delete_folder() {
folder := './test1' folder := './test1'
os.mkdir(folder) or { panic(err) } os.mkdir(folder) or {
panic(err)
}
assert os.is_dir(folder) assert os.is_dir(folder)
folder_contents := os.ls(folder) or {
folder_contents := os.ls(folder) or { panic(err) } panic(err)
assert folder_contents.len == 0 }
assert folder_contents.len == 0
os.rmdir(folder) os.rmdir(folder)
folder_exists := os.is_dir(folder)
folder_exists := os.is_dir(folder) assert folder_exists == false
assert folder_exists == false
} }
fn walk_callback(file string) { fn walk_callback(file string) {
if file == '.' || file == '..' { if file == '.' || file == '..' {
return return
} }
assert file == 'test_walk' + os.path_separator + 'test1' assert file == 'test_walk' + os.path_separator + 'test1'
} }
fn test_walk() { fn test_walk() {
folder := 'test_walk' folder := 'test_walk'
os.mkdir(folder) or { panic(err) } os.mkdir(folder) or {
panic(err)
file1 := folder + os.path_separator + 'test1' }
file1 := folder + os.path_separator + 'test1'
os.write_file(file1,'test-1') os.write_file(file1, 'test-1')
os.walk(folder, walk_callback)
os.walk(folder, walk_callback)
os.rm(file1) os.rm(file1)
os.rmdir(folder) os.rmdir(folder)
} }
fn test_cp() { fn test_cp() {
old_file_name := 'cp_example.txt' old_file_name := 'cp_example.txt'
new_file_name := 'cp_new_example.txt' new_file_name := 'cp_new_example.txt'
os.write_file(old_file_name, 'Test data 1 2 3, V is awesome #$%^[]!~')
os.write_file(old_file_name, 'Test data 1 2 3, V is awesome #$%^[]!~') os.cp(old_file_name, new_file_name) or {
os.cp(old_file_name, new_file_name) or { panic('$err: errcode: $errcode') } panic('$err: errcode: $errcode')
}
old_file := os.read_file(old_file_name) or { panic(err) } old_file := os.read_file(old_file_name) or {
new_file := os.read_file(new_file_name) or { panic(err) } panic(err)
assert old_file == new_file }
new_file := os.read_file(new_file_name) or {
os.rm(old_file_name) panic(err)
os.rm(new_file_name) }
assert old_file == new_file
os.rm(old_file_name)
os.rm(new_file_name)
} }
fn test_cp_r() { fn test_cp_r() {
//fileX -> dir/fileX // fileX -> dir/fileX
// NB: clean up of the files happens inside the cleanup_leftovers function // NB: clean up of the files happens inside the cleanup_leftovers function
os.write_file('ex1.txt', 'wow!') os.write_file('ex1.txt', 'wow!')
os.mkdir('ex') or { panic(err) } os.mkdir('ex') or {
os.cp_all('ex1.txt', 'ex', false) or { panic(err) } panic(err)
old := os.read_file('ex1.txt') or { panic(err) } }
new := os.read_file('ex/ex1.txt') or { panic(err) } os.cp_all('ex1.txt', 'ex', false) or {
assert old == new panic(err)
os.mkdir('ex/ex2') or { panic(err) } }
os.write_file('ex2.txt', 'great!') old := os.read_file('ex1.txt') or {
os.cp_all('ex2.txt', 'ex/ex2', false) or { panic(err) } panic(err)
old2 := os.read_file('ex2.txt') or { panic(err) } }
new2 := os.read_file('ex/ex2/ex2.txt') or { panic(err) } new := os.read_file('ex/ex1.txt') or {
assert old2 == new2 panic(err)
//recurring on dir -> local dir }
os.cp_all('ex', './', true) or { panic(err) } assert old == new
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)
}
assert old2 == new2
// recurring on dir -> local dir
os.cp_all('ex', './', true) or {
panic(err)
}
} }
fn test_tmpdir(){ fn test_tmpdir() {
t := os.temp_dir() t := os.temp_dir()
assert t.len > 0 assert t.len > 0
assert os.is_dir(t) assert os.is_dir(t)
tfile := t + os.path_separator + 'tmpfile.txt' tfile := t + os.path_separator + 'tmpfile.txt'
os.rm(tfile) // just in case os.rm(tfile) // just in case
tfile_content := 'this is a temporary file' tfile_content := 'this is a temporary file'
os.write_file(tfile, tfile_content) os.write_file(tfile, tfile_content)
tfile_content_read := os.read_file(tfile) or {
tfile_content_read := os.read_file(tfile) or { panic(err) } panic(err)
}
assert tfile_content_read == tfile_content assert tfile_content_read == tfile_content
os.rm(tfile) os.rm(tfile)
} }
fn test_make_symlink_check_is_link_and_remove_symlink() { fn test_make_symlink_check_is_link_and_remove_symlink() {
$if windows { $if windows {
// TODO // TODO
assert true assert true
return return
} }
folder := 'tfolder'
folder := 'tfolder' symlink := 'tsymlink'
symlink := 'tsymlink' os.rm(symlink)
os.rm(folder)
os.rm(symlink) os.mkdir(folder) or {
os.rm(folder) panic(err)
}
os.mkdir(folder) or { panic(err) } folder_contents := os.ls(folder) or {
folder_contents := os.ls(folder) or { panic(err) } panic(err)
assert folder_contents.len == 0 }
assert folder_contents.len == 0
os.system('ln -s $folder $symlink') os.system('ln -s $folder $symlink')
assert os.is_link(symlink) == true assert os.is_link(symlink) == true
os.rm(symlink)
os.rm(symlink) os.rm(folder)
os.rm(folder) folder_exists := os.is_dir(folder)
assert folder_exists == false
folder_exists := os.is_dir(folder) symlink_exists := os.is_link(symlink)
assert folder_exists == false assert symlink_exists == false
symlink_exists := os.is_link(symlink)
assert symlink_exists == false
} }
//fn test_fork() { // fn test_fork() {
// pid := os.fork() // pid := os.fork()
// if pid == 0 { // if pid == 0 {
// println('Child') // println('Child')
// } // }
// else { // else {
// println('Parent') // println('Parent')
// } // }
//} // }
// fn test_wait() {
//fn test_wait() { // pid := os.fork()
// pid := os.fork() // if pid == 0 {
// if pid == 0 { // println('Child')
// println('Child') // exit(0)
// exit(0) // }
// } // else {
// else { // cpid := os.wait()
// cpid := os.wait() // println('Parent')
// println('Parent') // println(cpid)
// println(cpid) // }
// } // }
//}
fn test_symlink() { fn test_symlink() {
$if windows { return } $if windows {
os.mkdir('symlink') or { panic(err) } return
os.symlink('symlink', 'symlink2') or { panic(err) } }
assert os.exists('symlink2') os.mkdir('symlink') or {
panic(err)
// cleanup }
os.rm('symlink') os.symlink('symlink', 'symlink2') or {
os.rm('symlink2') panic(err)
}
assert os.exists('symlink2')
// cleanup
os.rm('symlink')
os.rm('symlink2')
} }
fn test_is_executable_writable_readable() { fn test_is_executable_writable_readable() {
file_name := os.temp_dir() + os.path_separator + 'rwxfile.exe' file_name := os.temp_dir() + os.path_separator + 'rwxfile.exe'
mut f := os.create(file_name) or {
mut f := os.create(file_name) or { eprintln('failed to create file $file_name')
eprintln('failed to create file $file_name') return
return }
} f.close()
f.close() $if !windows {
os.chmod(file_name, 0600) // mark as readable && writable, but NOT executable
$if !windows { assert os.is_writable(file_name)
os.chmod(file_name, 0o600) // mark as readable && writable, but NOT executable assert os.is_readable(file_name)
assert os.is_writable(file_name) assert !os.is_executable(file_name)
assert os.is_readable(file_name) os.chmod(file_name, 0700) // mark as executable too
assert !os.is_executable(file_name) assert os.is_executable(file_name)
os.chmod(file_name, 0o700) // mark as executable too } $else {
assert os.is_executable(file_name) assert os.is_writable(file_name)
} $else { assert os.is_readable(file_name)
assert os.is_writable(file_name) assert os.is_executable(file_name)
assert os.is_readable(file_name) }
assert os.is_executable(file_name) // We finally delete the test file.
} os.rm(file_name)
// We finally delete the test file.
os.rm(file_name)
} }
fn test_ext() { fn test_ext() {
@ -280,7 +289,6 @@ fn test_ext() {
fn test_is_abs() { fn test_is_abs() {
assert os.is_abs_path('/home/user') == true assert os.is_abs_path('/home/user') == true
assert os.is_abs_path('v/vlib') == false assert os.is_abs_path('v/vlib') == false
$if windows { $if windows {
assert os.is_abs_path('C:\\Windows\\') == true assert os.is_abs_path('C:\\Windows\\') == true
} }
@ -288,9 +296,9 @@ fn test_is_abs() {
fn test_join() { fn test_join() {
$if windows { $if windows {
assert os.join_path('v', 'vlib', 'os') == 'v\\vlib\\os' assert os.join_path('v','vlib','os') == 'v\\vlib\\os'
} $else { } $else {
assert os.join_path('v', 'vlib', 'os') == 'v/vlib/os' assert os.join_path('v','vlib','os') == 'v/vlib/os'
} }
} }
@ -300,7 +308,6 @@ fn test_dir() {
} $else { } $else {
assert os.dir('/var/tmp/foo') == '/var/tmp' assert os.dir('/var/tmp/foo') == '/var/tmp'
} }
assert os.dir('os') == '.' assert os.dir('os') == '.'
} }
@ -310,7 +317,6 @@ fn test_basedir() {
} $else { } $else {
assert os.base_dir('v/vlib/os') == 'v/vlib' assert os.base_dir('v/vlib/os') == 'v/vlib'
} }
assert os.base_dir('filename') == 'filename' assert os.base_dir('filename') == 'filename'
} }
@ -321,7 +327,6 @@ fn cleanup_leftovers() {
// possible leftovers from test_cp // possible leftovers from test_cp
os.rm('cp_example.txt') os.rm('cp_example.txt')
os.rm('cp_new_example.txt') os.rm('cp_new_example.txt')
// possible leftovers from test_cp_r // possible leftovers from test_cp_r
os.rmdir_all('ex') os.rmdir_all('ex')
os.rmdir_all('ex2') os.rmdir_all('ex2')

View File

@ -474,6 +474,7 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) {
} }
pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type { pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type {
// println('checker: array init $array_init.pos.line_nr $c.file.path')
mut elem_type := table.void_type mut elem_type := table.void_type
// []string - was set in parser // []string - was set in parser
if array_init.typ != table.void_type { if array_init.typ != table.void_type {