Skip to content

Commit 85bba6a

Browse files
committed
tests/misc/coreutils.sh: Fail with invalid binary name
1 parent 6f955da commit 85bba6a

File tree

3 files changed

+18
-32
lines changed

3 files changed

+18
-32
lines changed

src/bin/coreutils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ fn main() {
6060

6161
let util_name = if let Some(&util) = matched_util {
6262
Some(OsString::from(util))
63-
} else {
63+
} else if binary_as_util.ends_with("utils") || binary_as_util.ends_with("box") {
64+
// todo: Remove support of "*box" from binary
6465
uucore::set_utility_is_second_arg();
6566
args.next()
67+
} else {
68+
validation::not_found(&OsString::from(binary_as_util));
6669
};
6770

6871
// 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));
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));
203184
}
204185

205186
#[test]

util/fetch-gnu.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ sed -i -e 's/no-mtab-status.sh/no-mtab-status-masked-proc.sh/' -e 's/nproc-quota
1616
curl -L ${repo}/raw/refs/heads/master/tests/df/no-mtab-status-masked-proc.sh > tests/df/no-mtab-status-masked-proc.sh
1717
curl -L ${repo}/raw/refs/heads/master/tests/nproc/nproc-quota-systemd.sh > tests/nproc/nproc-quota-systemd.sh
1818
curl -L ${repo}/raw/refs/heads/master/tests/stty/bad-speed.sh > tests/stty/bad-speed.sh
19+
# symlink to /bin/false should fail with --help. Freeze commit to avoid regression.
20+
curl -L https://raw.githubusercontent.com/coreutils/coreutils/d5164f3d216917005003877faeb1abe7cae5d765/tests/misc/coreutils.sh > tests/misc/coreutils.sh
1921
# Better support for single binary
2022
curl -L ${repo}/raw/refs/heads/master/tests/env/env.sh > tests/env/env.sh
2123
# Avoid incorrect PASS

0 commit comments

Comments
 (0)