Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mv: if more than one of -i, -f, -n is specified, only the final one takes effect #4826

Merged
merged 3 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/uu/mv/mv.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Move `SOURCE` to `DEST`, or multiple `SOURCE`(s) to `DIRECTORY`.

## After Help

When specifying more than one of -i, -f, -n, only the final one will take effect.

Do not move a non-directory that has an existing destination with the same or newer modification timestamp;
instead, silently skip the file without failing. If the move is across file system boundaries, the comparison is
to the source timestamp truncated to the resolutions of the destination file system and of the system calls used
Expand Down
10 changes: 8 additions & 2 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static ARG_FILES: &str = "files";

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let mut app = uu_app().after_help(backup_control::BACKUP_CONTROL_LONG_HELP);
let mut app = uu_app();
let matches = app.try_get_matches_from_mut(args)?;

if !matches.contains_id(OPT_TARGET_DIRECTORY)
Expand Down Expand Up @@ -138,27 +138,33 @@ pub fn uu_app() -> Command {
.version(crate_version!())
.about(ABOUT)
.override_usage(format_usage(USAGE))
.after_help(AFTER_HELP)
.after_help(format!(
"{AFTER_HELP}\n\n{}",
backup_control::BACKUP_CONTROL_LONG_HELP
))
.infer_long_args(true)
.arg(
Arg::new(OPT_FORCE)
.short('f')
.long(OPT_FORCE)
.help("do not prompt before overwriting")
.overrides_with_all([OPT_INTERACTIVE, OPT_NO_CLOBBER])
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(OPT_INTERACTIVE)
.short('i')
.long(OPT_INTERACTIVE)
.help("prompt before override")
.overrides_with_all([OPT_FORCE, OPT_NO_CLOBBER])
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(OPT_NO_CLOBBER)
.short('n')
.long(OPT_NO_CLOBBER)
.help("do not overwrite an existing file")
.overrides_with_all([OPT_FORCE, OPT_INTERACTIVE])
.action(ArgAction::SetTrue),
)
.arg(
Expand Down
35 changes: 35 additions & 0 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,41 @@ fn test_mv_interactive_dir_to_file_not_affirmative() {
assert!(at.dir_exists(dir));
}

#[test]
fn test_mv_interactive_no_clobber_force_last_arg_wins() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

let file_a = "a.txt";
let file_b = "b.txt";

at.touch(file_a);
at.touch(file_b);

scene
.ucmd()
.args(&[file_a, file_b, "-f", "-i", "-n"])
.fails()
.stderr_is(format!("mv: not replacing '{file_b}'\n"));

scene
.ucmd()
.args(&[file_a, file_b, "-n", "-f", "-i"])
.fails()
.stderr_is(format!("mv: overwrite '{file_b}'? "));

at.write(file_a, "aa");

scene
.ucmd()
.args(&[file_a, file_b, "-i", "-n", "-f"])
.succeeds()
.no_output();

assert!(!at.file_exists(file_a));
assert_eq!("aa", at.read(file_b));
}

#[test]
fn test_mv_arg_update_interactive() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down