1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: change optional to result of io (#16075)

This commit is contained in:
yuyi
2022-10-16 14:28:57 +08:00
committed by GitHub
parent 6e46933c55
commit f6844e9766
187 changed files with 1885 additions and 1874 deletions

View File

@ -49,7 +49,7 @@ fn test_open_file() {
fn test_read_file_from_virtual_filesystem() {
$if linux {
mounts := os.read_file('/proc/mounts')?
mounts := os.read_file('/proc/mounts')!
// it is not empty, contains some mounting such as root filesystem: /dev/x / ext4 rw 0 0
assert mounts.len > 20
@ -60,7 +60,7 @@ fn test_read_file_from_virtual_filesystem() {
fn test_read_binary_from_virtual_filesystem() {
$if linux {
mounts_raw := os.read_bytes('/proc/mounts')?
mounts_raw := os.read_bytes('/proc/mounts')!
mounts := mounts_raw.bytestr()
// it is not empty, contains some mounting such as root filesystem: /dev/x / ext4 rw 0 0
@ -108,21 +108,21 @@ fn test_open_file_binary() {
// assert line2 == 'line 2'
// }
fn create_file(fpath string) ? {
mut f := os.create(fpath)?
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)?
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() {
filename := './test1.txt'
hello := 'hello world!'
create_and_write_to_file(filename, hello)?
create_and_write_to_file(filename, hello)!
assert u64(hello.len) == os.file_size(filename)
os.rm(filename) or { panic(err) }
}
@ -194,7 +194,7 @@ fn test_write_and_read_bytes() {
// check that trying to read data from EOF doesn't error and returns 0
mut a := []u8{len: 5}
nread := file_read.read_bytes_into(5, mut a) or {
n := if err is none {
n := if err == IError(os.Eof{}) {
int(0)
} else {
eprintln(err)
@ -221,22 +221,22 @@ fn test_ls() {
}
}
fn create_tree() ? {
os.mkdir_all('myfolder/f1/f2/f3')?
os.mkdir_all('myfolder/a1/a2/a3', mode: 0o700)?
fn create_tree() ! {
os.mkdir_all('myfolder/f1/f2/f3')!
os.mkdir_all('myfolder/a1/a2/a3', mode: 0o700)!
f3 := os.real_path('myfolder/f1/f2/f3')
assert os.is_dir(f3)
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')?
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')!
}
fn remove_tree() {
@ -250,7 +250,7 @@ fn normalise_paths(paths []string) []string {
}
fn test_walk_ext() {
create_tree()?
create_tree()!
defer {
remove_tree()
}
@ -277,8 +277,8 @@ fn test_walk_ext() {
assert mds == ['myfolder/another.md', 'myfolder/f1/f2/f3/d.md']
}
fn test_walk_with_context() ? {
create_tree()?
fn test_walk_with_context() {
create_tree()!
defer {
remove_tree()
}
@ -468,8 +468,8 @@ fn test_realpath_does_not_absolutize_non_existing_relative_paths() {
fn test_realpath_absolutepath_symlink() {
file_name := 'tolink_file.txt'
symlink_name := 'symlink.txt'
create_file(file_name)?
assert os.symlink(file_name, symlink_name)?
create_file(file_name)!
assert os.symlink(file_name, symlink_name)!
rpath := os.real_path(symlink_name)
println(rpath)
assert os.is_abs_path(rpath)
@ -510,7 +510,7 @@ fn test_make_symlink_check_is_link_and_remove_symlink_with_file() {
symlink := 'tsymlink'
os.rm(symlink) or {}
os.rm(file) or {}
create_file(file)?
create_file(file)!
os.symlink(file, symlink) or { panic(err) }
assert os.is_link(symlink)
os.rm(symlink) or { panic(err) }
@ -524,7 +524,7 @@ fn test_make_hardlink_check_is_link_and_remove_hardlink_with_file() {
symlink := 'tsymlink'
os.rm(symlink) or {}
os.rm(file) or {}
create_file(file)?
create_file(file)!
os.link(file, symlink) or { panic(err) }
assert os.exists(symlink)
os.rm(symlink) or { panic(err) }
@ -569,7 +569,7 @@ fn test_symlink() {
fn test_is_executable_writable_readable() {
file_name := 'rwxfile.exe'
create_file(file_name)?
create_file(file_name)!
$if !windows {
os.chmod(file_name, 0o600) or {} // mark as readable && writable, but NOT executable
assert os.is_writable(file_name)
@ -732,7 +732,7 @@ fn test_posix_set_bit() {
assert true
} $else {
fpath := 'permtest'
create_file(fpath)?
create_file(fpath)!
os.chmod(fpath, 0o0777) or { panic(err) }
c_fpath := &char(fpath.str)
mut s := C.stat{}
@ -790,8 +790,8 @@ fn test_exists_in_system_path() {
fn test_truncate() {
filename := './test_trunc.txt'
hello := 'hello world!'
mut f := os.create(filename)?
f.write_string(hello)?
mut f := os.create(filename)!
f.write_string(hello)!
f.close()
assert u64(hello.len) == os.file_size(filename)
newlen := u64(40000)
@ -808,10 +808,10 @@ fn test_glob() {
os.mkdir('test_dir') or { panic(err) }
for i in 0 .. 4 {
if i == 3 {
create_file('test_dir/test0_another')?
create_file('test_dir/test')?
create_file('test_dir/test0_another')!
create_file('test_dir/test')!
} else {
create_file('test_dir/test' + i.str())?
create_file('test_dir/test' + i.str())!
}
}
files := os.glob('test_dir/t*') or { panic(err) }
@ -846,7 +846,7 @@ fn test_execute() {
// The output of the next command contains a 0 byte in the middle.
// Nevertheless, the execute function *should* return a string that
// contains it.
os.write_file(print0script, 'C.printf(c"start%cMIDDLE%cfinish\nxx", 0, 0)\n')?
os.write_file(print0script, 'C.printf(c"start%cMIDDLE%cfinish\nxx", 0, 0)\n')!
defer {
os.rm(print0script) or {}
}
@ -904,12 +904,12 @@ fn test_reading_from_proc_cpuinfo() {
assert true
return
}
info := os.read_file('/proc/cpuinfo')?
info := os.read_file('/proc/cpuinfo')!
assert info.len > 0
assert info.contains('processor')
assert info.ends_with('\n\n')
info_bytes := os.read_bytes('/proc/cpuinfo')?
info_bytes := os.read_bytes('/proc/cpuinfo')!
assert info_bytes.len > 0
assert info.len == info_bytes.len
}
@ -918,11 +918,11 @@ fn test_reading_from_empty_file() {
empty_file := os.join_path_single(tfolder, 'empty_file.txt')
os.rm(empty_file) or {}
assert !os.exists(empty_file)
os.write_file(empty_file, '')?
os.write_file(empty_file, '')!
assert os.exists(empty_file)
content := os.read_file(empty_file)?
content := os.read_file(empty_file)!
assert content.len == 0
content_bytes := os.read_bytes(empty_file)?
content_bytes := os.read_bytes(empty_file)!
assert content_bytes.len == 0
os.rm(empty_file)?
os.rm(empty_file)!
}