make cp can copy folders contains dangling symbolic link (#5645)

* cp with no dangling link

* add -p to not follow symbolic link

* change comment

* add one more test case to check symblink body after copied

* better help message
This commit is contained in:
WindSoilder
2022-05-26 23:42:52 +08:00
committed by GitHub
parent 50146bdef3
commit 219c719e98
3 changed files with 162 additions and 20 deletions

View File

@ -1,3 +1,4 @@
use nu_test_support::fs::file_contents;
use nu_test_support::fs::{files_exist_at, AbsoluteFile, Stub::EmptyFile};
use nu_test_support::nu;
use nu_test_support::playground::Playground;
@ -249,3 +250,71 @@ fn copy_to_non_existing_dir() {
assert!(actual.err.contains("destination directory does not exist"));
});
}
#[test]
fn copy_dir_contains_symlink_ignored() {
Playground::setup("cp_test_12", |_dirs, sandbox| {
sandbox
.within("tmp_dir")
.with_files(vec![EmptyFile("hello_there"), EmptyFile("good_bye")])
.within("tmp_dir")
.symlink("good_bye", "dangle_symlink");
// make symbolic link and copy.
nu!(
cwd: sandbox.cwd(),
"rm tmp_dir/good_bye; cp -r tmp_dir tmp_dir_2",
);
// check hello_there exists inside `tmp_dir_2`, and `dangle_symlink` don't exists inside `tmp_dir_2`.
let expected = sandbox.cwd().join("tmp_dir_2");
assert!(files_exist_at(vec!["hello_there"], expected.clone()));
let path = expected.join("dangle_symlink");
assert!(!path.exists() && !path.is_symlink());
});
}
#[test]
fn copy_dir_contains_symlink() {
Playground::setup("cp_test_13", |_dirs, sandbox| {
sandbox
.within("tmp_dir")
.with_files(vec![EmptyFile("hello_there"), EmptyFile("good_bye")])
.within("tmp_dir")
.symlink("good_bye", "dangle_symlink");
// make symbolic link and copy.
nu!(
cwd: sandbox.cwd(),
"rm tmp_dir/good_bye; cp -r -p tmp_dir tmp_dir_2",
);
// check hello_there exists inside `tmp_dir_2`, and `dangle_symlink` also exists inside `tmp_dir_2`.
let expected = sandbox.cwd().join("tmp_dir_2");
assert!(files_exist_at(vec!["hello_there"], expected.clone()));
let path = expected.join("dangle_symlink");
assert!(path.is_symlink());
});
}
#[test]
fn copy_dir_symlink_file_body_not_changed() {
Playground::setup("cp_test_14", |_dirs, sandbox| {
sandbox
.within("tmp_dir")
.with_files(vec![EmptyFile("hello_there"), EmptyFile("good_bye")])
.within("tmp_dir")
.symlink("good_bye", "dangle_symlink");
// make symbolic link and copy.
nu!(
cwd: sandbox.cwd(),
"rm tmp_dir/good_bye; cp -r -p tmp_dir tmp_dir_2; rm -r tmp_dir; cp -r -p tmp_dir_2 tmp_dir; echo hello_data | save tmp_dir/good_bye",
);
// check dangle_symlink in tmp_dir is no longer dangling.
let expected_file = sandbox.cwd().join("tmp_dir").join("dangle_symlink");
let actual = file_contents(expected_file);
assert!(actual.contains("hello_data"));
});
}