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: Change error message #5104

Closed
wants to merge 10 commits into from
50 changes: 37 additions & 13 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (ToDO) sourcepath targetpath nushell canonicalized
// spell-checker:ignore (ToDO) sourcepath targetpath nushell canonicalized ENOTEMPTY EEXIST EISDIR ENOSPC EMLINK ETXTBSY

mod error;

Expand All @@ -29,6 +29,7 @@
};
#[cfg(all(unix, not(any(target_os = "macos", target_os = "redox"))))]
use uucore::fsxattr;
use uucore::libc;
use uucore::update_control;

// These are exposed for projects (e.g. nushell) that want to create an `Options` value, which
Expand Down Expand Up @@ -497,17 +498,38 @@
match rename(sourcepath, &targetpath, options, multi_progress.as_ref()) {
Err(e) if e.to_string().is_empty() => set_exit_code(1),
Err(e) => {
let e = e.map_err_context(|| {
format!(
"cannot move {} to {}",
sourcepath.quote(),
targetpath.quote()
)
});
match multi_progress {
Some(ref pb) => pb.suspend(|| show!(e)),
None => show!(e),
};
match e.raw_os_error() {
Some(
libc::ENOTEMPTY
| libc::EEXIST
| libc::EISDIR
| libc::ENOSPC
| libc::EMLINK
| libc::ETXTBSY,
) => {
// The error message was changed to match GNU's decision
// when an issue was filed. These will match when merged upstream.
let e = e
.map_err_context(|| format!("cannot overwrite {}", targetpath.quote()));
match multi_progress {
Some(ref pb) => pb.suspend(|| show!(e)),

Check warning on line 515 in src/uu/mv/src/mv.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/mv/src/mv.rs#L515

Added line #L515 was not covered by tests
None => show!(e),
};
}
_ => {
let e = e.map_err_context(|| {
format!(
"cannot move {} to {}",
sourcepath.quote(),
targetpath.quote()
)
});
match multi_progress {
Some(ref pb) => pb.suspend(|| show!(e)),

Check warning on line 528 in src/uu/mv/src/mv.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/mv/src/mv.rs#L528

Added line #L528 was not covered by tests
None => show!(e),
};
}
}
}
Ok(()) => (),
}
Expand Down Expand Up @@ -581,7 +603,9 @@
if is_empty_dir(to) {
fs::remove_dir(to)?;
} else {
return Err(io::Error::new(io::ErrorKind::Other, "Directory not empty"));
// TODO: change when 'io::Error::DirectoryNotEmpty' is stabilized.
PThorpe92 marked this conversation as resolved.
Show resolved Hide resolved
// https://github.com/rust-lang/rust/issues/86442
return Err(io::Error::from_raw_os_error(libc::ENOTEMPTY));
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,35 @@ fn test_mv_overwrite_nonempty_dir() {
assert!(at.dir_exists(dir_b));
}

#[test]
fn test_mv_nonempty_directory_exists() {
let (at, mut ucmd) = at_and_ucmd!();
let test_dir = "test_dir";
let dir_a = "test_mv_nonempty_directory_exists";
let dir_b = "test_dir/test_mv_nonempty_directory_exists";
let dummy = "test_mv_nonempty_directory_exists/file";
let dummy2 = "test_dir/test_mv_nonempty_directory_exists/file";
at.mkdir(test_dir);
at.mkdir(dir_a);
at.mkdir(dir_b);
at.touch(dummy);
at.touch(dummy2);

// Not same error as GNU; the error message is custom.
// GNU: "mv: Couldn't move (Directory not empty; from=a; to=b)
// Current: "mv: cannot overwrite {}: Directory not empty""

ucmd.arg(dir_a)
.arg(test_dir)
.fails()
.stderr_contains("cannot overwrite");

assert!(at.dir_exists(dir_a));
assert!(at.dir_exists(dir_b));
assert!(at.file_exists(dummy));
assert!(at.file_exists(dummy2));
}

#[test]
fn test_mv_backup_dir() {
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 @@ -230,6 +230,9 @@ sed -i -e "s|rm: cannot remove 'a/b/file'|rm: cannot remove 'a'|g" tests/rm/cycl

sed -i -e "s|rm: cannot remove directory 'b/a/p'|rm: cannot remove 'b'|g" tests/rm/rm1.sh

# TODO: when GNU's output is changed to match this error, this can be removed.
sed -i -e "s|mv: cannot move 'b/t' to 'a/t'|mv: cannot overwrite 'b/t'|g" tests/mv/dir2dir.sh

sed -i -e "s|rm: cannot remove 'a/1'|rm: cannot remove 'a'|g" tests/rm/rm2.sh

sed -i -e "s|removed directory 'a/'|removed directory 'a'|g" tests/rm/v-slash.sh
Expand Down
Loading