2020-09-08 15:17:01 +03:00
|
|
|
import os
|
|
|
|
import io.util
|
|
|
|
|
|
|
|
const (
|
|
|
|
// 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.
|
2022-11-03 10:24:52 +03:00
|
|
|
tfolder = os.join_path(os.vtmp_dir(), 'v', 'tests', 'io_util_test')
|
2020-09-08 15:17:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
fn testsuite_begin() {
|
|
|
|
eprintln('testsuite_begin, tfolder = $tfolder')
|
2021-05-08 13:32:29 +03:00
|
|
|
os.rmdir_all(tfolder) or {}
|
2020-09-08 15:17:01 +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 10:23:01 +03:00
|
|
|
os.chdir(tfolder) or {}
|
2020-09-08 15:17:01 +03:00
|
|
|
assert os.is_dir(tfolder)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn testsuite_end() {
|
2021-08-28 10:23:01 +03:00
|
|
|
os.chdir(os.wd_at_startup) or {}
|
2021-05-08 13:32:29 +03:00
|
|
|
os.rmdir_all(tfolder) or {}
|
2020-09-08 15:17:01 +03:00
|
|
|
assert !os.is_dir(tfolder)
|
|
|
|
// eprintln('testsuite_end , tfolder = $tfolder removed.')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_temp_file() {
|
|
|
|
// Test defaults
|
2021-07-20 11:17:08 +03:00
|
|
|
mut f, mut path := util.temp_file() or {
|
2020-09-08 15:17:01 +03:00
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut prev_path := path
|
|
|
|
defer {
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
assert os.is_file(path)
|
|
|
|
assert f.is_opened
|
|
|
|
// Test pattern
|
|
|
|
f.close()
|
2021-01-26 17:43:10 +03:00
|
|
|
f, path = util.temp_file(
|
2020-09-08 15:17:01 +03:00
|
|
|
pattern: 'some_*_test.file'
|
2021-01-26 17:43:10 +03:00
|
|
|
) or {
|
2020-09-08 15:17:01 +03:00
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert path != prev_path
|
|
|
|
assert os.is_file(path)
|
|
|
|
assert f.is_opened
|
|
|
|
mut filename := os.file_name(path)
|
|
|
|
assert filename.contains('_test.file')
|
|
|
|
// Check for 9 digits where the wildcard is placed in the pattern
|
|
|
|
for i, c in filename {
|
|
|
|
if i > 4 && i <= 4 + 9 {
|
|
|
|
assert c.is_digit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Test custom path
|
|
|
|
prev_path = path
|
|
|
|
f.close()
|
2021-01-26 17:43:10 +03:00
|
|
|
f, path = util.temp_file(
|
2020-09-08 15:17:01 +03:00
|
|
|
path: tfolder
|
2021-01-26 17:43:10 +03:00
|
|
|
) or {
|
2020-09-08 15:17:01 +03:00
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert path != prev_path
|
|
|
|
assert os.is_file(path)
|
|
|
|
assert path.contains(tfolder)
|
|
|
|
assert f.is_opened
|
|
|
|
filename = os.file_name(path)
|
|
|
|
for c in filename {
|
|
|
|
assert c.is_digit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_temp_dir() {
|
|
|
|
// Test defaults
|
2021-07-20 11:17:08 +03:00
|
|
|
mut path := util.temp_dir() or {
|
2020-09-08 15:17:01 +03:00
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert os.is_dir(path)
|
2022-10-20 13:56:06 +03:00
|
|
|
os.ensure_folder_is_writable(path) or {
|
2020-09-08 15:17:01 +03:00
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut prev_path := path
|
|
|
|
// Test pattern
|
2021-01-26 17:43:10 +03:00
|
|
|
path = util.temp_dir(
|
2020-09-08 15:17:01 +03:00
|
|
|
pattern: 'some_*_test_dir'
|
2021-01-26 17:43:10 +03:00
|
|
|
) or {
|
2020-09-08 15:17:01 +03:00
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert path != prev_path
|
|
|
|
assert os.is_dir(path)
|
|
|
|
mut filename := os.file_name(path)
|
|
|
|
assert filename.contains('_test_dir')
|
|
|
|
// Check for 9 digits where the wildcard is placed in the pattern
|
|
|
|
for i, c in filename {
|
|
|
|
if i > 4 && i <= 4 + 9 {
|
|
|
|
assert c.is_digit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Test custom path
|
|
|
|
prev_path = path
|
2021-01-26 17:43:10 +03:00
|
|
|
path = util.temp_dir(
|
2020-09-08 15:17:01 +03:00
|
|
|
path: tfolder
|
2021-01-26 17:43:10 +03:00
|
|
|
) or {
|
2020-09-08 15:17:01 +03:00
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert path != prev_path
|
|
|
|
assert os.is_dir(path)
|
2022-10-20 13:56:06 +03:00
|
|
|
os.ensure_folder_is_writable(path) or {
|
2020-09-08 15:17:01 +03:00
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert path.contains(tfolder)
|
|
|
|
filename = os.file_name(path)
|
|
|
|
for c in filename {
|
|
|
|
assert c.is_digit()
|
|
|
|
}
|
|
|
|
}
|