From 283269405667da4ec4bd672e310bc702f0212f43 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 23 Dec 2023 15:29:32 +0100 Subject: [PATCH] install: support when a hyphen is passed (#5697) * install: support when a hyphen is passed Should fix: tests/install/strip-program.sh Co-authored-by: Daniel Hofstetter --- src/uu/install/src/install.rs | 16 +++++++++++++++- tests/by-util/test_install.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/uu/install/src/install.rs b/src/uu/install/src/install.rs index 43925a7f8..1154b32c1 100644 --- a/src/uu/install/src/install.rs +++ b/src/uu/install/src/install.rs @@ -749,7 +749,21 @@ fn copy_file(from: &Path, to: &Path) -> UResult<()> { /// Returns an empty Result or an error in case of failure. /// fn strip_file(to: &Path, b: &Behavior) -> UResult<()> { - match process::Command::new(&b.strip_program).arg(to).output() { + // Check if the filename starts with a hyphen and adjust the path + let to = if to + .file_name() + .unwrap_or_default() + .to_str() + .unwrap_or_default() + .starts_with('-') + { + let mut new_path = PathBuf::from("."); + new_path.push(to); + new_path + } else { + to.to_path_buf() + }; + 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 diff --git a/tests/by-util/test_install.rs b/tests/by-util/test_install.rs index 7387748c6..3e61bc92b 100644 --- a/tests/by-util/test_install.rs +++ b/tests/by-util/test_install.rs @@ -674,6 +674,34 @@ fn test_install_and_strip_with_program() { assert!(!stdout.contains(STRIP_SOURCE_FILE_SYMBOL)); } +#[cfg(all(unix, feature = "chmod"))] +#[test] +// FixME: Freebsd fails on 'No such file or directory' +#[cfg(not(target_os = "freebsd"))] +fn test_install_and_strip_with_program_hyphen() { + let scene = TestScenario::new(util_name!()); + + let at = &scene.fixtures; + let content = r#"#!/bin/sh + echo $1 &> /tmp/a.log + printf -- '%s\n' "$1" | grep '^[^-]' + "#; + at.write("no-hyphen", content); + scene.ccmd("chmod").arg("+x").arg("no-hyphen").succeeds(); + + at.touch("src"); + scene + .ucmd() + .arg("-s") + .arg("--strip-program") + .arg("./no-hyphen") + .arg("--") + .arg("src") + .arg("-dest") + .succeeds() + .no_stderr(); +} + #[test] #[cfg(not(windows))] fn test_install_and_strip_with_invalid_program() {