Skip to content
Open
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
12 changes: 10 additions & 2 deletions src/bin/coreutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ fn matched_utility<T>(name: &str, utils: &UtilityMap<T>) -> Option<&'static str>
if name.ends_with("utils") {
return None;
}
// Prefer stty over tty, and never treat *utils as ls.
// Exact match, delimited prefix (uu-cat, coreutils-cat, x86_64-linux-gnu-cat),
// or a single-letter PROG_PREFIX without a separator (uls, ucat, gcat).
// Rejects false suffix matches such as pwd-symlink -> link.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that pwd-link and pwdlink should be pwd? I don't think so.
There is no way to make every one happy at here. Please close this PR.

utils
.keys()
.filter(|&&util| name.ends_with(util))
.filter(|&&util| {
name.strip_suffix(util).is_some_and(|prefix| {
prefix.is_empty()
|| prefix.ends_with(['-', '_', '.'])
|| matches!(prefix.as_bytes(), [b] if b.is_ascii_alphanumeric())
})
})
.max_by_key(|util| util.len())
.copied()
}
Expand Down
35 changes: 35 additions & 0 deletions tests/test_util_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,38 @@ fn coreutils_prog_diagnostics_use_selected_name() {
assert!(!output.status.success());
assert!(String::from_utf8_lossy(&output.stderr).starts_with("ls:"));
}

#[test]
#[cfg(all(unix, feature = "ls"))]
fn test_undelimited_prog_prefix() {
use std::process::Command;

let scene = TestScenario::new("uls");
let uls = scene.fixtures.plus("uls");
symlink_file(&scene.bin_path, &uls).unwrap();

let output = Command::new(&uls).arg("--help").output().unwrap();
assert!(output.status.success(), "{output:?}");
assert!(
String::from_utf8_lossy(&output.stdout).contains("Usage: ls"),
"{output:?}"
);
}

#[test]
#[cfg(all(unix, feature = "link"))]
fn test_delimited_name_does_not_false_match_link() {
use std::process::Command;

let scene = TestScenario::new("true_symlink");
let alias = scene.fixtures.plus("true-symlink");
symlink_file(&scene.bin_path, &alias).unwrap();

let output = Command::new(&alias).output().unwrap();
assert_eq!(output.status.code(), Some(1), "{output:?}");
assert!(output.stdout.is_empty(), "{output:?}");
assert_eq!(
output.stderr, b"coreutils: unknown program 'true-symlink'\n",
"{output:?}"
);
}
Loading