2020-03-08 00:26:26 +03:00
|
|
|
import os
|
2021-06-30 10:21:23 +03:00
|
|
|
import time
|
2019-06-27 14:51:03 +03:00
|
|
|
|
2020-03-25 00:18:58 +03:00
|
|
|
const (
|
2020-03-26 02:09:30 +03: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 17:28:45 +03:00
|
|
|
tfolder = os.join_path(os.temp_dir(), 'v', 'tests', 'os_test')
|
2020-03-25 00:18:58 +03:00
|
|
|
)
|
|
|
|
|
2021-01-10 15:31:20 +03: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 14:24:13 +03:00
|
|
|
fn testsuite_begin() {
|
2021-01-25 14:08:43 +03:00
|
|
|
eprintln('testsuite_begin, tfolder = $tfolder')
|
2021-03-06 22:04:51 +03:00
|
|
|
os.rmdir_all(tfolder) or {}
|
2021-01-25 14:08:43 +03:00
|
|
|
assert !os.is_dir(tfolder)
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir_all(tfolder) or { panic(err) }
|
2021-08-28 09:35:39 +03:00
|
|
|
os.chdir(tfolder) or {}
|
2021-01-25 14:08:43 +03:00
|
|
|
assert os.is_dir(tfolder)
|
2021-01-10 15:31:20 +03:00
|
|
|
// println('args_at_start: $args_at_start')
|
2021-01-25 14:08:43 +03:00
|
|
|
assert args_at_start.len > 0
|
|
|
|
assert args_at_start == os.args
|
2020-02-21 14:24:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn testsuite_end() {
|
2021-08-28 09:35:39 +03:00
|
|
|
os.chdir(os.wd_at_startup) or {}
|
2021-03-06 22:04:51 +03:00
|
|
|
os.rmdir_all(tfolder) or {}
|
2021-01-25 14:08:43 +03:00
|
|
|
assert !os.is_dir(tfolder)
|
2020-09-20 17:28:45 +03:00
|
|
|
// eprintln('testsuite_end , tfolder = $tfolder removed.')
|
2019-11-07 22:04:18 +03:00
|
|
|
}
|
|
|
|
|
2020-01-21 18:58:47 +03:00
|
|
|
fn test_open_file() {
|
2020-03-24 18:18:37 +03:00
|
|
|
filename := './test1.txt'
|
|
|
|
hello := 'hello world!'
|
2020-03-24 19:16:00 +03:00
|
|
|
os.open_file(filename, 'r+', 0o666) or {
|
2022-02-11 16:52:33 +03:00
|
|
|
assert err.msg() == 'No such file or directory'
|
2020-04-07 17:36:00 +03:00
|
|
|
os.File{}
|
2020-03-24 18:18:37 +03:00
|
|
|
}
|
2021-03-01 02:18:14 +03:00
|
|
|
mut file := os.open_file(filename, 'w+', 0o666) or { panic(err) }
|
2021-03-20 19:25:51 +03:00
|
|
|
file.write_string(hello) or { panic(err) }
|
2020-03-24 18:18:37 +03:00
|
|
|
file.close()
|
|
|
|
assert hello.len == os.file_size(filename)
|
2020-12-16 12:02:36 +03:00
|
|
|
read_hello := os.read_file(filename) or { panic('error reading file $filename') }
|
2020-03-24 18:18:37 +03:00
|
|
|
assert hello == read_hello
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rm(filename) or { panic(err) }
|
2020-01-21 18:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-09-20 17:28:45 +03:00
|
|
|
fn test_open_file_binary() {
|
|
|
|
filename := './test1.dat'
|
|
|
|
hello := 'hello \n world!'
|
|
|
|
os.open_file(filename, 'r+', 0o666) or {
|
2022-02-11 16:52:33 +03:00
|
|
|
assert err.msg() == 'No such file or directory'
|
2020-09-20 17:28:45 +03:00
|
|
|
os.File{}
|
|
|
|
}
|
2021-03-01 02:18:14 +03:00
|
|
|
mut file := os.open_file(filename, 'wb+', 0o666) or { panic(err) }
|
2020-09-20 17:28:45 +03:00
|
|
|
bytes := hello.bytes()
|
2021-03-20 10:02:28 +03:00
|
|
|
unsafe { file.write_ptr(bytes.data, bytes.len) }
|
2020-09-20 17:28:45 +03:00
|
|
|
file.close()
|
|
|
|
assert hello.len == os.file_size(filename)
|
2020-12-16 12:02:36 +03:00
|
|
|
read_hello := os.read_bytes(filename) or { panic('error reading file $filename') }
|
2020-09-20 17:28:45 +03:00
|
|
|
assert bytes == read_hello
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rm(filename) or { panic(err) }
|
2020-09-20 17:28:45 +03:00
|
|
|
}
|
|
|
|
|
2020-11-15 23:54:47 +03: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'
|
|
|
|
// }
|
2021-10-24 18:38:44 +03:00
|
|
|
|
|
|
|
fn create_file(fpath string) ? {
|
|
|
|
mut f := os.create(fpath) ?
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_and_write_to_file(fpath string, content string) ? {
|
|
|
|
mut f := os.create(fpath) ?
|
|
|
|
f.write_string(content) ?
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_create_file() ? {
|
2020-02-23 22:46:13 +03:00
|
|
|
filename := './test1.txt'
|
|
|
|
hello := 'hello world!'
|
2021-10-24 18:38:44 +03:00
|
|
|
create_and_write_to_file(filename, hello) ?
|
2020-02-23 22:46:13 +03:00
|
|
|
assert hello.len == os.file_size(filename)
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rm(filename) or { panic(err) }
|
2020-02-23 22:46:13 +03:00
|
|
|
}
|
2020-09-06 13:39:02 +03:00
|
|
|
|
2020-09-04 23:27:52 +03:00
|
|
|
fn test_is_file() {
|
|
|
|
// Setup
|
2021-11-22 15:40:55 +03:00
|
|
|
work_dir := os.join_path_single(os.getwd(), 'is_file_test')
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir_all(work_dir) or { panic(err) }
|
2021-11-22 15:40:55 +03:00
|
|
|
tfile := os.join_path_single(work_dir, 'tmp_file')
|
2020-09-04 23:27:52 +03: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'
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file(tfile, tfile_content) or { panic(err) }
|
2020-09-04 23:27:52 +03:00
|
|
|
assert os.is_file(tfile)
|
|
|
|
// Test dir symlinks
|
|
|
|
$if windows {
|
|
|
|
assert true
|
|
|
|
} $else {
|
2021-11-22 15:40:55 +03:00
|
|
|
dsymlink := os.join_path_single(work_dir, 'dir_symlink')
|
2021-07-12 14:23:25 +03:00
|
|
|
os.symlink(work_dir, dsymlink) or { panic(err) }
|
2020-09-04 23:27:52 +03:00
|
|
|
assert os.is_file(dsymlink) == false
|
|
|
|
}
|
|
|
|
// Test file symlinks
|
|
|
|
$if windows {
|
|
|
|
assert true
|
|
|
|
} $else {
|
2021-11-22 15:40:55 +03:00
|
|
|
fsymlink := os.join_path_single(work_dir, 'file_symlink')
|
2021-07-12 14:23:25 +03:00
|
|
|
os.symlink(tfile, fsymlink) or { panic(err) }
|
2020-09-04 23:27:52 +03:00
|
|
|
assert os.is_file(fsymlink)
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 13:39:02 +03:00
|
|
|
|
2019-07-02 00:24:19 +03:00
|
|
|
fn test_write_and_read_string_to_file() {
|
2020-03-24 18:18:37 +03:00
|
|
|
filename := './test1.txt'
|
|
|
|
hello := 'hello world!'
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file(filename, hello) or { panic(err) }
|
2020-03-24 18:18:37 +03:00
|
|
|
assert hello.len == os.file_size(filename)
|
2020-12-16 12:02:36 +03:00
|
|
|
read_hello := os.read_file(filename) or { panic('error reading file $filename') }
|
2020-03-24 18:18:37 +03:00
|
|
|
assert hello == read_hello
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rm(filename) or { panic(err) }
|
2019-07-02 00:24:19 +03:00
|
|
|
}
|
2019-07-14 05:18:54 +03:00
|
|
|
|
2019-10-20 20:45:16 +03: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 17:28:45 +03:00
|
|
|
file_name := './byte_reader_writer.tst'
|
|
|
|
payload := [byte(`I`), `D`, `D`, `Q`, `D`]
|
2020-08-29 07:43:23 +03: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).
|
2021-03-20 10:02:28 +03:00
|
|
|
unsafe { file_write.write_ptr(payload.data, 5) }
|
2020-08-29 07:43:23 +03:00
|
|
|
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 16:34:34 +03:00
|
|
|
// check that trying to read data from EOF doesn't error and returns 0
|
|
|
|
mut a := []byte{len: 5}
|
2020-09-27 11:18:55 +03:00
|
|
|
nread := file_read.read_bytes_into(5, mut a) or {
|
2021-05-13 09:48:55 +03:00
|
|
|
n := if err is none {
|
|
|
|
int(0)
|
|
|
|
} else {
|
|
|
|
eprintln(err)
|
|
|
|
int(-1)
|
|
|
|
}
|
|
|
|
n
|
2020-09-14 16:34:34 +03:00
|
|
|
}
|
|
|
|
assert nread == 0
|
|
|
|
file_read.close()
|
2020-08-29 07:43:23 +03:00
|
|
|
// We finally delete the test file.
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rm(file_name) or { panic(err) }
|
2019-10-20 20:45:16 +03:00
|
|
|
}
|
2020-03-24 18:18:37 +03:00
|
|
|
|
2021-10-24 18:38:44 +03:00
|
|
|
fn test_ls() {
|
|
|
|
if x := os.ls('') {
|
|
|
|
assert false
|
|
|
|
} else {
|
|
|
|
assert true
|
|
|
|
}
|
|
|
|
if x := os.ls('.') {
|
|
|
|
assert x.len > 0
|
|
|
|
} else {
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 19:12:14 +03:00
|
|
|
fn create_tree() ? {
|
2021-10-24 18:38:44 +03:00
|
|
|
os.mkdir_all('myfolder/f1/f2/f3') ?
|
|
|
|
os.mkdir_all('myfolder/a1/a2/a3') ?
|
2021-10-24 19:16:46 +03:00
|
|
|
f3 := os.real_path('myfolder/f1/f2/f3')
|
|
|
|
assert os.is_dir(f3)
|
2021-10-24 18:38:44 +03:00
|
|
|
create_file('myfolder/f1/f2/f3/a.txt') ?
|
|
|
|
create_file('myfolder/f1/f2/f3/b.txt') ?
|
|
|
|
create_file('myfolder/f1/f2/f3/c.txt') ?
|
|
|
|
create_file('myfolder/f1/f2/f3/d.md') ?
|
|
|
|
create_file('myfolder/f1/0.txt') ?
|
|
|
|
create_file('myfolder/another.md') ?
|
|
|
|
create_file('myfolder/a1/a2/a3/x.txt') ?
|
|
|
|
create_file('myfolder/a1/a2/a3/y.txt') ?
|
|
|
|
create_file('myfolder/a1/a2/a3/z.txt') ?
|
|
|
|
create_file('myfolder/a1/1.txt') ?
|
|
|
|
create_file('myfolder/xyz.ini') ?
|
2021-10-24 19:12:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_tree() {
|
|
|
|
os.rmdir_all('myfolder') or {}
|
|
|
|
}
|
|
|
|
|
2021-10-24 19:54:00 +03:00
|
|
|
fn normalise_paths(paths []string) []string {
|
|
|
|
mut res := paths.map(it.replace(os.path_separator, '/'))
|
|
|
|
res.sort()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-10-24 19:12:14 +03:00
|
|
|
fn test_walk_ext() ? {
|
|
|
|
create_tree() ?
|
|
|
|
defer {
|
|
|
|
remove_tree()
|
|
|
|
}
|
2021-10-24 18:38:44 +03:00
|
|
|
all := os.walk_ext('.', '')
|
|
|
|
assert all.len > 10
|
2021-10-24 19:54:00 +03:00
|
|
|
top := normalise_paths(os.walk_ext('myfolder', '.txt'))
|
2021-10-24 18:38:44 +03:00
|
|
|
assert top == [
|
|
|
|
'myfolder/a1/1.txt',
|
|
|
|
'myfolder/a1/a2/a3/x.txt',
|
|
|
|
'myfolder/a1/a2/a3/y.txt',
|
|
|
|
'myfolder/a1/a2/a3/z.txt',
|
|
|
|
'myfolder/f1/0.txt',
|
|
|
|
'myfolder/f1/f2/f3/a.txt',
|
|
|
|
'myfolder/f1/f2/f3/b.txt',
|
|
|
|
'myfolder/f1/f2/f3/c.txt',
|
|
|
|
]
|
2021-10-24 19:54:00 +03:00
|
|
|
subfolder_txts := normalise_paths(os.walk_ext('myfolder/a1/a2', '.txt'))
|
2021-10-24 18:38:44 +03:00
|
|
|
assert subfolder_txts == [
|
|
|
|
'myfolder/a1/a2/a3/x.txt',
|
|
|
|
'myfolder/a1/a2/a3/y.txt',
|
|
|
|
'myfolder/a1/a2/a3/z.txt',
|
|
|
|
]
|
2021-10-24 19:54:00 +03:00
|
|
|
mut mds := normalise_paths(os.walk_ext('myfolder', '.md'))
|
2021-10-24 18:38:44 +03:00
|
|
|
assert mds == ['myfolder/another.md', 'myfolder/f1/f2/f3/d.md']
|
2021-10-24 19:12:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_walk_with_context() ? {
|
|
|
|
create_tree() ?
|
|
|
|
defer {
|
|
|
|
remove_tree()
|
|
|
|
}
|
|
|
|
mut res := []string{}
|
|
|
|
os.walk_with_context('myfolder', &res, fn (mut res []string, fpath string) {
|
|
|
|
res << fpath
|
|
|
|
})
|
2021-10-24 19:54:00 +03:00
|
|
|
res = normalise_paths(res)
|
2021-10-24 19:12:14 +03:00
|
|
|
assert 'myfolder/f1/f2/f3/b.txt' in res
|
|
|
|
assert 'myfolder/another.md' in res
|
2021-10-24 18:38:44 +03:00
|
|
|
}
|
|
|
|
|
2019-07-16 16:55:51 +03:00
|
|
|
fn test_create_and_delete_folder() {
|
2020-03-24 18:18:37 +03:00
|
|
|
folder := './test1'
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir(folder) or { panic(err) }
|
2019-12-04 23:03:12 +03:00
|
|
|
assert os.is_dir(folder)
|
2021-03-01 02:18:14 +03:00
|
|
|
folder_contents := os.ls(folder) or { panic(err) }
|
2020-03-24 18:18:37 +03:00
|
|
|
assert folder_contents.len == 0
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rmdir(folder) or { panic(err) }
|
2020-03-24 18:18:37 +03:00
|
|
|
folder_exists := os.is_dir(folder)
|
|
|
|
assert folder_exists == false
|
2019-07-16 16:55:51 +03:00
|
|
|
}
|
|
|
|
|
2019-10-24 15:17:09 +03:00
|
|
|
fn walk_callback(file string) {
|
2020-03-24 18:18:37 +03:00
|
|
|
if file == '.' || file == '..' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert file == 'test_walk' + os.path_separator + 'test1'
|
2019-10-24 15:17:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_walk() {
|
2020-03-24 18:18:37 +03:00
|
|
|
folder := 'test_walk'
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir(folder) or { panic(err) }
|
2020-03-24 18:18:37 +03:00
|
|
|
file1 := folder + os.path_separator + 'test1'
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file(file1, 'test-1') or { panic(err) }
|
2020-03-24 18:18:37 +03:00
|
|
|
os.walk(folder, walk_callback)
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rm(file1) or { panic(err) }
|
|
|
|
os.rmdir(folder) or { panic(err) }
|
2019-10-24 15:17:09 +03:00
|
|
|
}
|
|
|
|
|
2019-11-02 22:37:29 +03:00
|
|
|
fn test_cp() {
|
2020-03-24 18:18:37 +03:00
|
|
|
old_file_name := 'cp_example.txt'
|
|
|
|
new_file_name := 'cp_new_example.txt'
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file(old_file_name, 'Test data 1 2 3, V is awesome #$%^[]!~⭐') or { panic(err) }
|
2021-02-28 22:24:29 +03:00
|
|
|
os.cp(old_file_name, new_file_name) or { panic('$err') }
|
2021-03-01 02:18:14 +03:00
|
|
|
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 18:18:37 +03:00
|
|
|
assert old_file == new_file
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rm(old_file_name) or { panic(err) }
|
|
|
|
os.rm(new_file_name) or { panic(err) }
|
2019-11-02 22:37:29 +03:00
|
|
|
}
|
|
|
|
|
2020-09-04 14:08:47 +03:00
|
|
|
fn test_mv() {
|
2021-11-22 15:40:55 +03:00
|
|
|
work_dir := os.join_path_single(os.getwd(), 'mv_test')
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir_all(work_dir) or { panic(err) }
|
2020-09-04 14:08:47 +03:00
|
|
|
// Setup test files
|
2021-11-22 15:40:55 +03:00
|
|
|
tfile1 := os.join_path_single(work_dir, 'file')
|
|
|
|
tfile2 := os.join_path_single(work_dir, 'file.test')
|
|
|
|
tfile3 := os.join_path_single(work_dir, 'file.3')
|
2020-09-04 14:08:47 +03:00
|
|
|
tfile_content := 'temporary file'
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file(tfile1, tfile_content) or { panic(err) }
|
|
|
|
os.write_file(tfile2, tfile_content) or { panic(err) }
|
2020-09-04 14:08:47 +03:00
|
|
|
// Setup test dirs
|
2021-11-22 15:40:55 +03:00
|
|
|
tdir1 := os.join_path_single(work_dir, 'dir')
|
|
|
|
tdir2 := os.join_path_single(work_dir, 'dir2')
|
|
|
|
tdir3 := os.join_path_single(work_dir, 'dir3')
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir(tdir1) or { panic(err) }
|
|
|
|
os.mkdir(tdir2) or { panic(err) }
|
2020-09-04 14:08:47 +03:00
|
|
|
// Move file with no extension to dir
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mv(tfile1, tdir1) or { panic(err) }
|
2021-11-22 15:40:55 +03:00
|
|
|
mut expected := os.join_path_single(tdir1, 'file')
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.exists(expected)
|
2021-05-02 21:46:12 +03:00
|
|
|
assert !os.is_dir(expected)
|
2020-09-04 14:08:47 +03:00
|
|
|
// Move dir with contents to other dir
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mv(tdir1, tdir2) or { panic(err) }
|
2021-11-22 15:40:55 +03:00
|
|
|
expected = os.join_path_single(tdir2, 'dir')
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.exists(expected)
|
2021-05-02 21:46:12 +03:00
|
|
|
assert os.is_dir(expected)
|
2020-09-20 17:28:45 +03:00
|
|
|
expected = os.join_path(tdir2, 'dir', 'file')
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.exists(expected)
|
2021-05-02 21:46:12 +03:00
|
|
|
assert !os.is_dir(expected)
|
2020-09-04 14:08:47 +03:00
|
|
|
// Move dir with contents to other dir (by renaming)
|
2021-11-22 15:40:55 +03:00
|
|
|
os.mv(os.join_path_single(tdir2, 'dir'), tdir3) or { panic(err) }
|
2020-09-04 14:08:47 +03:00
|
|
|
expected = tdir3
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.exists(expected)
|
2021-05-02 21:46:12 +03:00
|
|
|
assert os.is_dir(expected)
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.is_dir_empty(tdir2)
|
2020-09-04 14:08:47 +03:00
|
|
|
// Move file with extension to dir
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mv(tfile2, tdir2) or { panic(err) }
|
2021-11-22 15:40:55 +03:00
|
|
|
expected = os.join_path_single(tdir2, 'file.test')
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.exists(expected)
|
2021-05-02 21:46:12 +03:00
|
|
|
assert !os.is_dir(expected)
|
2020-09-04 14:08:47 +03:00
|
|
|
// Move file to dir (by renaming)
|
2021-11-22 15:40:55 +03:00
|
|
|
os.mv(os.join_path_single(tdir2, 'file.test'), tfile3) or { panic(err) }
|
2020-09-04 14:08:47 +03:00
|
|
|
expected = tfile3
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.exists(expected)
|
2021-05-02 21:46:12 +03:00
|
|
|
assert !os.is_dir(expected)
|
2020-09-04 14:08:47 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 02:55:36 +03:00
|
|
|
fn test_cp_all() {
|
2020-03-24 18:18:37 +03:00
|
|
|
// fileX -> dir/fileX
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: clean up of the files happens inside the cleanup_leftovers function
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file('ex1.txt', 'wow!') 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) }
|
2020-03-24 18:18:37 +03:00
|
|
|
assert old == new
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir('ex/ex2') or { panic(err) }
|
|
|
|
os.write_file('ex2.txt', 'great!') 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) }
|
2020-03-24 18:18:37 +03:00
|
|
|
assert old2 == new2
|
|
|
|
// recurring on dir -> local dir
|
2021-03-01 02:18:14 +03:00
|
|
|
os.cp_all('ex', './', true) or { panic(err) }
|
2021-02-12 02:55:36 +03:00
|
|
|
// regression test for executive runs with overwrite := true
|
2021-03-01 02:18:14 +03:00
|
|
|
os.cp_all('ex', './', true) or { panic(err) }
|
2021-03-12 17:26:55 +03:00
|
|
|
os.cp_all('ex', 'nonexisting', true) or { panic(err) }
|
2021-11-22 15:40:55 +03:00
|
|
|
assert os.exists(os.join_path_single('nonexisting', 'ex1.txt'))
|
2019-11-06 23:05:35 +03:00
|
|
|
}
|
|
|
|
|
2021-06-15 11:17:16 +03:00
|
|
|
fn test_realpath_of_empty_string_works() {
|
2021-02-19 12:20:06 +03:00
|
|
|
assert os.real_path('') == ''
|
|
|
|
}
|
|
|
|
|
2021-06-15 11:17:16 +03:00
|
|
|
fn test_realpath_non_existing() {
|
|
|
|
non_existing_path := 'sdyfuisd_non_existing_file'
|
2021-06-15 13:34:28 +03:00
|
|
|
rpath := os.real_path(non_existing_path)
|
|
|
|
$if windows {
|
|
|
|
// on windows, the workdir is prepended, so the result is absolute:
|
|
|
|
assert rpath.len > non_existing_path.len
|
|
|
|
}
|
|
|
|
$if !windows {
|
|
|
|
// on unix, the workdir is NOT prepended for now, so the result remains the same.
|
|
|
|
// TODO: the windows behaviour seems saner, think about normalising the unix case to do the same.
|
|
|
|
assert os.real_path(non_existing_path) == non_existing_path
|
|
|
|
}
|
2021-06-15 11:17:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_realpath_existing() {
|
|
|
|
existing_file_name := 'existing_file.txt'
|
2021-11-22 15:40:55 +03:00
|
|
|
existing_file := os.join_path_single(os.temp_dir(), existing_file_name)
|
2021-06-15 11:17:16 +03:00
|
|
|
os.rm(existing_file) or {}
|
|
|
|
os.write_file(existing_file, 'abc') or {}
|
|
|
|
assert os.exists(existing_file)
|
2021-06-15 14:13:12 +03:00
|
|
|
rpath := os.real_path(existing_file)
|
|
|
|
assert os.is_abs_path(rpath)
|
|
|
|
assert rpath.ends_with(existing_file_name)
|
2021-06-15 11:17:16 +03:00
|
|
|
os.rm(existing_file) or {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_realpath_removes_dots() {
|
|
|
|
examples_folder := os.join_path(@VEXEROOT, 'vlib', 'v', '..', '..', 'cmd', '.', '..',
|
|
|
|
'examples')
|
|
|
|
real_path_of_examples_folder := os.real_path(examples_folder)
|
|
|
|
assert real_path_of_examples_folder.len < examples_folder.len
|
|
|
|
assert !real_path_of_examples_folder.contains('..')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_realpath_absolutizes_existing_relative_paths() {
|
|
|
|
old_wd := os.getwd()
|
|
|
|
defer {
|
2021-08-28 09:35:39 +03:00
|
|
|
os.chdir(old_wd) or { panic(err) }
|
2021-06-15 11:17:16 +03:00
|
|
|
}
|
2021-08-28 09:35:39 +03:00
|
|
|
os.chdir(@VEXEROOT) or { panic(err) }
|
2021-06-15 11:17:16 +03:00
|
|
|
examples_folder := os.join_path('vlib', 'v', '..', '..', 'cmd', '.', '..', 'examples')
|
|
|
|
real_path_of_examples_folder := os.real_path(examples_folder)
|
|
|
|
assert os.is_abs_path(real_path_of_examples_folder)
|
|
|
|
}
|
|
|
|
|
2021-06-15 13:34:28 +03:00
|
|
|
// TODO: think much more about whether this is desirable:
|
2021-06-15 11:17:16 +03:00
|
|
|
fn test_realpath_does_not_absolutize_non_existing_relative_paths() {
|
|
|
|
relative_path := os.join_path('one', 'nonexisting_folder', '..', 'something')
|
2021-06-15 13:34:28 +03:00
|
|
|
$if !windows {
|
|
|
|
assert os.real_path(relative_path).contains('..')
|
|
|
|
assert os.real_path(relative_path) == relative_path
|
|
|
|
}
|
2021-06-15 11:17:16 +03:00
|
|
|
}
|
|
|
|
|
2021-07-05 05:05:37 +03:00
|
|
|
fn test_realpath_absolutepath_symlink() ? {
|
2021-06-21 14:14:58 +03:00
|
|
|
file_name := 'tolink_file.txt'
|
|
|
|
symlink_name := 'symlink.txt'
|
2021-10-24 18:38:44 +03:00
|
|
|
create_file(file_name) ?
|
2021-07-05 05:05:37 +03:00
|
|
|
assert os.symlink(file_name, symlink_name) ?
|
2021-06-21 14:14:58 +03:00
|
|
|
rpath := os.real_path(symlink_name)
|
|
|
|
println(rpath)
|
|
|
|
assert os.is_abs_path(rpath)
|
|
|
|
assert rpath.ends_with(file_name)
|
|
|
|
os.rm(symlink_name) or {}
|
|
|
|
os.rm(file_name) or {}
|
|
|
|
}
|
|
|
|
|
2020-03-24 18:18:37 +03:00
|
|
|
fn test_tmpdir() {
|
2020-03-10 17:02:09 +03:00
|
|
|
t := os.temp_dir()
|
2019-11-17 06:45:20 +03:00
|
|
|
assert t.len > 0
|
|
|
|
assert os.is_dir(t)
|
2020-03-08 00:26:26 +03:00
|
|
|
tfile := t + os.path_separator + 'tmpfile.txt'
|
2021-03-06 22:04:51 +03:00
|
|
|
os.rm(tfile) or {} // just in case
|
2019-11-17 06:45:20 +03:00
|
|
|
tfile_content := 'this is a temporary file'
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file(tfile, tfile_content) or { panic(err) }
|
|
|
|
tfile_content_read := os.read_file(tfile) or { panic(err) }
|
2019-11-17 06:45:20 +03:00
|
|
|
assert tfile_content_read == tfile_content
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rm(tfile) or { panic(err) }
|
2019-11-17 06:45:20 +03:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:56:54 +03:00
|
|
|
fn test_is_writable_folder() {
|
|
|
|
tmp := os.temp_dir()
|
|
|
|
f := os.is_writable_folder(tmp) or {
|
|
|
|
eprintln('err: $err')
|
2020-04-07 17:36:00 +03:00
|
|
|
false
|
2020-04-05 16:56:54 +03:00
|
|
|
}
|
|
|
|
assert f
|
|
|
|
}
|
|
|
|
|
2019-12-06 12:09:35 +03:00
|
|
|
fn test_make_symlink_check_is_link_and_remove_symlink() {
|
2020-03-24 18:18:37 +03:00
|
|
|
folder := 'tfolder'
|
|
|
|
symlink := 'tsymlink'
|
2021-05-29 23:26:13 +03:00
|
|
|
// windows creates a directory symlink, so delete it with rmdir()
|
|
|
|
$if windows {
|
|
|
|
os.rmdir(symlink) or {}
|
|
|
|
} $else {
|
|
|
|
os.rm(symlink) or {}
|
|
|
|
}
|
|
|
|
os.rmdir(folder) or {}
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir(folder) or { panic(err) }
|
|
|
|
folder_contents := os.ls(folder) or { panic(err) }
|
2020-03-24 18:18:37 +03:00
|
|
|
assert folder_contents.len == 0
|
2021-05-29 23:26:13 +03:00
|
|
|
os.symlink(folder, symlink) or { panic(err) }
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.is_link(symlink)
|
2021-05-29 23:26:13 +03:00
|
|
|
$if windows {
|
|
|
|
os.rmdir(symlink) or { panic(err) }
|
|
|
|
} $else {
|
|
|
|
os.rm(symlink) or { panic(err) }
|
|
|
|
}
|
|
|
|
os.rmdir(folder) or { panic(err) }
|
2020-03-24 18:18:37 +03:00
|
|
|
folder_exists := os.is_dir(folder)
|
|
|
|
assert folder_exists == false
|
|
|
|
symlink_exists := os.is_link(symlink)
|
|
|
|
assert symlink_exists == false
|
2019-12-06 12:09:35 +03:00
|
|
|
}
|
|
|
|
|
2021-10-24 18:38:44 +03:00
|
|
|
fn test_make_symlink_check_is_link_and_remove_symlink_with_file() ? {
|
2021-05-29 23:26:13 +03:00
|
|
|
file := 'tfile'
|
|
|
|
symlink := 'tsymlink'
|
|
|
|
os.rm(symlink) or {}
|
|
|
|
os.rm(file) or {}
|
2021-10-24 18:38:44 +03:00
|
|
|
create_file(file) ?
|
2021-05-29 23:26:13 +03:00
|
|
|
os.symlink(file, symlink) or { panic(err) }
|
|
|
|
assert os.is_link(symlink)
|
|
|
|
os.rm(symlink) or { panic(err) }
|
|
|
|
os.rm(file) or { panic(err) }
|
|
|
|
symlink_exists := os.is_link(symlink)
|
|
|
|
assert symlink_exists == false
|
|
|
|
}
|
|
|
|
|
2021-10-24 18:38:44 +03:00
|
|
|
fn test_make_hardlink_check_is_link_and_remove_hardlink_with_file() ? {
|
2021-05-29 23:26:13 +03:00
|
|
|
file := 'tfile'
|
|
|
|
symlink := 'tsymlink'
|
|
|
|
os.rm(symlink) or {}
|
|
|
|
os.rm(file) or {}
|
2021-10-24 18:38:44 +03:00
|
|
|
create_file(file) ?
|
2021-05-29 23:26:13 +03:00
|
|
|
os.link(file, symlink) or { panic(err) }
|
|
|
|
assert os.exists(symlink)
|
|
|
|
os.rm(symlink) or { panic(err) }
|
|
|
|
os.rm(file) or { panic(err) }
|
|
|
|
symlink_exists := os.is_link(symlink)
|
|
|
|
assert symlink_exists == false
|
|
|
|
}
|
|
|
|
|
2020-03-24 18:18:37 +03: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 13:28:10 +03:00
|
|
|
fn test_symlink() {
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir('symlink') or { panic(err) }
|
|
|
|
os.symlink('symlink', 'symlink2') or { panic(err) }
|
2020-03-24 18:18:37 +03:00
|
|
|
assert os.exists('symlink2')
|
|
|
|
// cleanup
|
2021-05-29 23:26:13 +03:00
|
|
|
os.rmdir('symlink') or { panic(err) }
|
|
|
|
$if windows {
|
|
|
|
os.rmdir('symlink2') or { panic(err) }
|
|
|
|
} $else {
|
|
|
|
os.rm('symlink2') or { panic(err) }
|
|
|
|
}
|
2019-12-31 13:28:10 +03:00
|
|
|
}
|
|
|
|
|
2021-10-24 18:38:44 +03:00
|
|
|
fn test_is_executable_writable_readable() ? {
|
2020-03-26 02:09:30 +03:00
|
|
|
file_name := 'rwxfile.exe'
|
2021-10-24 18:38:44 +03:00
|
|
|
create_file(file_name) ?
|
2020-03-24 18:18:37 +03:00
|
|
|
$if !windows {
|
2021-08-28 10:23:01 +03:00
|
|
|
os.chmod(file_name, 0o600) or {} // mark as readable && writable, but NOT executable
|
2020-03-24 18:18:37 +03:00
|
|
|
assert os.is_writable(file_name)
|
|
|
|
assert os.is_readable(file_name)
|
|
|
|
assert !os.is_executable(file_name)
|
2021-08-28 10:23:01 +03:00
|
|
|
os.chmod(file_name, 0o700) or {} // mark as executable too
|
2020-03-24 18:18:37 +03: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.
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rm(file_name) or { panic(err) }
|
2020-01-06 11:15:06 +03:00
|
|
|
}
|
|
|
|
|
2020-03-08 00:26:26 +03:00
|
|
|
fn test_ext() {
|
2020-03-26 16:18:08 +03:00
|
|
|
assert os.file_ext('file.v') == '.v'
|
|
|
|
assert os.file_ext('file') == ''
|
2020-03-08 00:26:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_is_abs() {
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.is_abs_path('/home/user')
|
2020-03-10 18:09:37 +03:00
|
|
|
assert os.is_abs_path('v/vlib') == false
|
2020-03-08 00:26:26 +03:00
|
|
|
$if windows {
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.is_abs_path('C:\\Windows\\')
|
2020-03-08 00:26:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_join() {
|
|
|
|
$if windows {
|
2020-09-20 17:28:45 +03:00
|
|
|
assert os.join_path('v', 'vlib', 'os') == 'v\\vlib\\os'
|
2020-03-08 00:26:26 +03:00
|
|
|
} $else {
|
2020-09-20 17:28:45 +03:00
|
|
|
assert os.join_path('v', 'vlib', 'os') == 'v/vlib/os'
|
2020-03-08 00:26:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 09:43:38 +03:00
|
|
|
fn test_rmdir_all() {
|
|
|
|
mut dirs := ['some/dir', 'some/.hidden/directory']
|
|
|
|
$if windows {
|
|
|
|
for mut d in dirs {
|
|
|
|
d = d.replace('/', '\\')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for d in dirs {
|
|
|
|
os.mkdir_all(d) or { panic(err) }
|
|
|
|
assert os.is_dir(d)
|
|
|
|
}
|
|
|
|
os.rmdir_all('some') or { assert false }
|
|
|
|
assert !os.exists('some')
|
|
|
|
}
|
|
|
|
|
2020-03-08 00:26:26 +03:00
|
|
|
fn test_dir() {
|
|
|
|
$if windows {
|
|
|
|
assert os.dir('C:\\a\\b\\c') == 'C:\\a\\b'
|
2020-12-12 12:59:43 +03:00
|
|
|
assert os.dir('C:\\a\\b\\') == 'C:\\a\\b'
|
2021-07-31 13:52:54 +03:00
|
|
|
assert os.dir('C:/a/b/c') == 'C:\\a\\b'
|
|
|
|
assert os.dir('C:/a/b/') == 'C:\\a\\b'
|
2020-03-08 00:26:26 +03:00
|
|
|
} $else {
|
2021-07-30 03:14:09 +03:00
|
|
|
assert os.dir('/') == '/'
|
|
|
|
assert os.dir('/abc') == '/'
|
2020-03-08 00:26:26 +03:00
|
|
|
assert os.dir('/var/tmp/foo') == '/var/tmp'
|
2020-12-12 12:59:43 +03:00
|
|
|
assert os.dir('/var/tmp/') == '/var/tmp'
|
2021-07-31 13:52:54 +03:00
|
|
|
assert os.dir('C:\\a\\b\\c') == 'C:/a/b'
|
|
|
|
assert os.dir('C:\\a\\b\\') == 'C:/a/b'
|
2020-03-08 00:26:26 +03:00
|
|
|
}
|
|
|
|
assert os.dir('os') == '.'
|
|
|
|
}
|
|
|
|
|
2020-10-01 02:25:52 +03:00
|
|
|
fn test_base() {
|
2020-03-08 00:26:26 +03:00
|
|
|
$if windows {
|
2020-10-01 02:25:52 +03:00
|
|
|
assert os.base('v\\vlib\\os') == 'os'
|
|
|
|
assert os.base('v\\vlib\\os\\') == 'os'
|
2021-07-31 13:52:54 +03:00
|
|
|
assert os.base('v/vlib/os') == 'os'
|
|
|
|
assert os.base('v/vlib/os/') == 'os'
|
2020-03-08 00:26:26 +03:00
|
|
|
} $else {
|
2020-10-01 02:25:52 +03:00
|
|
|
assert os.base('v/vlib/os') == 'os'
|
|
|
|
assert os.base('v/vlib/os/') == 'os'
|
2021-07-31 13:52:54 +03:00
|
|
|
assert os.base('v\\vlib\\os') == 'os'
|
|
|
|
assert os.base('v\\vlib\\os\\') == 'os'
|
2020-03-08 00:26:26 +03:00
|
|
|
}
|
2020-10-01 02:25:52 +03:00
|
|
|
assert os.base('filename') == 'filename'
|
2020-03-08 00:26:26 +03:00
|
|
|
}
|
2020-06-14 16:46:30 +03:00
|
|
|
|
2021-02-15 09:37:35 +03:00
|
|
|
fn test_file_name() {
|
|
|
|
$if windows {
|
|
|
|
assert os.file_name('v\\vlib\\os\\os.v') == 'os.v'
|
|
|
|
assert os.file_name('v\\vlib\\os\\') == ''
|
|
|
|
assert os.file_name('v\\vlib\\os') == 'os'
|
|
|
|
} $else {
|
|
|
|
assert os.file_name('v/vlib/os/os.v') == 'os.v'
|
|
|
|
assert os.file_name('v/vlib/os/') == ''
|
|
|
|
assert os.file_name('v/vlib/os') == 'os'
|
|
|
|
}
|
|
|
|
assert os.file_name('filename') == 'filename'
|
|
|
|
}
|
|
|
|
|
2020-06-14 16:46:30 +03: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 10:45:10 +03: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 14:08:43 +03:00
|
|
|
mut arr := []byte{len: maxn}
|
|
|
|
for i in 0 .. maxn {
|
2020-07-13 10:45:10 +03:00
|
|
|
arr[i] = 65 + byte(i)
|
|
|
|
}
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file_array(fpath, arr) or { panic(err) }
|
|
|
|
rarr := os.read_bytes(fpath) or { panic(err) }
|
2020-07-13 10:45:10 +03:00
|
|
|
assert arr == rarr
|
2020-09-20 17:28:45 +03:00
|
|
|
// eprintln(arr.str())
|
|
|
|
// eprintln(rarr.str())
|
2020-07-13 10:45:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_write_file_array_structs() {
|
|
|
|
fpath := './astructs.bin'
|
2021-01-25 14:08:43 +03:00
|
|
|
mut arr := []IntPoint{len: maxn}
|
|
|
|
for i in 0 .. maxn {
|
2020-07-13 10:45:10 +03:00
|
|
|
arr[i] = IntPoint{65 + i, 65 + i + 10}
|
|
|
|
}
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file_array(fpath, arr) or { panic(err) }
|
2020-07-13 10:45:10 +03:00
|
|
|
rarr := os.read_file_array<IntPoint>(fpath)
|
|
|
|
assert rarr == arr
|
2021-01-25 14:08:43 +03:00
|
|
|
assert rarr.len == maxn
|
2020-09-20 17:28:45 +03:00
|
|
|
// eprintln( rarr.str().replace('\n', ' ').replace('},', '},\n'))
|
2020-07-13 10:45:10 +03:00
|
|
|
}
|
2020-11-05 10:44:34 +03:00
|
|
|
|
|
|
|
fn test_stdout_capture() {
|
|
|
|
/*
|
2020-12-16 12:02:36 +03:00
|
|
|
mut cmd := os.Command{
|
2020-11-05 10:44:34 +03:00
|
|
|
path:'cat'
|
|
|
|
redirect_stdout: true
|
|
|
|
}
|
|
|
|
cmd.start()
|
|
|
|
for !cmd.eof {
|
|
|
|
line := cmd.read_line()
|
|
|
|
println('line="$line"')
|
|
|
|
}
|
|
|
|
cmd.close()
|
2020-12-16 12:02:36 +03:00
|
|
|
*/
|
2020-11-05 10:44:34 +03:00
|
|
|
}
|
2021-01-04 20:57:17 +03:00
|
|
|
|
2021-10-24 18:38:44 +03:00
|
|
|
fn test_posix_set_bit() ? {
|
2021-01-04 20:57:17 +03:00
|
|
|
$if windows {
|
|
|
|
assert true
|
|
|
|
} $else {
|
2021-10-24 18:38:44 +03:00
|
|
|
fpath := 'permtest'
|
|
|
|
create_file(fpath) ?
|
2021-08-28 10:23:01 +03:00
|
|
|
os.chmod(fpath, 0o0777) or { panic(err) }
|
2021-04-04 17:43:32 +03:00
|
|
|
c_fpath := &char(fpath.str)
|
2021-01-04 20:57:17 +03:00
|
|
|
mut s := C.stat{}
|
|
|
|
unsafe {
|
|
|
|
C.stat(c_fpath, &s)
|
|
|
|
}
|
|
|
|
// Take the permissions part of the mode
|
2021-06-07 13:32:55 +03:00
|
|
|
mut mode := u32(s.st_mode) & 0o0777
|
|
|
|
assert mode == 0o0777
|
2021-01-04 20:57:17 +03:00
|
|
|
// `chmod u-r`
|
2021-05-02 21:46:12 +03:00
|
|
|
os.posix_set_permission_bit(fpath, os.s_irusr, false)
|
2021-01-04 20:57:17 +03:00
|
|
|
unsafe {
|
|
|
|
C.stat(c_fpath, &s)
|
|
|
|
}
|
2021-06-07 13:32:55 +03:00
|
|
|
mode = u32(s.st_mode) & 0o0777
|
|
|
|
assert mode == 0o0377
|
2021-01-04 20:57:17 +03:00
|
|
|
// `chmod u+r`
|
2021-05-02 21:46:12 +03:00
|
|
|
os.posix_set_permission_bit(fpath, os.s_irusr, true)
|
2021-01-04 20:57:17 +03:00
|
|
|
unsafe {
|
|
|
|
C.stat(c_fpath, &s)
|
|
|
|
}
|
2021-06-07 13:32:55 +03:00
|
|
|
mode = u32(s.st_mode) & 0o0777
|
|
|
|
assert mode == 0o0777
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: setting the sticky bit is platform dependend
|
2021-01-04 20:57:17 +03:00
|
|
|
// `chmod -s -g -t`
|
2021-05-02 21:46:12 +03:00
|
|
|
os.posix_set_permission_bit(fpath, os.s_isuid, false)
|
|
|
|
os.posix_set_permission_bit(fpath, os.s_isgid, false)
|
|
|
|
os.posix_set_permission_bit(fpath, os.s_isvtx, false)
|
2021-01-04 20:57:17 +03:00
|
|
|
unsafe {
|
|
|
|
C.stat(c_fpath, &s)
|
|
|
|
}
|
2021-06-07 13:32:55 +03:00
|
|
|
mode = u32(s.st_mode) & 0o0777
|
2021-01-04 20:57:17 +03:00
|
|
|
assert mode == 0o0777
|
|
|
|
// `chmod g-w o-w`
|
2021-05-02 21:46:12 +03:00
|
|
|
os.posix_set_permission_bit(fpath, os.s_iwgrp, false)
|
|
|
|
os.posix_set_permission_bit(fpath, os.s_iwoth, false)
|
2021-01-04 20:57:17 +03:00
|
|
|
unsafe {
|
|
|
|
C.stat(c_fpath, &s)
|
|
|
|
}
|
|
|
|
mode = u32(s.st_mode) & 0o7777
|
|
|
|
assert mode == 0o0755
|
2021-05-02 21:46:12 +03:00
|
|
|
os.rm(fpath) or {}
|
2021-01-04 20:57:17 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-12 18:05:26 +03:00
|
|
|
|
|
|
|
fn test_exists_in_system_path() {
|
|
|
|
assert os.exists_in_system_path('') == false
|
|
|
|
$if windows {
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.exists_in_system_path('cmd.exe')
|
2021-03-12 18:05:26 +03:00
|
|
|
return
|
|
|
|
}
|
2021-03-18 20:45:04 +03:00
|
|
|
assert os.exists_in_system_path('ls')
|
2021-03-12 18:05:26 +03:00
|
|
|
}
|
2021-04-16 11:53:44 +03:00
|
|
|
|
2021-10-24 18:38:44 +03:00
|
|
|
fn test_truncate() ? {
|
2021-04-16 11:53:44 +03:00
|
|
|
filename := './test_trunc.txt'
|
|
|
|
hello := 'hello world!'
|
2021-10-24 18:38:44 +03:00
|
|
|
mut f := os.create(filename) ?
|
|
|
|
f.write_string(hello) ?
|
2021-04-16 11:53:44 +03:00
|
|
|
f.close()
|
|
|
|
assert hello.len == os.file_size(filename)
|
|
|
|
newlen := u64(40000)
|
|
|
|
os.truncate(filename, newlen) or { panic(err) }
|
|
|
|
assert newlen == os.file_size(filename)
|
|
|
|
os.rm(filename) or { panic(err) }
|
|
|
|
}
|
2021-04-24 14:08:37 +03:00
|
|
|
|
|
|
|
fn test_hostname() {
|
|
|
|
assert os.hostname().len > 2
|
|
|
|
}
|
2021-06-30 08:30:18 +03:00
|
|
|
|
2021-10-24 18:38:44 +03:00
|
|
|
fn test_glob() ? {
|
2021-06-30 08:30:18 +03:00
|
|
|
os.mkdir('test_dir') or { panic(err) }
|
|
|
|
for i in 0 .. 4 {
|
|
|
|
if i == 3 {
|
2021-10-24 18:38:44 +03:00
|
|
|
create_file('test_dir/test0_another') ?
|
|
|
|
create_file('test_dir/test') ?
|
2021-06-30 08:30:18 +03:00
|
|
|
} else {
|
2021-10-24 18:38:44 +03:00
|
|
|
create_file('test_dir/test' + i.str()) ?
|
2021-06-30 08:30:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
files := os.glob('test_dir/t*') or { panic(err) }
|
|
|
|
assert files.len == 5
|
|
|
|
assert os.base(files[0]) == 'test'
|
|
|
|
|
|
|
|
for i in 0 .. 3 {
|
|
|
|
os.rm('test_dir/test' + i.str()) or { panic(err) }
|
|
|
|
}
|
|
|
|
os.rm('test_dir/test0_another') or { panic(err) }
|
|
|
|
os.rm('test_dir/test') or { panic(err) }
|
|
|
|
os.rmdir_all('test_dir') or { panic(err) }
|
|
|
|
}
|
2021-06-30 10:21:23 +03:00
|
|
|
|
|
|
|
fn test_utime() {
|
|
|
|
filename := './test_utime.txt'
|
|
|
|
hello := 'hello world!'
|
|
|
|
mut f := os.create(filename) or { panic(err) }
|
|
|
|
defer {
|
|
|
|
f.close()
|
|
|
|
os.rm(filename) or { panic(err) }
|
|
|
|
}
|
|
|
|
f.write_string(hello) or { panic(err) }
|
|
|
|
atime := time.now().add_days(2).unix_time()
|
|
|
|
mtime := time.now().add_days(4).unix_time()
|
2021-08-04 13:12:02 +03:00
|
|
|
os.utime(filename, int(atime), int(mtime)) or { panic(err) }
|
2021-06-30 10:21:23 +03:00
|
|
|
assert os.file_last_mod_unix(filename) == mtime
|
|
|
|
}
|
2021-09-01 16:32:28 +03:00
|
|
|
|
|
|
|
fn test_expand_tilde_to_home() {
|
|
|
|
home_test := os.join_path(os.home_dir(), 'test', 'tilde', 'expansion')
|
|
|
|
home_expansion_test := os.expand_tilde_to_home(os.join_path('~', 'test', 'tilde',
|
|
|
|
'expansion'))
|
|
|
|
assert home_test == home_expansion_test
|
|
|
|
assert os.expand_tilde_to_home('~') == os.home_dir()
|
|
|
|
}
|
2021-11-12 10:39:15 +03:00
|
|
|
|
2021-11-12 12:24:37 +03:00
|
|
|
fn test_execute() ? {
|
2021-11-22 15:40:55 +03:00
|
|
|
print0script := os.join_path_single(tfolder, 'print0.v')
|
2021-11-12 10:39:15 +03:00
|
|
|
// The output of the next command contains a 0 byte in the middle.
|
|
|
|
// Nevertheless, the execute function *should* return a string that
|
2021-11-12 12:24:37 +03:00
|
|
|
// contains it.
|
|
|
|
os.write_file(print0script, 'C.printf(c"start%cMIDDLE%cfinish\nxx", 0, 0)\n') ?
|
|
|
|
defer {
|
|
|
|
os.rm(print0script) or {}
|
|
|
|
}
|
2022-01-22 22:13:16 +03:00
|
|
|
result := os.execute('${os.quoted_path(@VEXE)} run ${os.quoted_path(print0script)}')
|
2021-11-12 10:39:15 +03:00
|
|
|
hexresult := result.output.bytes().hex()
|
2021-11-12 12:24:37 +03:00
|
|
|
// println('exit_code: $result.exit_code')
|
|
|
|
// println('output: |$result.output|')
|
|
|
|
// println('output.len: $result.output.len')
|
|
|
|
// println('output hexresult: $hexresult')
|
2021-11-12 10:39:15 +03:00
|
|
|
assert result.exit_code == 0
|
2021-11-12 12:44:26 +03:00
|
|
|
assert hexresult.starts_with('7374617274004d4944444c450066696e697368')
|
|
|
|
assert hexresult.ends_with('0a7878')
|
2021-11-12 10:39:15 +03:00
|
|
|
}
|
2022-01-30 01:44:52 +03:00
|
|
|
|
|
|
|
fn test_command() {
|
|
|
|
if os.user_os() == 'windows' {
|
|
|
|
eprintln('>>> os.Command is not implemented fully on Windows yet')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut cmd := os.Command{
|
|
|
|
path: 'ls'
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.start() or { panic(err) }
|
|
|
|
for !cmd.eof {
|
|
|
|
cmd.read_line()
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.close() or { panic(err) }
|
|
|
|
// dump( cmd )
|
|
|
|
assert cmd.exit_code == 0
|
|
|
|
|
|
|
|
// This will return a non 0 code
|
|
|
|
mut cmd_to_fail := os.Command{
|
|
|
|
path: 'ls -M'
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd_to_fail.start() or { panic(err) }
|
|
|
|
for !cmd_to_fail.eof {
|
|
|
|
cmd_to_fail.read_line()
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd_to_fail.close() or { panic(err) }
|
|
|
|
// dump( cmd_to_fail )
|
|
|
|
assert cmd_to_fail.exit_code != 0 // 2 on linux, 1 on macos
|
|
|
|
}
|