2020-03-07 22:26:26 +01:00
|
|
|
import os
|
2019-06-27 13:51:03 +02:00
|
|
|
|
2020-03-24 22:18:58 +01: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.
|
2020-09-20 16:28:45 +02:00
|
|
|
tfolder = os.join_path(os.temp_dir(), 'v', 'tests', 'os_test')
|
2020-03-24 22:18:58 +01:00
|
|
|
)
|
|
|
|
|
2021-01-10 13:31:20 +01:00
|
|
|
// os.args has to be *already initialized* with the program's argc/argv at this point
|
|
|
|
// thus it can be used for other consts too:
|
|
|
|
const args_at_start = os.args.clone()
|
|
|
|
|
2020-02-21 12:24:13 +01:00
|
|
|
fn testsuite_begin() {
|
2021-01-25 12:08:43 +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)
|
2021-01-10 13:31:20 +01:00
|
|
|
// println('args_at_start: $args_at_start')
|
2021-01-25 12:08:43 +01:00
|
|
|
assert args_at_start.len > 0
|
|
|
|
assert args_at_start == os.args
|
2020-02-21 12:24:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn testsuite_end() {
|
2020-09-20 16:28:45 +02:00
|
|
|
os.chdir(os.wd_at_startup)
|
2021-01-25 12:08:43 +01:00
|
|
|
os.rmdir_all(tfolder)
|
|
|
|
assert !os.is_dir(tfolder)
|
2020-09-20 16:28:45 +02:00
|
|
|
// eprintln('testsuite_end , tfolder = $tfolder removed.')
|
2019-11-07 20:04:18 +01:00
|
|
|
}
|
|
|
|
|
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-04-07 16:36:00 +02:00
|
|
|
os.File{}
|
2020-03-24 16:18:37 +01:00
|
|
|
}
|
2020-12-16 10:02:36 +01:00
|
|
|
mut file := os.open_file(filename, 'w+', 0o666) or { panic(err) }
|
2020-11-15 21:54:47 +01:00
|
|
|
file.write_str(hello)
|
2020-03-24 16:18:37 +01:00
|
|
|
file.close()
|
|
|
|
assert hello.len == os.file_size(filename)
|
2020-12-16 10:02:36 +01:00
|
|
|
read_hello := os.read_file(filename) or { panic('error reading file $filename') }
|
2020-03-24 16:18:37 +01:00
|
|
|
assert hello == read_hello
|
|
|
|
os.rm(filename)
|
2020-01-21 16:58:47 +01:00
|
|
|
}
|
|
|
|
|
2020-09-20 16:28:45 +02:00
|
|
|
fn test_open_file_binary() {
|
|
|
|
filename := './test1.dat'
|
|
|
|
hello := 'hello \n world!'
|
|
|
|
os.open_file(filename, 'r+', 0o666) or {
|
|
|
|
assert err == 'No such file or directory'
|
|
|
|
os.File{}
|
|
|
|
}
|
2020-12-16 10:02:36 +01:00
|
|
|
mut file := os.open_file(filename, 'wb+', 0o666) or { panic(err) }
|
2020-09-20 16:28:45 +02:00
|
|
|
bytes := hello.bytes()
|
|
|
|
file.write_bytes(bytes.data, bytes.len)
|
|
|
|
file.close()
|
|
|
|
assert hello.len == os.file_size(filename)
|
2020-12-16 10:02:36 +01:00
|
|
|
read_hello := os.read_bytes(filename) or { panic('error reading file $filename') }
|
2020-09-20 16:28:45 +02:00
|
|
|
assert bytes == read_hello
|
|
|
|
os.rm(filename)
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
// fn test_file_get_line() {
|
|
|
|
// filename := './fgetline.txt'
|
|
|
|
// os.write_file(filename, 'line 1\nline 2')
|
|
|
|
// mut f := os.open_file(filename, 'r', 0) or {
|
|
|
|
// assert false
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// line1 := f.get_line() or {
|
|
|
|
// ''
|
|
|
|
// }
|
|
|
|
// line2 := f.get_line() or {
|
|
|
|
// ''
|
|
|
|
// }
|
|
|
|
// f.close()
|
|
|
|
// //
|
|
|
|
// eprintln('line1: $line1 $line1.bytes()')
|
|
|
|
// eprintln('line2: $line2 $line2.bytes()')
|
|
|
|
// assert line1 == 'line 1\n'
|
|
|
|
// assert line2 == 'line 2'
|
|
|
|
// }
|
2020-02-23 20:46:13 +01:00
|
|
|
fn test_create_file() {
|
|
|
|
filename := './test1.txt'
|
|
|
|
hello := 'hello world!'
|
2020-12-16 10:02:36 +01:00
|
|
|
mut f := os.create(filename) or { panic(err) }
|
2020-11-15 21:54:47 +01:00
|
|
|
f.write_str(hello)
|
2020-02-23 20:46:13 +01:00
|
|
|
f.close()
|
|
|
|
assert hello.len == os.file_size(filename)
|
|
|
|
os.rm(filename)
|
|
|
|
}
|
2020-09-06 12:39:02 +02:00
|
|
|
|
2020-09-04 22:27:52 +02:00
|
|
|
fn test_is_file() {
|
|
|
|
// Setup
|
2020-09-20 16:28:45 +02:00
|
|
|
work_dir := os.join_path(os.getwd(), 'is_file_test')
|
2020-09-04 22:27:52 +02:00
|
|
|
os.mkdir_all(work_dir)
|
2020-09-20 16:28:45 +02:00
|
|
|
tfile := os.join_path(work_dir, 'tmp_file')
|
2020-09-04 22:27:52 +02:00
|
|
|
// Test things that shouldn't be a file
|
|
|
|
assert os.is_file(work_dir) == false
|
|
|
|
assert os.is_file('non-existent_file.tmp') == false
|
|
|
|
// Test file
|
|
|
|
tfile_content := 'temporary file'
|
|
|
|
os.write_file(tfile, tfile_content)
|
|
|
|
assert os.is_file(tfile)
|
|
|
|
// Test dir symlinks
|
|
|
|
$if windows {
|
|
|
|
assert true
|
|
|
|
} $else {
|
2020-09-20 16:28:45 +02:00
|
|
|
dsymlink := os.join_path(work_dir, 'dir_symlink')
|
2020-09-04 22:27:52 +02:00
|
|
|
os.system('ln -s $work_dir $dsymlink')
|
|
|
|
assert os.is_file(dsymlink) == false
|
|
|
|
}
|
|
|
|
// Test file symlinks
|
|
|
|
$if windows {
|
|
|
|
assert true
|
|
|
|
} $else {
|
2020-09-20 16:28:45 +02:00
|
|
|
fsymlink := os.join_path(work_dir, 'file_symlink')
|
2020-09-04 22:27:52 +02:00
|
|
|
os.system('ln -s $tfile $fsymlink')
|
|
|
|
assert os.is_file(fsymlink)
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 12:39:02 +02:00
|
|
|
|
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)
|
2020-12-16 10:02:36 +01:00
|
|
|
read_hello := os.read_file(filename) or { panic('error reading file $filename') }
|
2020-03-24 16:18:37 +01:00
|
|
|
assert hello == read_hello
|
|
|
|
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() {
|
2020-09-20 16:28:45 +02:00
|
|
|
file_name := './byte_reader_writer.tst'
|
|
|
|
payload := [byte(`I`), `D`, `D`, `Q`, `D`]
|
2020-08-29 06:43:23 +02: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)
|
|
|
|
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).
|
|
|
|
rbytes := file_read.read_bytes(5)
|
|
|
|
// eprintln('rbytes: $rbytes')
|
|
|
|
// eprintln('payload: $payload')
|
|
|
|
assert rbytes == payload
|
2020-09-14 15:34:34 +02:00
|
|
|
// check that trying to read data from EOF doesn't error and returns 0
|
|
|
|
mut a := []byte{len: 5}
|
2020-09-27 10:18:55 +02:00
|
|
|
nread := file_read.read_bytes_into(5, mut a) or {
|
2020-09-14 15:34:34 +02:00
|
|
|
eprintln(err)
|
|
|
|
int(-1)
|
|
|
|
}
|
|
|
|
assert nread == 0
|
|
|
|
file_read.close()
|
2020-08-29 06:43:23 +02:00
|
|
|
// We finally delete the test file.
|
|
|
|
os.rm(file_name)
|
2019-10-20 19:45:16 +02:00
|
|
|
}
|
2020-03-24 16:18:37 +01:00
|
|
|
|
2019-07-16 15:55:51 +02:00
|
|
|
fn test_create_and_delete_folder() {
|
2020-03-24 16:18:37 +01:00
|
|
|
folder := './test1'
|
2020-12-16 10:02:36 +01:00
|
|
|
os.mkdir(folder) or { panic(err) }
|
2019-12-04 21:03:12 +01:00
|
|
|
assert os.is_dir(folder)
|
2020-12-16 10:02:36 +01:00
|
|
|
folder_contents := os.ls(folder) or { panic(err) }
|
2020-03-24 16:18:37 +01:00
|
|
|
assert folder_contents.len == 0
|
|
|
|
os.rmdir(folder)
|
|
|
|
folder_exists := os.is_dir(folder)
|
|
|
|
assert folder_exists == false
|
2019-07-16 15:55:51 +02:00
|
|
|
}
|
|
|
|
|
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'
|
2020-12-16 10:02:36 +01:00
|
|
|
os.mkdir(folder) or { panic(err) }
|
2020-03-24 16:18:37 +01:00
|
|
|
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 #$%^[]!~⭐')
|
2020-12-16 10:02:36 +01:00
|
|
|
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) }
|
2020-03-24 16:18:37 +01:00
|
|
|
assert old_file == new_file
|
|
|
|
os.rm(old_file_name)
|
|
|
|
os.rm(new_file_name)
|
2019-11-02 20:37:29 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 13:08:47 +02:00
|
|
|
fn test_mv() {
|
2020-09-20 16:28:45 +02:00
|
|
|
work_dir := os.join_path(os.getwd(), 'mv_test')
|
2020-09-06 13:12:07 +02:00
|
|
|
os.mkdir_all(work_dir)
|
2020-09-04 13:08:47 +02:00
|
|
|
// Setup test files
|
2020-09-20 16:28:45 +02:00
|
|
|
tfile1 := os.join_path(work_dir, 'file')
|
|
|
|
tfile2 := os.join_path(work_dir, 'file.test')
|
|
|
|
tfile3 := os.join_path(work_dir, 'file.3')
|
2020-09-04 13:08:47 +02:00
|
|
|
tfile_content := 'temporary file'
|
|
|
|
os.write_file(tfile1, tfile_content)
|
|
|
|
os.write_file(tfile2, tfile_content)
|
|
|
|
// Setup test dirs
|
2020-09-20 16:28:45 +02:00
|
|
|
tdir1 := os.join_path(work_dir, 'dir')
|
|
|
|
tdir2 := os.join_path(work_dir, 'dir2')
|
|
|
|
tdir3 := os.join_path(work_dir, 'dir3')
|
2020-09-04 13:08:47 +02:00
|
|
|
mkdir(tdir1)
|
|
|
|
mkdir(tdir2)
|
|
|
|
// Move file with no extension to dir
|
2020-09-20 16:28:45 +02:00
|
|
|
os.mv(tfile1, tdir1)
|
|
|
|
mut expected := os.join_path(tdir1, 'file')
|
2020-09-04 13:08:47 +02:00
|
|
|
assert os.exists(expected) && !is_dir(expected) == true
|
|
|
|
// Move dir with contents to other dir
|
2020-09-20 16:28:45 +02:00
|
|
|
os.mv(tdir1, tdir2)
|
|
|
|
expected = os.join_path(tdir2, 'dir')
|
2020-09-04 13:08:47 +02:00
|
|
|
assert os.exists(expected) && is_dir(expected) == true
|
2020-09-20 16:28:45 +02:00
|
|
|
expected = os.join_path(tdir2, 'dir', 'file')
|
2020-09-04 13:08:47 +02:00
|
|
|
assert os.exists(expected) && !is_dir(expected) == true
|
|
|
|
// Move dir with contents to other dir (by renaming)
|
2020-09-20 16:28:45 +02:00
|
|
|
os.mv(os.join_path(tdir2, 'dir'), tdir3)
|
2020-09-04 13:08:47 +02:00
|
|
|
expected = tdir3
|
|
|
|
assert os.exists(expected) && is_dir(expected) == true
|
|
|
|
assert os.is_dir_empty(tdir2) == true
|
|
|
|
// Move file with extension to dir
|
2020-09-20 16:28:45 +02:00
|
|
|
os.mv(tfile2, tdir2)
|
|
|
|
expected = os.join_path(tdir2, 'file.test')
|
2020-09-04 13:08:47 +02:00
|
|
|
assert os.exists(expected) && !is_dir(expected) == true
|
|
|
|
// Move file to dir (by renaming)
|
2020-09-20 16:28:45 +02:00
|
|
|
os.mv(os.join_path(tdir2, 'file.test'), tfile3)
|
2020-09-04 13:08:47 +02:00
|
|
|
expected = tfile3
|
|
|
|
assert os.exists(expected) && !is_dir(expected) == true
|
|
|
|
}
|
|
|
|
|
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!')
|
2020-12-16 10:02:36 +01:00
|
|
|
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) }
|
2020-03-24 16:18:37 +01:00
|
|
|
assert old == new
|
2020-12-16 10:02:36 +01:00
|
|
|
os.mkdir('ex/ex2') or { panic(err) }
|
2020-03-24 16:18:37 +01:00
|
|
|
os.write_file('ex2.txt', 'great!')
|
2020-12-16 10:02:36 +01:00
|
|
|
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) }
|
2020-03-24 16:18:37 +01:00
|
|
|
assert old2 == new2
|
|
|
|
// recurring on dir -> local dir
|
2020-12-16 10:02:36 +01:00
|
|
|
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()
|
2019-11-17 04:45:20 +01:00
|
|
|
assert t.len > 0
|
|
|
|
assert os.is_dir(t)
|
2020-03-07 22:26:26 +01:00
|
|
|
tfile := t + os.path_separator + 'tmpfile.txt'
|
2019-11-17 04:45:20 +01:00
|
|
|
os.rm(tfile) // just in case
|
|
|
|
tfile_content := 'this is a temporary file'
|
|
|
|
os.write_file(tfile, tfile_content)
|
2020-12-16 10:02:36 +01:00
|
|
|
tfile_content_read := os.read_file(tfile) or { panic(err) }
|
2019-11-17 04:45:20 +01:00
|
|
|
assert tfile_content_read == tfile_content
|
|
|
|
os.rm(tfile)
|
|
|
|
}
|
|
|
|
|
2020-04-05 15:56:54 +02:00
|
|
|
fn test_is_writable_folder() {
|
|
|
|
tmp := os.temp_dir()
|
|
|
|
f := os.is_writable_folder(tmp) or {
|
|
|
|
eprintln('err: $err')
|
2020-04-07 16:36:00 +02:00
|
|
|
false
|
2020-04-05 15:56:54 +02:00
|
|
|
}
|
|
|
|
assert f
|
|
|
|
}
|
|
|
|
|
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)
|
2020-12-16 10:02:36 +01:00
|
|
|
os.mkdir(folder) or { panic(err) }
|
|
|
|
folder_contents := os.ls(folder) or { panic(err) }
|
2020-03-24 16:18:37 +01:00
|
|
|
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)
|
|
|
|
// }
|
|
|
|
// }
|
2019-12-31 11:28:10 +01:00
|
|
|
fn test_symlink() {
|
2020-03-24 16:18:37 +01:00
|
|
|
$if windows {
|
|
|
|
return
|
|
|
|
}
|
2020-12-16 10:02:36 +01:00
|
|
|
os.mkdir('symlink') or { panic(err) }
|
|
|
|
os.symlink('symlink', 'symlink2') or { panic(err) }
|
2020-03-24 16:18:37 +01:00
|
|
|
assert os.exists('symlink2')
|
|
|
|
// cleanup
|
|
|
|
os.rm('symlink')
|
|
|
|
os.rm('symlink2')
|
2019-12-31 11:28:10 +01:00
|
|
|
}
|
|
|
|
|
2020-01-06 09:15:06 +01:00
|
|
|
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 {
|
2020-03-24 22:18:58 +01:00
|
|
|
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)
|
2020-03-24 22:18:58 +01:00
|
|
|
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-01-06 09:15:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 22:26:26 +01:00
|
|
|
fn test_ext() {
|
2020-03-26 14:18:08 +01:00
|
|
|
assert os.file_ext('file.v') == '.v'
|
|
|
|
assert os.file_ext('file') == ''
|
2020-03-07 22:26:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-20 16:28:45 +02:00
|
|
|
assert os.join_path('v', 'vlib', 'os') == 'v\\vlib\\os'
|
2020-03-07 22:26:26 +01:00
|
|
|
} $else {
|
2020-09-20 16:28:45 +02: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'
|
2020-12-12 10:59:43 +01:00
|
|
|
assert os.dir('C:\\a\\b\\') == 'C:\\a\\b'
|
2020-03-07 22:26:26 +01:00
|
|
|
} $else {
|
|
|
|
assert os.dir('/var/tmp/foo') == '/var/tmp'
|
2020-12-12 10:59:43 +01:00
|
|
|
assert os.dir('/var/tmp/') == '/var/tmp'
|
2020-03-07 22:26:26 +01:00
|
|
|
}
|
|
|
|
assert os.dir('os') == '.'
|
|
|
|
}
|
|
|
|
|
2020-10-01 01:25:52 +02:00
|
|
|
fn test_base() {
|
2020-03-07 22:26:26 +01:00
|
|
|
$if windows {
|
2020-10-01 01:25:52 +02:00
|
|
|
assert os.base('v\\vlib\\os') == 'os'
|
|
|
|
assert os.base('v\\vlib\\os\\') == 'os'
|
2020-03-07 22:26:26 +01:00
|
|
|
} $else {
|
2020-10-01 01:25:52 +02:00
|
|
|
assert os.base('v/vlib/os') == 'os'
|
|
|
|
assert os.base('v/vlib/os/') == 'os'
|
2020-03-07 22:26:26 +01:00
|
|
|
}
|
2020-10-01 01:25:52 +02:00
|
|
|
assert os.base('filename') == 'filename'
|
2020-03-07 22:26:26 +01:00
|
|
|
}
|
2020-06-14 15:46:30 +02:00
|
|
|
|
|
|
|
fn test_uname() {
|
|
|
|
u := os.uname()
|
|
|
|
assert u.sysname.len > 0
|
|
|
|
assert u.nodename.len > 0
|
|
|
|
assert u.release.len > 0
|
|
|
|
assert u.version.len > 0
|
|
|
|
assert u.machine.len > 0
|
|
|
|
}
|
2020-07-13 09:45:10 +02:00
|
|
|
|
|
|
|
// tests for write_file_array and read_file_array<T>:
|
|
|
|
const (
|
|
|
|
maxn = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
struct IntPoint {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_write_file_array_bytes() {
|
|
|
|
fpath := './abytes.bin'
|
2021-01-25 12:08:43 +01:00
|
|
|
mut arr := []byte{len: maxn}
|
|
|
|
for i in 0 .. maxn {
|
2020-07-13 09:45:10 +02:00
|
|
|
arr[i] = 65 + byte(i)
|
|
|
|
}
|
|
|
|
os.write_file_array(fpath, arr)
|
2020-12-16 10:02:36 +01:00
|
|
|
rarr := os.read_bytes(fpath) or { panic(err) }
|
2020-07-13 09:45:10 +02:00
|
|
|
assert arr == rarr
|
2020-09-20 16:28:45 +02:00
|
|
|
// eprintln(arr.str())
|
|
|
|
// eprintln(rarr.str())
|
2020-07-13 09:45:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_write_file_array_structs() {
|
|
|
|
fpath := './astructs.bin'
|
2021-01-25 12:08:43 +01:00
|
|
|
mut arr := []IntPoint{len: maxn}
|
|
|
|
for i in 0 .. maxn {
|
2020-07-13 09:45:10 +02:00
|
|
|
arr[i] = IntPoint{65 + i, 65 + i + 10}
|
|
|
|
}
|
|
|
|
os.write_file_array(fpath, arr)
|
|
|
|
rarr := os.read_file_array<IntPoint>(fpath)
|
|
|
|
assert rarr == arr
|
2021-01-25 12:08:43 +01:00
|
|
|
assert rarr.len == maxn
|
2020-09-20 16:28:45 +02:00
|
|
|
// eprintln( rarr.str().replace('\n', ' ').replace('},', '},\n'))
|
2020-07-13 09:45:10 +02:00
|
|
|
}
|
2020-11-05 08:44:34 +01:00
|
|
|
|
|
|
|
fn test_stdout_capture() {
|
|
|
|
/*
|
2020-12-16 10:02:36 +01:00
|
|
|
mut cmd := os.Command{
|
2020-11-05 08:44:34 +01:00
|
|
|
path:'cat'
|
|
|
|
redirect_stdout: true
|
|
|
|
}
|
|
|
|
cmd.start()
|
|
|
|
for !cmd.eof {
|
|
|
|
line := cmd.read_line()
|
|
|
|
println('line="$line"')
|
|
|
|
}
|
|
|
|
cmd.close()
|
2020-12-16 10:02:36 +01:00
|
|
|
*/
|
2020-11-05 08:44:34 +01:00
|
|
|
}
|
2021-01-04 18:57:17 +01:00
|
|
|
|
|
|
|
fn test_posix_set_bit() {
|
|
|
|
$if windows {
|
|
|
|
assert true
|
|
|
|
} $else {
|
|
|
|
fpath := '/tmp/permtest'
|
|
|
|
create(fpath) or { panic("Couldn't create file") }
|
|
|
|
chmod(fpath, 0o7777)
|
|
|
|
c_fpath := charptr(fpath.str)
|
|
|
|
mut s := C.stat{}
|
|
|
|
unsafe {
|
|
|
|
C.stat(c_fpath, &s)
|
|
|
|
}
|
|
|
|
// Take the permissions part of the mode
|
|
|
|
mut mode := u32(s.st_mode) & 0o7777
|
|
|
|
assert mode == 0o7777
|
|
|
|
// `chmod u-r`
|
|
|
|
posix_set_permission_bit(fpath, os.s_irusr, false)
|
|
|
|
unsafe {
|
|
|
|
C.stat(c_fpath, &s)
|
|
|
|
}
|
|
|
|
mode = u32(s.st_mode) & 0o7777
|
|
|
|
assert mode == 0o7377
|
|
|
|
// `chmod u+r`
|
|
|
|
posix_set_permission_bit(fpath, os.s_irusr, true)
|
|
|
|
unsafe {
|
|
|
|
C.stat(c_fpath, &s)
|
|
|
|
}
|
|
|
|
mode = u32(s.st_mode) & 0o7777
|
|
|
|
assert mode == 0o7777
|
|
|
|
// `chmod -s -g -t`
|
|
|
|
posix_set_permission_bit(fpath, os.s_isuid, false)
|
|
|
|
posix_set_permission_bit(fpath, os.s_isgid, false)
|
|
|
|
posix_set_permission_bit(fpath, os.s_isvtx, false)
|
|
|
|
unsafe {
|
|
|
|
C.stat(c_fpath, &s)
|
|
|
|
}
|
|
|
|
mode = u32(s.st_mode) & 0o7777
|
|
|
|
assert mode == 0o0777
|
|
|
|
// `chmod g-w o-w`
|
|
|
|
posix_set_permission_bit(fpath, os.s_iwgrp, false)
|
|
|
|
posix_set_permission_bit(fpath, os.s_iwoth, false)
|
|
|
|
unsafe {
|
|
|
|
C.stat(c_fpath, &s)
|
|
|
|
}
|
|
|
|
mode = u32(s.st_mode) & 0o7777
|
|
|
|
assert mode == 0o0755
|
|
|
|
rm(fpath)
|
|
|
|
}
|
|
|
|
}
|