Merge pull request #3419 from sylvestre/install-strip

install: When install --strip-program=foor fails, remove the target file
This commit is contained in:
Sylvestre Ledru 2022-04-18 19:29:26 +02:00 committed by GitHub
commit ee50f408bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View file

@ -594,13 +594,19 @@ fn copy(from: &Path, to: &Path, b: &Behavior) -> UResult<()> {
match process::Command::new(&b.strip_program).arg(to).output() {
Ok(o) => {
if !o.status.success() {
// Follow GNU's behavior: if strip fails, removes the target
let _ = fs::remove_file(to);
return Err(InstallError::StripProgramFailed(
String::from_utf8(o.stderr).unwrap_or_default(),
)
.into());
}
}
Err(e) => return Err(InstallError::StripProgramFailed(e.to_string()).into()),
Err(e) => {
// Follow GNU's behavior: if strip fails, removes the target
let _ = fs::remove_file(to);
return Err(InstallError::StripProgramFailed(e.to_string()).into());
}
}
}

View file

@ -607,7 +607,11 @@ fn test_install_and_strip_with_program() {
#[test]
#[cfg(not(windows))]
fn test_install_and_strip_with_invalid_program() {
new_ucmd!()
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
scene
.ucmd()
.arg("-s")
.arg("--strip-program")
.arg("/bin/date")
@ -615,12 +619,17 @@ fn test_install_and_strip_with_invalid_program() {
.arg(STRIP_TARGET_FILE)
.fails()
.stderr_contains("strip program failed");
assert!(!at.file_exists(STRIP_TARGET_FILE));
}
#[test]
#[cfg(not(windows))]
fn test_install_and_strip_with_non_existent_program() {
new_ucmd!()
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
scene
.ucmd()
.arg("-s")
.arg("--strip-program")
.arg("/usr/bin/non_existent_program")
@ -628,6 +637,7 @@ fn test_install_and_strip_with_non_existent_program() {
.arg(STRIP_TARGET_FILE)
.fails()
.stderr_contains("No such file or directory");
assert!(!at.file_exists(STRIP_TARGET_FILE));
}
#[test]