Skip to content

Commit 3d6b0b2

Browse files
sylvestrecakebaker
andauthored
--version should just print the command name, not the path (#8921)
* --version should just print the command name, not the path This will fix the parsing for old autoconf Closes: #8880 * Update tests/by-util/test_mkdir.rs Co-authored-by: Daniel Hofstetter <[email protected]> --------- Co-authored-by: Daniel Hofstetter <[email protected]>
1 parent ca8eb4d commit 3d6b0b2

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/uucore/src/lib/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,14 @@ static UTIL_NAME: LazyLock<String> = LazyLock::new(|| {
328328
let is_man = usize::from(ARGV[base_index].eq("manpage"));
329329
let argv_index = base_index + is_man;
330330

331-
ARGV[argv_index].to_string_lossy().into_owned()
331+
// Strip directory path to show only utility name
332+
// (e.g., "mkdir" instead of "./target/debug/mkdir")
333+
// in version output, error messages, and other user-facing output
334+
std::path::Path::new(&ARGV[argv_index])
335+
.file_name()
336+
.unwrap_or(&ARGV[argv_index])
337+
.to_string_lossy()
338+
.into_owned()
332339
});
333340

334341
/// Derive the utility name.

tests/by-util/test_mkdir.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,42 @@ fn test_invalid_arg() {
2424
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
2525
}
2626

27+
#[test]
28+
fn test_version_no_path() {
29+
use std::process::Command;
30+
use uutests::get_tests_binary;
31+
32+
// This test verifies that when an individual utility binary is invoked with its full path,
33+
// the version output shows just "mkdir", not the full path like "/path/to/mkdir".
34+
//
35+
// Note: The multicall binary (coreutils) doesn't have this issue because it reads
36+
// the utility name from ARGV[1], not ARGV[0]. This bug only affects individual binaries.
37+
38+
let tests_binary = get_tests_binary!();
39+
let mkdir_binary_path = std::path::Path::new(tests_binary)
40+
.parent()
41+
.unwrap()
42+
.join("mkdir");
43+
44+
// If the individual mkdir binary exists, test it
45+
let output = if mkdir_binary_path.exists() {
46+
// Invoke the individual mkdir binary with its full path
47+
Command::new(&mkdir_binary_path)
48+
.arg("--version")
49+
.output()
50+
.expect("Failed to execute mkdir binary")
51+
} else {
52+
// If only multicall binary exists, test that (it should already pass)
53+
Command::new(tests_binary)
54+
.args(["mkdir", "--version"])
55+
.output()
56+
.expect("Failed to execute mkdir via multicall binary")
57+
};
58+
59+
let stdout = String::from_utf8_lossy(&output.stdout);
60+
assert!(stdout.starts_with("mkdir (uutils coreutils)"));
61+
}
62+
2763
#[test]
2864
fn test_no_arg() {
2965
new_ucmd!()

0 commit comments

Comments
 (0)