Skip to content

Commit ff586b4

Browse files
committed
cp: Duplicate source error message specify the type of file.
1 parent 4d5bf42 commit ff586b4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/uu/cp/src/cp.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,15 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
12661266
for source in sources {
12671267
let normalized_source = normalize_path(source);
12681268
if options.backup == BackupMode::NoBackup && seen_sources.contains(&normalized_source) {
1269-
show_warning!("source file {} specified more than once", source.quote());
1269+
let file_type = if source.symlink_metadata()?.file_type().is_dir() {
1270+
"directory"
1271+
} else {
1272+
"file"
1273+
};
1274+
show_warning!(
1275+
"source {file_type} {} specified more than once",
1276+
source.quote()
1277+
);
12701278
} else {
12711279
let dest = construct_dest_path(source, target, target_type, options)
12721280
.unwrap_or_else(|_| target.to_path_buf());

tests/by-util/test_cp.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,19 @@ fn test_cp_duplicate_files() {
120120
));
121121
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n");
122122
}
123-
123+
#[test]
124+
fn test_cp_duplicate_folder() {
125+
let (at, mut ucmd) = at_and_ucmd!();
126+
ucmd.arg("-r")
127+
.arg(TEST_COPY_FROM_FOLDER)
128+
.arg(TEST_COPY_FROM_FOLDER)
129+
.arg(TEST_COPY_TO_FOLDER)
130+
.succeeds()
131+
.stderr_contains(format!(
132+
"source directory '{TEST_COPY_FROM_FOLDER}' specified more than once"
133+
));
134+
assert!(at.dir_exists(format!("{TEST_COPY_TO_FOLDER}/{TEST_COPY_FROM_FOLDER}").as_str()));
135+
}
124136
#[test]
125137
fn test_cp_duplicate_files_normalized_path() {
126138
let (at, mut ucmd) = at_and_ucmd!();

0 commit comments

Comments
 (0)