2021-06-16 15:16:28 +00:00
|
|
|
use std::fs::File;
|
|
|
|
|
2021-03-29 11:00:47 +00:00
|
|
|
use crate::common::util::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn test_dev_null() {
|
|
|
|
new_ucmd!()
|
2021-06-16 15:16:28 +00:00
|
|
|
.set_stdin(File::open("/dev/null").unwrap())
|
2021-03-29 11:00:47 +00:00
|
|
|
.fails()
|
2021-06-16 15:16:28 +00:00
|
|
|
.code_is(1)
|
2021-03-29 11:00:47 +00:00
|
|
|
.stdout_is("not a tty\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn test_dev_null_silent() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-s"])
|
2021-06-16 15:16:28 +00:00
|
|
|
.set_stdin(File::open("/dev/null").unwrap())
|
2021-03-29 11:00:47 +00:00
|
|
|
.fails()
|
2021-06-16 15:16:28 +00:00
|
|
|
.code_is(1)
|
2021-03-29 11:00:47 +00:00
|
|
|
.stdout_is("");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_close_stdin() {
|
2021-06-16 15:16:28 +00:00
|
|
|
let mut child = new_ucmd!().run_no_wait();
|
|
|
|
drop(child.stdin.take());
|
|
|
|
let output = child.wait_with_output().unwrap();
|
|
|
|
assert_eq!(output.status.code(), Some(1));
|
|
|
|
assert_eq!(std::str::from_utf8(&output.stdout), Ok("not a tty\n"));
|
2021-03-29 11:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_close_stdin_silent() {
|
2021-06-16 15:16:28 +00:00
|
|
|
let mut child = new_ucmd!().arg("-s").run_no_wait();
|
|
|
|
drop(child.stdin.take());
|
|
|
|
let output = child.wait_with_output().unwrap();
|
|
|
|
assert_eq!(output.status.code(), Some(1));
|
|
|
|
assert!(output.stdout.is_empty());
|
2021-03-29 11:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_close_stdin_silent_long() {
|
2021-06-16 15:16:28 +00:00
|
|
|
let mut child = new_ucmd!().arg("--silent").run_no_wait();
|
|
|
|
drop(child.stdin.take());
|
|
|
|
let output = child.wait_with_output().unwrap();
|
|
|
|
assert_eq!(output.status.code(), Some(1));
|
|
|
|
assert!(output.stdout.is_empty());
|
2021-03-29 11:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_close_stdin_silent_alias() {
|
2021-06-16 15:16:28 +00:00
|
|
|
let mut child = new_ucmd!().arg("--quiet").run_no_wait();
|
|
|
|
drop(child.stdin.take());
|
|
|
|
let output = child.wait_with_output().unwrap();
|
|
|
|
assert_eq!(output.status.code(), Some(1));
|
|
|
|
assert!(output.stdout.is_empty());
|
2021-03-29 11:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wrong_argument() {
|
2021-06-16 15:16:28 +00:00
|
|
|
new_ucmd!().args(&["a"]).fails().code_is(2);
|
2021-03-29 11:00:47 +00:00
|
|
|
}
|
2021-06-16 15:38:07 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-08-24 06:53:20 +00:00
|
|
|
// FixME: freebsd panic
|
2021-08-27 16:10:13 +00:00
|
|
|
#[cfg(all(unix, not(target_os = "freebsd")))]
|
2021-06-16 15:38:07 +00:00
|
|
|
fn test_stdout_fail() {
|
2021-08-27 16:10:13 +00:00
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
let ts = TestScenario::new(util_name!());
|
|
|
|
// Sleep inside a shell to ensure the process doesn't finish before we've
|
|
|
|
// closed its stdout
|
|
|
|
let mut proc = Command::new("sh")
|
|
|
|
.arg("-c")
|
|
|
|
.arg(format!(
|
|
|
|
"sleep 0.2; exec {} {}",
|
|
|
|
ts.bin_path.to_str().unwrap(),
|
|
|
|
ts.util_name
|
|
|
|
))
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.unwrap();
|
|
|
|
drop(proc.stdout.take());
|
|
|
|
let status = proc.wait().unwrap();
|
2021-06-16 15:38:07 +00:00
|
|
|
assert_eq!(status.code(), Some(3));
|
|
|
|
}
|