os: make behave more like Unix "mv" command (#6300)

pull/6302/head
Larpon 2020-09-04 13:08:47 +02:00 committed by GitHub
parent 333f355e23
commit efa49bfbb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 2 deletions

View File

@ -58,10 +58,14 @@ pub fn file_size(path string) int {
// mv moves files or folders from `src` to `dst`.
pub fn mv(src, dst string) {
mut rdst := dst
if is_dir(rdst) {
rdst = join_path(rdst,file_name(src.trim_right(path_separator)))
}
$if windows {
C._wrename(src.to_wide(), dst.to_wide())
C._wrename(src.to_wide(), rdst.to_wide())
} $else {
C.rename(charptr(src.str), charptr(dst.str))
C.rename(charptr(src.str), charptr(rdst.str))
}
}

View File

@ -156,6 +156,52 @@ fn test_cp() {
os.rm(new_file_name)
}
/*
fn test_mv() {
work_dir := os.join_path(os.temp_dir(),'v','mv_test')
if os.exists(work_dir) {
os.rmdir_all(work_dir)
}
mkdir_all(work_dir)
// Setup test files
tfile1 := os.join_path(work_dir,'file')
tfile2 := os.join_path(work_dir,'file.test')
tfile3 := os.join_path(work_dir,'file.3')
tfile_content := 'temporary file'
os.write_file(tfile1, tfile_content)
os.write_file(tfile2, tfile_content)
// Setup test dirs
tdir1 := os.join_path(work_dir,'dir')
tdir2 := os.join_path(work_dir,'dir2')
tdir3 := os.join_path(work_dir,'dir3')
mkdir(tdir1)
mkdir(tdir2)
// Move file with no extension to dir
os.mv(tfile1,tdir1)
mut expected := os.join_path(tdir1,'file')
assert os.exists(expected) && !is_dir(expected) == true
// Move dir with contents to other dir
os.mv(tdir1,tdir2)
expected = os.join_path(tdir2,'dir')
assert os.exists(expected) && is_dir(expected) == true
expected = os.join_path(tdir2,'dir','file')
assert os.exists(expected) && !is_dir(expected) == true
// Move dir with contents to other dir (by renaming)
os.mv(os.join_path(tdir2,'dir'),tdir3)
expected = tdir3
assert os.exists(expected) && is_dir(expected) == true
assert os.is_dir_empty(tdir2) == true
// Move file with extension to dir
os.mv(tfile2,tdir2)
expected = os.join_path(tdir2,'file.test')
assert os.exists(expected) && !is_dir(expected) == true
// Move file to dir (by renaming)
os.mv(os.join_path(tdir2,'file.test'),tfile3)
expected = tfile3
assert os.exists(expected) && !is_dir(expected) == true
}
*/
fn test_cp_r() {
// fileX -> dir/fileX
// NB: clean up of the files happens inside the cleanup_leftovers function