os: fix os.cp_all, by making the dst folder, if it does not exist already

pull/9276/head
Delyan Angelov 2021-03-12 16:26:55 +02:00
parent e229d0c1c3
commit 995db65471
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 7 additions and 2 deletions

View File

@ -48,6 +48,9 @@ pub fn cp_all(src string, dst string, overwrite bool) ? {
cp(source_path, adjusted_path) ? cp(source_path, adjusted_path) ?
return return
} }
if !exists(dest_path) {
mkdir(dest_path) ?
}
if !is_dir(dest_path) { if !is_dir(dest_path) {
return error('Destination path is not a valid directory') return error('Destination path is not a valid directory')
} }

View File

@ -55,7 +55,7 @@ fn test_open_file_binary() {
} }
mut file := os.open_file(filename, 'wb+', 0o666) or { panic(err) } mut file := os.open_file(filename, 'wb+', 0o666) or { panic(err) }
bytes := hello.bytes() bytes := hello.bytes()
file.write_bytes(bytes.data, bytes.len) unsafe { file.write_bytes(bytes.data, bytes.len) }
file.close() file.close()
assert hello.len == os.file_size(filename) assert hello.len == os.file_size(filename)
read_hello := os.read_bytes(filename) or { panic('error reading file $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 // 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). // 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() file_write.close()
assert payload.len == os.file_size(file_name) assert payload.len == os.file_size(file_name)
mut file_read := os.open(os.real_path(file_name)) or { 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) } os.cp_all('ex', './', true) or { panic(err) }
// regression test for executive runs with overwrite := true // regression test for executive runs with overwrite := true
os.cp_all('ex', './', true) or { panic(err) } 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() { fn test_realpath() {