Skip to content

Commit 71d7d8f

Browse files
committed
coreutils: support --coreutils-prog utility selection
Support --coreutils-prog=UTIL when invoking the multicall binary, including under an unrecognized name or through a shebang wrapper. Help and diagnostics use the selected utility name. Add coverage for argument forwarding, exit status, invalid utilities, aliases, and shebang execution. All 20 multicall tests pass. Closes #9575.
1 parent aa74c61 commit 71d7d8f

3 files changed

Lines changed: 91 additions & 4 deletions

File tree

‎src/bin/coreutils.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Currently defined functions:
5151
#[allow(clippy::cognitive_complexity)]
5252
fn main() {
5353
let utils = util_map();
54-
let mut args = uucore::args_os();
54+
let mut args = uucore::args_os().peekable();
5555

5656
let binary = validation::binary_path(&mut args);
5757
let binary_as_util = validation::name(&binary).unwrap_or_else(|| {
@@ -68,6 +68,15 @@ fn main() {
6868

6969
let util_name = if let Some(&util) = matched_util {
7070
Some(OsString::from(util))
71+
} else if let Some(util) = args
72+
.peek()
73+
.and_then(|arg| arg.to_str())
74+
.and_then(|arg| arg.strip_prefix("--coreutils-prog="))
75+
{
76+
let util = OsString::from(util);
77+
uucore::set_utility_is_second_arg();
78+
args.next();
79+
Some(util)
7180
} else if is_coreutils || binary_as_util.ends_with("box") {
7281
// todo: Remove support of "*box" from binary
7382
uucore::set_utility_is_second_arg();

‎src/uucore/src/lib/lib.rs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,17 @@ static UTIL_NAME: LazyLock<String> = LazyLock::new(|| {
390390
// Strip directory path to show only utility name
391391
// (e.g., "mkdir" instead of "./target/debug/mkdir")
392392
// in version output, error messages, and other user-facing output
393-
std::path::Path::new(&ARGV[argv_index])
393+
let name = std::path::Path::new(&ARGV[argv_index])
394394
.file_name()
395395
.unwrap_or(&ARGV[argv_index])
396-
.to_string_lossy()
397-
.into_owned()
396+
.to_string_lossy();
397+
if get_utility_is_second_arg() {
398+
name.strip_prefix("--coreutils-prog=")
399+
.unwrap_or(&name)
400+
.to_owned()
401+
} else {
402+
name.into_owned()
403+
}
398404
});
399405

400406
/// Derive the utility name.

‎tests/test_util_name.rs‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,75 @@ fn test_sorted_utils() {
341341
.unwrap();
342342
assert!(out.lines().filter(|s| !s.is_empty()).is_sorted());
343343
}
344+
345+
#[test]
346+
#[cfg(all(feature = "echo", feature = "true", feature = "false"))]
347+
fn coreutils_prog_option() {
348+
use std::process::Command;
349+
let scene = TestScenario::new("dispatch");
350+
let output = Command::new(&scene.bin_path)
351+
.args(["--coreutils-prog=echo", "hello", "world"])
352+
.output()
353+
.unwrap();
354+
assert!(output.status.success());
355+
assert_eq!(output.stdout, b"hello world\n");
356+
assert!(output.stderr.is_empty());
357+
for (name, expected) in [("true", 0), ("false", 1), ("missing_program", 1), ("", 1)] {
358+
let output = Command::new(&scene.bin_path)
359+
.arg(format!("--coreutils-prog={name}"))
360+
.output()
361+
.unwrap();
362+
assert_eq!(output.status.code(), Some(expected), "{name}: {output:?}");
363+
}
364+
}
365+
366+
#[test]
367+
#[cfg(all(unix, feature = "true", feature = "echo"))]
368+
fn coreutils_prog_option_under_unknown_name() {
369+
use std::process::Command;
370+
let scene = TestScenario::new("dispatch");
371+
let alias = scene.fixtures.plus("invalid_name");
372+
symlink_file(&scene.bin_path, &alias).unwrap();
373+
let output = Command::new(&alias)
374+
.arg("--coreutils-prog=true")
375+
.output()
376+
.unwrap();
377+
assert!(output.status.success(), "{output:?}");
378+
assert!(output.stdout.is_empty());
379+
assert!(output.stderr.is_empty());
380+
381+
let output = Command::new(&alias)
382+
.args(["--coreutils-prog=echo", "--help"])
383+
.output()
384+
.unwrap();
385+
assert!(output.status.success());
386+
assert!(
387+
String::from_utf8(output.stdout)
388+
.unwrap()
389+
.contains("Usage: echo")
390+
);
391+
}
392+
393+
#[test]
394+
#[cfg(all(unix, feature = "echo"))]
395+
fn coreutils_prog_shebang() {
396+
use std::os::unix::fs::PermissionsExt;
397+
use std::process::Command;
398+
// Exercise the kernel's shebang argument forwarding, including the script
399+
// filename, rather than only passing --coreutils-prog directly.
400+
let scene = TestScenario::new("dispatch");
401+
let script = scene.fixtures.plus("wrapper");
402+
std::fs::write(
403+
&script,
404+
format!("#!{} --coreutils-prog=echo\n", scene.bin_path.display()),
405+
)
406+
.unwrap();
407+
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
408+
let output = Command::new(&script).arg("hello").output().unwrap();
409+
assert!(output.status.success(), "{output:?}");
410+
assert_eq!(
411+
output.stdout,
412+
format!("{} hello\n", script.display()).as_bytes()
413+
);
414+
assert!(output.stderr.is_empty());
415+
}

0 commit comments

Comments
 (0)