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: check if --target is a directory #4794

Merged
merged 4 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion src/uu/mv/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum MvError {
DirectoryToNonDirectory(String),
NonDirectoryToDirectory(String, String),
NotADirectory(String),
TargetNotADirectory(String),
}

impl Error for MvError {}
Expand All @@ -34,7 +35,8 @@ impl Display for MvError {
Self::NonDirectoryToDirectory(s, t) => {
write!(f, "cannot overwrite non-directory {t} with directory {s}")
}
Self::NotADirectory(t) => write!(f, "target {t} is not a directory"),
Self::NotADirectory(t) => write!(f, "target {t}: Not a directory"),
Self::TargetNotADirectory(t) => write!(f, "target directory {t}: Not a directory"),
}
}
}
7 changes: 6 additions & 1 deletion src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,12 @@ fn exec(files: &[OsString], b: &Behavior) -> UResult<()> {

fn move_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> UResult<()> {
if !target_dir.is_dir() {
return Err(MvError::NotADirectory(target_dir.quote().to_string()).into());
match b.target_dir {
Some(_) => {
return Err(MvError::TargetNotADirectory(target_dir.quote().to_string()).into())
Comment on lines +323 to +325
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a hack ;-) When reading this snippet, I translate it to "If it's a target directory, return a 'Target is not a directory' error".

I think the TargetNotADirectory error should be returned from where the cli args are processed.

}
None => return Err(MvError::NotADirectory(target_dir.quote().to_string()).into()),
}
}

let canonized_target_dir = target_dir
Expand Down
57 changes: 57 additions & 0 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,63 @@ fn test_mv_move_file_into_dir() {
assert!(at.file_exists(format!("{dir}/{file}")));
}

#[test]
fn test_mv_move_file_into_dir_with_target_arg() {
let (at, mut ucmd) = at_and_ucmd!();
let dir = "test_mv_move_file_into_dir_with_target_arg_dir";
let file = "test_mv_move_file_into_dir_with_target_arg_file";

at.mkdir(dir);
at.touch(file);

ucmd.arg("--target")
.arg(dir)
.arg(file)
.succeeds()
.no_stderr();

assert!(at.file_exists(format!("{dir}/{file}")))
}

#[test]
fn test_mv_move_file_into_file_with_target_arg() {
let (at, mut ucmd) = at_and_ucmd!();
let file1 = "test_mv_move_file_into_file_with_target_arg_file1";
let file2 = "test_mv_move_file_into_file_with_target_arg_file2";

at.touch(file1);
at.touch(file2);

ucmd.arg("--target")
.arg(file1)
.arg(file2)
.fails()
.stderr_is(format!("mv: target directory '{file1}': Not a directory\n"));

assert!(at.file_exists(file1))
}

#[test]
fn test_mv_move_multiple_files_into_file() {
let (at, mut ucmd) = at_and_ucmd!();
let file1 = "test_mv_move_multiple_files_into_file1";
let file2 = "test_mv_move_multiple_files_into_file2";
let file3 = "test_mv_move_multiple_files_into_file3";

at.touch(file1);
at.touch(file2);
at.touch(file3);

ucmd.arg(file1)
.arg(file2)
.arg(file3)
.fails()
.stderr_is(format!("mv: target '{file3}': Not a directory\n"));

assert!(at.file_exists(file1));
assert!(at.file_exists(file2));
}

#[test]
fn test_mv_move_file_between_dirs() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down
3 changes: 3 additions & 0 deletions util/build-gnu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ sed -i -Ez "s/\n([^\n#]*pad-3\.2[^\n]*)\n([^\n]*)\n([^\n]*)/\n# uutils\/numfmt s

# Update the GNU error message to match the one generated by clap
sed -i -e "s/\$prog: multiple field specifications/error: The argument '--field <FIELDS>' was provided more than once, but cannot be used multiple times\n\nUsage: numfmt [OPTION]... [NUMBER]...\n\n\nFor more information try '--help'/g" tests/misc/numfmt.pl
sed -i -e "s/Try 'mv --help' for more information/For more information, try '--help'/g" tests/mv/diag.sh
sed -i -e "s/mv: missing file operand/error: the following required arguments were not provided:\n <files>...\n\nUsage: mv [OPTION]... [-T] SOURCE DEST\n mv [OPTION]... SOURCE... DIRECTORY\n mv [OPTION]... -t DIRECTORY SOURCE...\n/g" tests/mv/diag.sh
sed -i -e "s/mv: missing destination file operand after 'no-file'/error: The argument '<files>...' requires at least 2 values, but only 1 was provided\n\nUsage: mv [OPTION]... [-T] SOURCE DEST\n mv [OPTION]... SOURCE... DIRECTORY\n mv [OPTION]... -t DIRECTORY SOURCE...\n/g" tests/mv/diag.sh
sylvestre marked this conversation as resolved.
Show resolved Hide resolved

# GNU doesn't support width > INT_MAX
# disable these test cases
Expand Down