Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/uucore/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,14 @@ static UTIL_NAME: LazyLock<String> = LazyLock::new(|| {
let is_man = usize::from(ARGV[base_index].eq("manpage"));
let argv_index = base_index + is_man;

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

/// Derive the utility name.
Expand Down
36 changes: 36 additions & 0 deletions tests/by-util/test_mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@ fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
}

#[test]
fn test_version_no_path() {
Comment thread
sylvestre marked this conversation as resolved.
use std::process::Command;
use uutests::get_tests_binary;

// This test verifies that when an individual utility binary is invoked with its full path,
// the version output shows just "mkdir", not the full path like "/path/to/mkdir".
//
// Note: The multicall binary (coreutils) doesn't have this issue because it reads
// the utility name from ARGV[1], not ARGV[0]. This bug only affects individual binaries.

let tests_binary = get_tests_binary!();
let mkdir_binary_path = std::path::Path::new(tests_binary)
.parent()
.unwrap()
.join("mkdir");

// If the individual mkdir binary exists, test it
let output = if mkdir_binary_path.exists() {
// Invoke the individual mkdir binary with its full path
Command::new(&mkdir_binary_path)
.arg("--version")
.output()
.expect("Failed to execute mkdir binary")
} else {
// If only multicall binary exists, test that (it should already pass)
Command::new(tests_binary)
.args(["mkdir", "--version"])
.output()
.expect("Failed to execute mkdir via multicall binary")
};

let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.starts_with("mkdir (uutils coreutils)"));
}

#[test]
fn test_no_arg() {
new_ucmd!()
Expand Down
Loading