diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 32168b09009..5829738b049 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1409,7 +1409,13 @@ fn copy_source( } } } - res + // this is just for gnu tests compatibility + Ok(res.map_err(|err| { + if err.to_string().contains("No such file or directory") { + return format!("cannot stat {}: No such file or directory", source.quote()); + } + err.to_string() + })?) } } @@ -2245,19 +2251,10 @@ fn copy_file( let context = context_for(source, dest); let context = context.as_str(); - let source_metadata = { - let result = if options.dereference(source_in_command_line) { - fs::metadata(source) - } else { - fs::symlink_metadata(source) - }; - // this is just for gnu tests compatibility - result.map_err(|err| { - if err.to_string().contains("No such file or directory") { - return format!("cannot stat {}: No such file or directory", source.quote()); - } - err.to_string() - })? + let source_metadata = if options.dereference(source_in_command_line) { + fs::metadata(source)? + } else { + fs::symlink_metadata(source)? }; let dest_permissions = calculate_dest_permissions(dest, &source_metadata, options, context)?; diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 7a0889b0fa6..799d121d850 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -5831,6 +5831,28 @@ fn test_cp_parents_absolute_path() { at.file_exists(res); } +/// Test the behavior of copying a non existent file to a folder +#[test] +#[cfg(unix)] +fn test_cp_existant_and_nonexistent_file() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.mkdir(TEST_COPY_TO_FOLDER_NEW); + + scene + .ucmd() + .arg("-a") + .arg(TEST_NONEXISTENT_FILE) + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_COPY_TO_FOLDER_NEW) + .fails() + .stderr_only("cp: cannot stat 'nonexistent_file.txt': No such file or directory\n"); + + // Check existing file has been copied + assert!(at.file_exists(TEST_COPY_TO_FOLDER_NEW_FILE)); +} + #[test] fn test_copy_symlink_overwrite() { let (at, mut ucmd) = at_and_ucmd!();