2019-06-27 13:51:03 +02:00
|
|
|
import os
|
|
|
|
|
2019-11-07 20:04:18 +01:00
|
|
|
fn test_aaa_setup(){
|
|
|
|
cleanup_leftovers() assert true
|
|
|
|
}
|
|
|
|
|
2019-06-27 13:51:03 +02:00
|
|
|
fn test_setenv() {
|
|
|
|
os.setenv('foo', 'bar', true)
|
|
|
|
assert os.getenv('foo') == 'bar'
|
2019-11-06 22:40:37 +01:00
|
|
|
|
2019-06-27 13:51:03 +02:00
|
|
|
// `setenv` should not set if `overwrite` is false
|
|
|
|
os.setenv('foo', 'bar2', false)
|
|
|
|
assert os.getenv('foo') == 'bar'
|
2019-11-06 22:40:37 +01:00
|
|
|
|
2019-06-27 13:51:03 +02:00
|
|
|
// `setenv` should overwrite if `overwrite` is true
|
|
|
|
os.setenv('foo', 'bar2', true)
|
|
|
|
assert os.getenv('foo') == 'bar2'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_unsetenv() {
|
|
|
|
os.setenv('foo', 'bar', true)
|
|
|
|
os.unsetenv('foo')
|
|
|
|
assert os.getenv('foo') == ''
|
|
|
|
}
|
|
|
|
|
2019-07-01 23:24:19 +02:00
|
|
|
fn test_write_and_read_string_to_file() {
|
|
|
|
filename := './test1.txt'
|
|
|
|
hello := 'hello world!'
|
|
|
|
os.write_file(filename, hello)
|
|
|
|
assert hello.len == os.file_size(filename)
|
2019-11-06 22:40:37 +01:00
|
|
|
|
2019-07-01 23:24:19 +02:00
|
|
|
read_hello := os.read_file(filename) or {
|
|
|
|
panic('error reading file $filename')
|
|
|
|
}
|
|
|
|
assert hello == read_hello
|
2019-07-04 18:13:25 +02:00
|
|
|
|
|
|
|
os.rm(filename)
|
2019-07-01 23:24:19 +02:00
|
|
|
}
|
2019-07-14 04:18:54 +02:00
|
|
|
|
2019-10-20 19:45:16 +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`]
|
|
|
|
|
2019-12-01 10:50:13 +01:00
|
|
|
mut file_write := os.create(os.realpath(file_name)) or {
|
2019-10-20 19:45:16 +02:00
|
|
|
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)
|
|
|
|
|
2019-12-01 10:50:13 +01:00
|
|
|
mut file_read := os.open(os.realpath(file_name)) or {
|
2019-10-20 19:45:16 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-16 15:55:51 +02:00
|
|
|
fn test_create_and_delete_folder() {
|
|
|
|
folder := './test1'
|
2019-11-23 17:55:18 +01:00
|
|
|
os.mkdir(folder) or { panic(err) }
|
2019-11-14 22:46:40 +01:00
|
|
|
assert os.dir_exists(folder)
|
2019-07-16 15:55:51 +02:00
|
|
|
|
2019-10-17 13:30:05 +02:00
|
|
|
folder_contents := os.ls(folder) or { panic(err) }
|
2019-07-16 15:55:51 +02:00
|
|
|
assert folder_contents.len == 0
|
|
|
|
|
|
|
|
os.rmdir(folder)
|
|
|
|
|
|
|
|
folder_exists := os.dir_exists(folder)
|
|
|
|
|
|
|
|
assert folder_exists == false
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:57:03 +02:00
|
|
|
fn test_dir() {
|
|
|
|
$if windows {
|
2019-11-06 22:40:37 +01:00
|
|
|
assert os.dir('C:\\a\\b\\c') == 'C:\\a\\b'
|
|
|
|
|
|
|
|
} $else {
|
|
|
|
assert os.dir('/var/tmp/foo') == '/var/tmp'
|
|
|
|
}
|
|
|
|
}
|
2019-07-16 01:57:03 +02:00
|
|
|
|
2019-10-24 14:17:09 +02:00
|
|
|
fn walk_callback(file string) {
|
|
|
|
if file == '.' || file == '..' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert file == 'test_walk'+os.path_separator+'test1'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_walk() {
|
|
|
|
folder := 'test_walk'
|
2019-11-23 17:55:18 +01:00
|
|
|
os.mkdir(folder) or { panic(err) }
|
2019-11-06 22:40:37 +01:00
|
|
|
|
2019-10-24 14:17:09 +02:00
|
|
|
file1 := folder+os.path_separator+'test1'
|
2019-11-06 22:40:37 +01:00
|
|
|
|
2019-10-24 14:17:09 +02:00
|
|
|
os.write_file(file1,'test-1')
|
2019-11-06 22:40:37 +01:00
|
|
|
|
2019-10-24 14:17:09 +02:00
|
|
|
os.walk(folder, walk_callback)
|
|
|
|
|
|
|
|
os.rm(file1)
|
|
|
|
os.rmdir(folder)
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:37:29 +01:00
|
|
|
fn test_cp() {
|
2019-11-07 20:04:18 +01:00
|
|
|
old_file_name := 'cp_example.txt'
|
|
|
|
new_file_name := 'cp_new_example.txt'
|
2019-11-14 22:46:40 +01:00
|
|
|
|
2019-11-02 20:37:29 +01:00
|
|
|
os.write_file(old_file_name, 'Test data 1 2 3, V is awesome #$%^[]!~⭐')
|
2019-11-07 20:04:18 +01:00
|
|
|
os.cp(old_file_name, new_file_name) or { panic('$err: errcode: $errcode') }
|
2019-11-02 20:37:29 +01:00
|
|
|
|
|
|
|
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
|
2019-11-06 22:40:37 +01:00
|
|
|
|
2019-11-02 20:37:29 +01:00
|
|
|
os.rm(old_file_name)
|
|
|
|
os.rm(new_file_name)
|
|
|
|
}
|
|
|
|
|
2019-11-06 21:05:35 +01:00
|
|
|
fn test_cp_r() {
|
|
|
|
//fileX -> dir/fileX
|
2019-11-07 20:04:18 +01:00
|
|
|
// NB: clean up of the files happens inside the cleanup_leftovers function
|
2019-11-06 21:05:35 +01:00
|
|
|
os.write_file('ex1.txt', 'wow!')
|
2019-11-23 17:55:18 +01:00
|
|
|
os.mkdir('ex') or { panic(err) }
|
2019-11-06 21:05:35 +01:00
|
|
|
os.cp_r('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
|
2019-11-23 17:55:18 +01:00
|
|
|
os.mkdir('ex/ex2') or { panic(err) }
|
2019-11-06 21:05:35 +01:00
|
|
|
os.write_file('ex2.txt', 'great!')
|
|
|
|
os.cp_r('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_r('ex', './', true) or { panic(err) }
|
|
|
|
}
|
|
|
|
|
2019-11-17 04:45:20 +01:00
|
|
|
fn test_tmpdir(){
|
|
|
|
t := os.tmpdir()
|
|
|
|
assert t.len > 0
|
|
|
|
assert os.is_dir(t)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
tfile_content_read := os.read_file(tfile) or { panic(err) }
|
|
|
|
assert tfile_content_read == tfile_content
|
|
|
|
|
|
|
|
os.rm(tfile)
|
|
|
|
}
|
|
|
|
|
2019-07-14 04:18:54 +02: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)
|
|
|
|
// }
|
|
|
|
//}
|
2019-11-07 20:04:18 +01:00
|
|
|
|
|
|
|
fn test_zzz_cleanup(){
|
|
|
|
cleanup_leftovers() assert true
|
|
|
|
}
|
|
|
|
|
|
|
|
// this function is called by both test_aaa_setup & test_zzz_cleanup
|
|
|
|
// it ensures that os tests do not polute the filesystem with leftover
|
|
|
|
// files so that they can be run several times in a row.
|
|
|
|
fn cleanup_leftovers(){
|
|
|
|
// possible leftovers from test_cp
|
|
|
|
os.rm('cp_example.txt')
|
|
|
|
os.rm('cp_new_example.txt')
|
|
|
|
|
|
|
|
// possible leftovers from test_cp_r
|
|
|
|
os.rm('ex/ex2/ex2.txt')
|
|
|
|
os.rm('ex/ex2')
|
|
|
|
os.rm('ex/ex1.txt')
|
2019-11-14 22:46:40 +01:00
|
|
|
os.rm('ex')
|
2019-11-07 20:04:18 +01:00
|
|
|
os.rm('ex2/ex2.txt')
|
2019-11-11 16:45:11 +01:00
|
|
|
os.rm('ex2')
|
|
|
|
os.rm('ex1.txt')
|
|
|
|
os.rm('ex2.txt')
|
2019-11-07 20:04:18 +01:00
|
|
|
}
|