1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

install: improve error message (#7794)

* friendly message install file to directory containing directory with same name

* install: test install file to directory containing directory with same name
This commit is contained in:
Zhang Wen 2025-04-21 00:45:15 +08:00 committed by GitHub
parent f92ee6a519
commit b009cae5a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View file

@ -98,6 +98,9 @@ enum InstallError {
#[error("failed to access {}: Not a directory", .0.quote())]
NotADirectory(PathBuf),
#[error("cannot overwrite directory {} with non-directory {}", .0.quote(), .1.quote())]
OverrideDirectoryFailed(PathBuf, PathBuf),
}
impl UError for InstallError {
@ -748,6 +751,13 @@ fn copy_normal_file(from: &Path, to: &Path) -> UResult<()> {
/// Returns an empty Result or an error in case of failure.
///
fn copy_file(from: &Path, to: &Path) -> UResult<()> {
if to.is_dir() && !from.is_dir() {
return Err(InstallError::OverrideDirectoryFailed(
to.to_path_buf().clone(),
from.to_path_buf().clone(),
)
.into());
}
// fs::copy fails if destination is a invalid symlink.
// so lets just remove all existing files at destination before copy.
if let Err(e) = fs::remove_file(to) {

View file

@ -1764,3 +1764,17 @@ fn test_install_from_stdin() {
assert!(at.file_exists(target));
assert_eq!(at.read(target), test_string);
}
#[test]
fn test_install_failing_copy_file_to_target_contain_subdir_with_same_name() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "file";
let dir1 = "dir1";
at.touch(file);
at.mkdir_all(&format!("{dir1}/{file}"));
ucmd.arg(file)
.arg(dir1)
.fails()
.stderr_contains("cannot overwrite directory");
}