Skip to content

Commit

Permalink
cp: error when --backup is used with conflicting option
Browse files Browse the repository at this point in the history
  • Loading branch information
matrixhead committed Aug 23, 2024
1 parent d1dfdf0 commit 5766547
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
10 changes: 7 additions & 3 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,10 +703,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} else if let Ok(mut matches) = matches {
let options = Options::from_matches(&matches)?;

if options.overwrite == OverwriteMode::NoClobber && options.backup != BackupMode::NoBackup {
if options.backup != BackupMode::NoBackup
&& (options.overwrite == OverwriteMode::NoClobber
|| options.update == UpdateMode::ReplaceNone
|| options.update == UpdateMode::ReplaceNoneFail)
{
return Err(UUsageError::new(
EXIT_ERR,
"options --backup and --no-clobber are mutually exclusive",
1,
"cannot combine --backup with -n/--no-clobber or --update=none-fail",
));
}

Expand Down
18 changes: 10 additions & 8 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,14 +1093,16 @@ fn test_cp_backup_off() {
}

#[test]
fn test_cp_backup_no_clobber_conflicting_options() {
new_ucmd!()
.arg("--backup")
.arg("--no-clobber")
.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_HOW_ARE_YOU_SOURCE)
.fails()
.usage_error("options --backup and --no-clobber are mutually exclusive");
fn test_cp_backup_conflicting_options() {
for conflicting_opt in ["--no-clobber", "--update=none-fail", "--update=none"] {
new_ucmd!()
.arg("--backup")
.arg(conflicting_opt)
.arg("file1")
.arg("file2")
.fails()
.usage_error("cannot combine --backup with -n/--no-clobber or --update=none-fail");
}
}

#[test]
Expand Down

0 comments on commit 5766547

Please sign in to comment.