From 995db65471affbcdeebe9121210b371ef3afcf54 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 12 Mar 2021 16:26:55 +0200 Subject: [PATCH] os: fix os.cp_all, by making the dst folder, if it does not exist already --- vlib/os/os.v | 3 +++ vlib/os/os_test.v | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/vlib/os/os.v b/vlib/os/os.v index d7a1e5ac40..ed5009c4f8 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -48,6 +48,9 @@ pub fn cp_all(src string, dst string, overwrite bool) ? { cp(source_path, adjusted_path) ? return } + if !exists(dest_path) { + mkdir(dest_path) ? + } if !is_dir(dest_path) { return error('Destination path is not a valid directory') } diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 8078d84681..cface1ebf7 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -55,7 +55,7 @@ fn test_open_file_binary() { } mut file := os.open_file(filename, 'wb+', 0o666) or { panic(err) } bytes := hello.bytes() - file.write_bytes(bytes.data, bytes.len) + unsafe { file.write_bytes(bytes.data, bytes.len) } file.close() assert hello.len == os.file_size(filename) read_hello := os.read_bytes(filename) or { panic('error reading file $filename') } @@ -144,7 +144,7 @@ fn test_write_and_read_bytes() { } // 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) + unsafe { 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 { @@ -269,6 +269,8 @@ fn test_cp_all() { os.cp_all('ex', './', true) or { panic(err) } // regression test for executive runs with overwrite := true os.cp_all('ex', './', true) or { panic(err) } + os.cp_all('ex', 'nonexisting', true) or { panic(err) } + assert os.exists(os.join_path('nonexisting', 'ex1.txt')) } fn test_realpath() {