Skip to content

Commit de37baa

Browse files
committed
tests: test multi-call logic
Thankfully, this revealed no bugs. Let's prevent regressions!
1 parent abdeead commit de37baa

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

tests/test_util_name.rs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,154 @@ fn util_name_single() {
9292
scenario.fixtures.plus("uu-sort").display()
9393
)));
9494
}
95+
96+
#[test]
97+
#[cfg(any(unix, windows))]
98+
fn util_invalid_name_help() {
99+
use std::{
100+
io::Write,
101+
process::{Command, Stdio},
102+
};
103+
104+
let scenario = TestScenario::new("invalid_name");
105+
symlink_file(scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
106+
let child = Command::new(scenario.fixtures.plus("invalid_name"))
107+
.arg("--help")
108+
.stdin(Stdio::piped())
109+
.stdout(Stdio::piped())
110+
.stderr(Stdio::piped())
111+
.spawn()
112+
.unwrap();
113+
let output = child.wait_with_output().unwrap();
114+
assert_eq!(output.status.code(), Some(0));
115+
assert_eq!(output.stderr, b"");
116+
let output_str = String::from_utf8(output.stdout).unwrap();
117+
assert!(
118+
output_str.contains("(multi-call binary)"),
119+
"{:?}",
120+
output_str
121+
);
122+
assert!(
123+
output_str.contains("Usage: invalid_name [function "),
124+
"{:?}",
125+
output_str
126+
);
127+
}
128+
129+
#[test]
130+
// The exact set of permitted filenames depends on many factors. Non-UTF-8 strings
131+
// work on very few platforms, but linux works, especially because it also increases
132+
// the likelihood that a filesystem is being used that supports non-UTF-8 filenames.
133+
#[cfg(target_os = "linux")]
134+
fn util_non_utf8_name_help() {
135+
// Make sure we don't crash even if the util name is invalid UTF-8.
136+
use std::{
137+
ffi::OsStr,
138+
io::Write,
139+
os::unix::ffi::OsStrExt,
140+
path::Path,
141+
process::{Command, Stdio},
142+
};
143+
144+
let scenario = TestScenario::new("invalid_name");
145+
let non_utf8_path = scenario.fixtures.plus(OsStr::from_bytes(b"\xff"));
146+
symlink_file(scenario.bin_path, &non_utf8_path).unwrap();
147+
let child = Command::new(&non_utf8_path)
148+
.arg("--help")
149+
.stdin(Stdio::piped())
150+
.stdout(Stdio::piped())
151+
.stderr(Stdio::piped())
152+
.spawn()
153+
.unwrap();
154+
let output = child.wait_with_output().unwrap();
155+
assert_eq!(output.status.code(), Some(0));
156+
assert_eq!(output.stderr, b"");
157+
let output_str = String::from_utf8(output.stdout).unwrap();
158+
assert!(
159+
output_str.contains("(multi-call binary)"),
160+
"{:?}",
161+
output_str
162+
);
163+
assert!(
164+
output_str.contains("Usage: <unknown binary name> [function "),
165+
"{:?}",
166+
output_str
167+
);
168+
}
169+
170+
#[test]
171+
#[cfg(any(unix, windows))]
172+
fn util_invalid_name_invalid_command() {
173+
use std::{
174+
io::Write,
175+
process::{Command, Stdio},
176+
};
177+
178+
let scenario = TestScenario::new("invalid_name");
179+
symlink_file(scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
180+
let child = Command::new(scenario.fixtures.plus("invalid_name"))
181+
.arg("definitely_invalid")
182+
.stdin(Stdio::piped())
183+
.stdout(Stdio::piped())
184+
.stderr(Stdio::piped())
185+
.spawn()
186+
.unwrap();
187+
let output = child.wait_with_output().unwrap();
188+
assert_eq!(output.status.code(), Some(1));
189+
assert_eq!(output.stderr, b"");
190+
assert_eq!(
191+
output.stdout,
192+
b"definitely_invalid: function/utility not found\n"
193+
);
194+
}
195+
196+
#[test]
197+
fn util_completion() {
198+
use std::{
199+
io::Write,
200+
process::{Command, Stdio},
201+
};
202+
203+
let scenario = TestScenario::new("completion");
204+
let child = Command::new(scenario.bin_path)
205+
.arg("completion")
206+
.arg("true")
207+
.arg("powershell")
208+
.stdin(Stdio::piped())
209+
.stdout(Stdio::piped())
210+
.stderr(Stdio::piped())
211+
.spawn()
212+
.unwrap();
213+
let output = child.wait_with_output().unwrap();
214+
assert_eq!(output.status.code(), Some(0));
215+
assert_eq!(output.stderr, b"");
216+
let output_str = String::from_utf8(output.stdout).unwrap();
217+
assert!(
218+
output_str.contains("using namespace System.Management.Automation"),
219+
"{:?}",
220+
output_str
221+
);
222+
}
223+
224+
#[test]
225+
fn util_manpage() {
226+
use std::{
227+
io::Write,
228+
process::{Command, Stdio},
229+
};
230+
231+
let scenario = TestScenario::new("completion");
232+
let child = Command::new(scenario.bin_path)
233+
.arg("manpage")
234+
.arg("true")
235+
.stdin(Stdio::piped())
236+
.stdout(Stdio::piped())
237+
.stderr(Stdio::piped())
238+
.spawn()
239+
.unwrap();
240+
let output = child.wait_with_output().unwrap();
241+
assert_eq!(output.status.code(), Some(0));
242+
assert_eq!(output.stderr, b"");
243+
let output_str = String::from_utf8(output.stdout).unwrap();
244+
assert!(output_str.contains("\n.TH true 1 "), "{:?}", output_str);
245+
}

0 commit comments

Comments
 (0)