Skip to content

Commit 963f649

Browse files
committed
coreutils: Fail with invalid binary name
1 parent 2c75e71 commit 963f649

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

src/bin/coreutils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ fn main() {
6565
if let Some(util) = validation::find_prefixed_util(binary_as_util, utils.keys().copied()) {
6666
// prefixed util => replace 0th (aka, executable name) argument
6767
Some(OsString::from(util))
68-
} else {
69-
// unmatched binary name => regard as multi-binary container and advance argument list
68+
} else if binary_as_util.ends_with("coreutils") || binary_as_util.ends_with("box") {
7069
uucore::set_utility_is_second_arg();
7170
args.next()
71+
} else {
72+
eprintln!("coreutils: I was probably called as symlink to false");
73+
process::exit(1);
7274
};
7375

7476
// 0th argument equals util name?

tests/test_util_name.rs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -109,30 +109,20 @@ fn util_name_single() {
109109
#[test]
110110
#[cfg(unix)]
111111
fn util_invalid_name_help() {
112-
use std::process::{Command, Stdio};
112+
use std::process::Command;
113113

114114
let scenario = TestScenario::new("invalid_name");
115115
if !scenario.bin_path.exists() {
116116
println!("Skipping test: Binary not found at {:?}", scenario.bin_path);
117117
return;
118118
}
119119
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
120-
let child = Command::new(scenario.fixtures.plus("invalid_name"))
120+
let code = Command::new(scenario.fixtures.plus("invalid_name"))
121121
.arg("--help")
122-
.stdin(Stdio::piped())
123-
.stdout(Stdio::piped())
124-
.stderr(Stdio::piped())
125-
.spawn()
126-
.unwrap();
127-
let output = child.wait_with_output().unwrap();
128-
assert_eq!(output.status.code(), Some(0));
129-
assert_eq!(output.stderr, b"");
130-
let output_str = String::from_utf8(output.stdout).unwrap();
131-
assert!(output_str.contains("(multi-call binary)"), "{output_str:?}");
132-
assert!(
133-
output_str.contains("Usage: invalid_name [function "),
134-
"{output_str:?}"
135-
);
122+
.status()
123+
.unwrap()
124+
.code();
125+
assert_eq!(code, Some(1)); // for GNU compat
136126
}
137127

138128
#[test]
@@ -177,7 +167,7 @@ fn util_non_utf8_name_help() {
177167
#[test]
178168
#[cfg(unix)]
179169
fn util_invalid_name_invalid_command() {
180-
use std::process::{Command, Stdio};
170+
use std::process::Command;
181171

182172
let scenario = TestScenario::new("invalid_name");
183173
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
@@ -186,20 +176,11 @@ fn util_invalid_name_invalid_command() {
186176
return;
187177
}
188178

189-
let child = Command::new(scenario.fixtures.plus("invalid_name"))
190-
.arg("definitely_invalid")
191-
.stdin(Stdio::piped())
192-
.stdout(Stdio::piped())
193-
.stderr(Stdio::piped())
194-
.spawn()
195-
.unwrap();
196-
let output = child.wait_with_output().unwrap();
197-
assert_eq!(output.status.code(), Some(1));
198-
assert_eq!(output.stdout, b"");
199-
assert_eq!(
200-
output.stderr,
201-
b"definitely_invalid: function/utility not found\n"
202-
);
179+
let code = Command::new(scenario.fixtures.plus("invalid_name"))
180+
.status()
181+
.unwrap()
182+
.code();
183+
assert_eq!(code, Some(1)); //for GNU compat
203184
}
204185

205186
#[test]

0 commit comments

Comments
 (0)