v/vlib/os/os_test.v

336 lines
7.3 KiB
V
Raw Normal View History

2020-03-07 22:26:26 +01:00
import os
2019-06-27 13:51:03 +02:00
const (
2020-03-26 00:09:30 +01:00
// tfolder will contain all the temporary files/subfolders made by
// the different tests. It would be removed in testsuite_end(), so
// individual os tests do not need to clean up after themselves.
tfolder = os.join_path( os.temp_dir(), 'v', 'tests', 'os_test')
)
2020-02-21 12:24:13 +01:00
fn testsuite_begin() {
2020-03-26 00:09:30 +01:00
eprintln('testsuite_begin, tfolder = $tfolder')
os.rmdir_all( tfolder )
assert !os.is_dir( tfolder )
os.mkdir_all( tfolder )
os.chdir( tfolder )
assert os.is_dir( tfolder )
2020-02-21 12:24:13 +01:00
}
fn testsuite_end() {
2020-03-26 00:09:30 +01:00
os.chdir( os.wd_at_startup )
os.rmdir_all( tfolder )
assert !os.is_dir( tfolder )
}
2020-01-21 16:58:47 +01:00
fn test_open_file() {
2020-03-24 16:18:37 +01:00
filename := './test1.txt'
hello := 'hello world!'
2020-03-24 17:16:00 +01:00
os.open_file(filename, 'r+', 0o666) or {
2020-03-24 16:18:37 +01:00
assert err == 'No such file or directory'
}
2020-03-24 17:16:00 +01:00
mut file := os.open_file(filename, 'w+', 0o666) or {
2020-03-24 16:18:37 +01:00
panic(err)
}
file.write(hello)
file.close()
assert hello.len == os.file_size(filename)
read_hello := os.read_file(filename) or {
panic('error reading file $filename')
}
assert hello == read_hello
os.rm(filename)
2020-01-21 16:58:47 +01:00
}
2020-02-23 20:46:13 +01:00
fn test_create_file() {
filename := './test1.txt'
hello := 'hello world!'
2020-03-24 16:18:37 +01:00
mut f := os.create(filename) or {
panic(err)
}
2020-02-23 20:46:13 +01:00
f.write(hello)
f.close()
assert hello.len == os.file_size(filename)
os.rm(filename)
}
2019-07-01 23:24:19 +02:00
fn test_write_and_read_string_to_file() {
2020-03-24 16:18:37 +01:00
filename := './test1.txt'
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')
}
assert hello == read_hello
os.rm(filename)
2019-07-01 23:24:19 +02:00
}
2019-07-14 04:18:54 +02:00
// test_write_and_read_bytes checks for regressions made in the functions
// read_bytes, read_bytes_at and write_bytes.
/*
fn test_write_and_read_bytes() {
file_name := './byte_reader_writer.tst'
payload := [`I`, `D`, `D`, `Q`, `D`]
2020-03-20 16:41:18 +01:00
mut file_write := os.create(os.real_path(file_name)) or {
eprintln('failed to create file $file_name')
return
}
// We use the standard write_bytes function to write the payload and
// compare the length of the array with the file size (have to match).
file_write.write_bytes(payload.data, 5)
file_write.close()
assert payload.len == os.file_size(file_name)
2020-03-20 16:41:18 +01:00
mut file_read := os.open(os.real_path(file_name)) or {
eprintln('failed to open file $file_name')
return
}
// We only need to test read_bytes because this function calls
// read_bytes_at with second parameter zeroed (size, 0).
red_bytes := file_read.read_bytes(5)
file_read.close()
assert red_bytes.str() == payload.str()
// We finally delete the test file.
os.rm(file_name)
}
*/
2020-03-24 16:18:37 +01:00
fn test_create_and_delete_folder() {
2020-03-24 16:18:37 +01:00
folder := './test1'
os.mkdir(folder) or {
panic(err)
}
assert os.is_dir(folder)
2020-03-24 16:18:37 +01:00
folder_contents := os.ls(folder) or {
panic(err)
}
assert folder_contents.len == 0
os.rmdir(folder)
folder_exists := os.is_dir(folder)
assert folder_exists == false
}
2019-10-24 14:17:09 +02:00
fn walk_callback(file string) {
2020-03-24 16:18:37 +01:00
if file == '.' || file == '..' {
return
}
assert file == 'test_walk' + os.path_separator + 'test1'
2019-10-24 14:17:09 +02:00
}
fn test_walk() {
2020-03-24 16:18:37 +01:00
folder := 'test_walk'
os.mkdir(folder) or {
panic(err)
}
file1 := folder + os.path_separator + 'test1'
os.write_file(file1, 'test-1')
os.walk(folder, walk_callback)
2019-10-24 14:17:09 +02:00
os.rm(file1)
os.rmdir(folder)
}
2019-11-02 20:37:29 +01:00
fn test_cp() {
2020-03-24 16:18:37 +01:00
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)
}
assert old_file == new_file
os.rm(old_file_name)
os.rm(new_file_name)
2019-11-02 20:37:29 +01:00
}
2019-11-06 21:05:35 +01:00
fn test_cp_r() {
2020-03-24 16:18:37 +01:00
// 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)
}
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)
}
2019-11-06 21:05:35 +01:00
}
2020-03-24 16:18:37 +01:00
fn test_tmpdir() {
2020-03-10 15:02:09 +01:00
t := os.temp_dir()
assert t.len > 0
assert os.is_dir(t)
2020-03-07 22:26:26 +01:00
tfile := t + os.path_separator + 'tmpfile.txt'
os.rm(tfile) // just in case
tfile_content := 'this is a temporary file'
os.write_file(tfile, tfile_content)
2020-03-24 16:18:37 +01:00
tfile_content_read := os.read_file(tfile) or {
panic(err)
}
assert tfile_content_read == tfile_content
os.rm(tfile)
}
2019-12-06 10:09:35 +01:00
fn test_make_symlink_check_is_link_and_remove_symlink() {
2020-03-24 16:18:37 +01:00
$if windows {
// TODO
assert true
return
}
folder := 'tfolder'
symlink := 'tsymlink'
os.rm(symlink)
os.rm(folder)
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
os.rm(symlink)
os.rm(folder)
folder_exists := os.is_dir(folder)
assert folder_exists == false
symlink_exists := os.is_link(symlink)
assert symlink_exists == false
2019-12-06 10:09:35 +01:00
}
2020-03-24 16:18:37 +01:00
// fn test_fork() {
// pid := os.fork()
// if pid == 0 {
// println('Child')
// }
// else {
// println('Parent')
// }
// }
// fn test_wait() {
// pid := os.fork()
// if pid == 0 {
// println('Child')
// exit(0)
// }
// else {
// cpid := os.wait()
// println('Parent')
// println(cpid)
// }
// }
fn test_symlink() {
2020-03-24 16:18:37 +01:00
$if windows {
return
}
os.mkdir('symlink') or {
panic(err)
}
os.symlink('symlink', 'symlink2') or {
panic(err)
}
assert os.exists('symlink2')
// cleanup
os.rm('symlink')
os.rm('symlink2')
}
fn test_is_executable_writable_readable() {
2020-03-26 00:09:30 +01:00
file_name := 'rwxfile.exe'
2020-03-24 16:18:37 +01:00
mut f := os.create(file_name) or {
eprintln('failed to create file $file_name')
return
}
f.close()
$if !windows {
os.chmod(file_name, 0o600) // mark as readable && writable, but NOT executable
2020-03-24 16:18:37 +01:00
assert os.is_writable(file_name)
assert os.is_readable(file_name)
assert !os.is_executable(file_name)
os.chmod(file_name, 0o700) // mark as executable too
2020-03-24 16:18:37 +01:00
assert os.is_executable(file_name)
} $else {
assert os.is_writable(file_name)
assert os.is_readable(file_name)
assert os.is_executable(file_name)
}
// We finally delete the test file.
os.rm(file_name)
}
2020-03-07 22:26:26 +01:00
fn test_ext() {
assert os.ext('file.v') == '.v'
assert os.ext('file') == ''
}
fn test_is_abs() {
2020-03-10 16:09:37 +01:00
assert os.is_abs_path('/home/user') == true
assert os.is_abs_path('v/vlib') == false
2020-03-07 22:26:26 +01:00
$if windows {
2020-03-10 16:09:37 +01:00
assert os.is_abs_path('C:\\Windows\\') == true
2020-03-07 22:26:26 +01:00
}
}
fn test_join() {
$if windows {
2020-03-24 16:18:37 +01:00
assert os.join_path('v','vlib','os') == 'v\\vlib\\os'
2020-03-07 22:26:26 +01:00
} $else {
2020-03-24 16:18:37 +01:00
assert os.join_path('v','vlib','os') == 'v/vlib/os'
2020-03-07 22:26:26 +01:00
}
}
fn test_dir() {
$if windows {
assert os.dir('C:\\a\\b\\c') == 'C:\\a\\b'
} $else {
assert os.dir('/var/tmp/foo') == '/var/tmp'
}
assert os.dir('os') == '.'
}
fn test_basedir() {
$if windows {
2020-03-08 15:43:56 +01:00
assert os.base_dir('v\\vlib\\os') == 'v\\vlib'
2020-03-07 22:26:26 +01:00
} $else {
2020-03-08 15:43:56 +01:00
assert os.base_dir('v/vlib/os') == 'v/vlib'
2020-03-07 22:26:26 +01:00
}
2020-03-08 15:43:56 +01:00
assert os.base_dir('filename') == 'filename'
2020-03-07 22:26:26 +01:00
}