From b7a5fa7fbe58d8d24e1b2a1c2cf1eb94dd2fa318 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 18 Mar 2021 19:45:04 +0200 Subject: [PATCH] os: fix `v -cflags "-Werror" test vlib/os/` --- vlib/os/file.c.v | 5 ++++- vlib/os/os.v | 2 +- vlib/os/os_test.v | 30 ++++++++++++++++++------------ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/vlib/os/file.c.v b/vlib/os/file.c.v index 4ea3af8810..939f76e830 100644 --- a/vlib/os/file.c.v +++ b/vlib/os/file.c.v @@ -315,7 +315,10 @@ pub fn (mut f File) write_str(s string) ? { if !f.is_opened { return error('file is closed') } - f.write(s.bytes()) ? + written := int(C.fwrite(voidptr(s.str), s.len, 1, f.cfile)) + if written == 0 && s.len != 0 { + return error('0 bytes written') + } } // read_struct reads a single struct of type `T` diff --git a/vlib/os/os.v b/vlib/os/os.v index fe087725c6..ddd974e3d8 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -333,7 +333,7 @@ pub fn home_dir() string { // write_file writes `text` data to a file in `path`. pub fn write_file(path string, text string) ? { mut f := create(path) ? - f.write(text.bytes()) ? + f.write_str(text) ? f.close() } diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 6f76ade136..a9e46c3b62 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -228,26 +228,32 @@ fn test_mv() { // Move file with no extension to dir os.mv(tfile1, tdir1) or { panic(err) } mut expected := os.join_path(tdir1, 'file') - assert os.exists(expected) && !is_dir(expected) == true + assert os.exists(expected) + assert !is_dir(expected) // Move dir with contents to other dir os.mv(tdir1, tdir2) or { panic(err) } expected = os.join_path(tdir2, 'dir') - assert os.exists(expected) && is_dir(expected) == true + assert os.exists(expected) + assert is_dir(expected) expected = os.join_path(tdir2, 'dir', 'file') - assert os.exists(expected) && !is_dir(expected) == true + assert os.exists(expected) + assert !is_dir(expected) // Move dir with contents to other dir (by renaming) os.mv(os.join_path(tdir2, 'dir'), tdir3) or { panic(err) } expected = tdir3 - assert os.exists(expected) && is_dir(expected) == true - assert os.is_dir_empty(tdir2) == true + assert os.exists(expected) + assert is_dir(expected) + assert os.is_dir_empty(tdir2) // Move file with extension to dir os.mv(tfile2, tdir2) or { panic(err) } expected = os.join_path(tdir2, 'file.test') - assert os.exists(expected) && !is_dir(expected) == true + assert os.exists(expected) + assert !is_dir(expected) // Move file to dir (by renaming) os.mv(os.join_path(tdir2, 'file.test'), tfile3) or { panic(err) } expected = tfile3 - assert os.exists(expected) && !is_dir(expected) == true + assert os.exists(expected) + assert !is_dir(expected) } fn test_cp_all() { @@ -313,7 +319,7 @@ fn test_make_symlink_check_is_link_and_remove_symlink() { folder_contents := os.ls(folder) or { panic(err) } assert folder_contents.len == 0 os.system('ln -s $folder $symlink') - assert os.is_link(symlink) == true + assert os.is_link(symlink) os.rm(symlink) or { panic(err) } os.rm(folder) or { panic(err) } folder_exists := os.is_dir(folder) @@ -384,10 +390,10 @@ fn test_ext() { } fn test_is_abs() { - assert os.is_abs_path('/home/user') == true + assert os.is_abs_path('/home/user') assert os.is_abs_path('v/vlib') == false $if windows { - assert os.is_abs_path('C:\\Windows\\') == true + assert os.is_abs_path('C:\\Windows\\') } } @@ -562,8 +568,8 @@ fn test_posix_set_bit() { fn test_exists_in_system_path() { assert os.exists_in_system_path('') == false $if windows { - assert os.exists_in_system_path('cmd.exe') == true + assert os.exists_in_system_path('cmd.exe') return } - assert os.exists_in_system_path('ls') == true + assert os.exists_in_system_path('ls') }