Merge pull request #4136 from Joining7943/tail-test-runner

`tests/common/util`: Implement UChild, an abstraction for std::process::Child, to return UChild from run_no_wait instead
This commit is contained in:
Sylvestre Ledru 2022-12-03 10:18:25 +01:00 committed by GitHub
commit 75502aba93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 1441 additions and 531 deletions

View file

@ -1,12 +1,11 @@
// spell-checker:ignore NOFILE
use crate::common::util::*;
use std::fs::OpenOptions;
#[cfg(unix)]
use std::io::Read;
#[cfg(any(target_os = "linux", target_os = "android"))]
use rlimit::Resource;
use std::fs::OpenOptions;
#[cfg(not(windows))]
use std::process::Stdio;
#[test]
fn test_output_simple() {
@ -87,8 +86,7 @@ fn test_fifo_symlink() {
pipe.write_all(&data).unwrap();
});
let output = proc.wait_with_output().unwrap();
assert_eq!(&output.stdout, &data2);
proc.wait().unwrap().stdout_only_bytes(data2);
thread.join().unwrap();
}
@ -395,17 +393,19 @@ fn test_squeeze_blank_before_numbering() {
#[test]
#[cfg(unix)]
fn test_dev_random() {
let mut buf = [0; 2048];
#[cfg(any(target_os = "linux", target_os = "android"))]
const DEV_RANDOM: &str = "/dev/urandom";
#[cfg(not(any(target_os = "linux", target_os = "android")))]
const DEV_RANDOM: &str = "/dev/random";
let mut proc = new_ucmd!().args(&[DEV_RANDOM]).run_no_wait();
let mut proc_stdout = proc.stdout.take().unwrap();
proc_stdout.read_exact(&mut buf).unwrap();
let mut proc = new_ucmd!()
.set_stdout(Stdio::piped())
.args(&[DEV_RANDOM])
.run_no_wait();
proc.make_assertion_with_delay(100).is_alive();
let buf = proc.stdout_exact_bytes(2048);
let num_zeroes = buf.iter().fold(0, |mut acc, &n| {
if n == 0 {
acc += 1;
@ -415,7 +415,7 @@ fn test_dev_random() {
// The probability of more than 512 zero bytes is essentially zero if the
// output is truly random.
assert!(num_zeroes < 512);
proc.kill().unwrap();
proc.kill();
}
/// Reading from /dev/full should return an infinite amount of zero bytes.
@ -423,29 +423,35 @@ fn test_dev_random() {
#[test]
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
fn test_dev_full() {
let mut buf = [0; 2048];
let mut proc = new_ucmd!().args(&["/dev/full"]).run_no_wait();
let mut proc_stdout = proc.stdout.take().unwrap();
let mut proc = new_ucmd!()
.set_stdout(Stdio::piped())
.args(&["/dev/full"])
.run_no_wait();
let expected = [0; 2048];
proc_stdout.read_exact(&mut buf).unwrap();
assert_eq!(&buf[..], &expected[..]);
proc.kill().unwrap();
proc.make_assertion_with_delay(100)
.is_alive()
.with_exact_output(2048, 0)
.stdout_only_bytes(expected);
proc.kill();
}
#[test]
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
fn test_dev_full_show_all() {
let mut buf = [0; 2048];
let mut proc = new_ucmd!().args(&["-A", "/dev/full"]).run_no_wait();
let mut proc_stdout = proc.stdout.take().unwrap();
proc_stdout.read_exact(&mut buf).unwrap();
let expected: Vec<u8> = (0..buf.len())
let buf_len = 2048;
let mut proc = new_ucmd!()
.set_stdout(Stdio::piped())
.args(&["-A", "/dev/full"])
.run_no_wait();
let expected: Vec<u8> = (0..buf_len)
.map(|n| if n & 1 == 0 { b'^' } else { b'@' })
.collect();
assert_eq!(&buf[..], &expected[..]);
proc.kill().unwrap();
proc.make_assertion_with_delay(100)
.is_alive()
.with_exact_output(buf_len, 0)
.stdout_only_bytes(expected);
proc.kill();
}
#[test]
@ -478,9 +484,7 @@ fn test_domain_socket() {
let child = new_ucmd!().args(&[socket_path]).run_no_wait();
barrier.wait();
let stdout = &child.wait_with_output().unwrap().stdout;
let output = String::from_utf8_lossy(stdout);
assert_eq!("a\tb", output);
child.wait().unwrap().stdout_is("a\tb");
thread.join().unwrap();
}

View file

@ -2330,10 +2330,7 @@ fn test_copy_contents_fifo() {
// At this point the child process should have terminated
// successfully with no output. The `outfile` should have the
// contents of `fifo` copied into it.
let output = child.wait_with_output().unwrap();
assert!(output.status.success());
assert!(output.stdout.is_empty());
assert!(output.stderr.is_empty());
child.wait().unwrap().no_stdout().no_stderr().success();
assert_eq!(at.read("outfile"), "foo");
}

View file

@ -1036,11 +1036,12 @@ fn test_random_73k_test_lazy_fullblock() {
sleep(Duration::from_millis(10));
}
}
let output = child.wait_with_output().unwrap();
assert!(output.status.success());
assert_eq!(&output.stdout, &data);
assert_eq!(&output.stderr, b"142+1 records in\n72+1 records out\n");
child
.wait()
.unwrap()
.success()
.stdout_is_bytes(&data)
.stderr_is("142+1 records in\n72+1 records out\n");
}
#[test]
@ -1381,9 +1382,6 @@ fn test_sync_delayed_reader() {
sleep(Duration::from_millis(10));
}
}
let output = child.wait_with_output().unwrap();
assert!(output.status.success());
// Expected output is 0xFFFFFFFF00000000FFFFFFFF00000000...
let mut expected: [u8; 8 * 16] = [0; 8 * 16];
for i in 0..8 {
@ -1391,8 +1389,13 @@ fn test_sync_delayed_reader() {
expected[16 * i + j] = 0xF;
}
}
assert_eq!(&output.stdout, &expected);
assert_eq!(&output.stderr, b"0+8 records in\n4+0 records out\n");
child
.wait()
.unwrap()
.success()
.stdout_is_bytes(expected)
.stderr_is("0+8 records in\n4+0 records out\n");
}
/// Test for making a sparse copy of the input file.

View file

@ -52,7 +52,7 @@ fn test_parallel() {
.open(tmp_dir.plus("output"))
.unwrap();
for mut child in (0..10)
for child in (0..10)
.map(|_| {
new_ucmd!()
.set_stdout(output.try_clone().unwrap())
@ -61,7 +61,7 @@ fn test_parallel() {
})
.collect::<Vec<_>>()
{
assert_eq!(child.wait().unwrap().code().unwrap(), 0);
child.wait().unwrap().success();
}
let result = TestScenario::new(util_name!())

View file

@ -357,8 +357,6 @@ fn test_rm_interactive_never() {
fn test_rm_descend_directory() {
// This test descends into each directory and deletes the files and folders inside of them
// This test will have the rm process asks 6 question and us answering Y to them will delete all the files and folders
use std::io::Write;
use std::process::Child;
// Needed for talking with stdin on platforms where CRLF or LF matters
const END_OF_LINE: &str = if cfg!(windows) { "\r\n" } else { "\n" };
@ -375,24 +373,15 @@ fn test_rm_descend_directory() {
at.touch(file_1);
at.touch(file_2);
let mut child: Child = scene.ucmd().arg("-ri").arg("a").run_no_wait();
let mut child = scene.ucmd().arg("-ri").arg("a").run_no_wait();
child.try_write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
// Needed so that we can talk to the rm program
let mut child_stdin = child.stdin.take().unwrap();
child_stdin.write_all(yes.as_bytes()).unwrap();
child_stdin.flush().unwrap();
child_stdin.write_all(yes.as_bytes()).unwrap();
child_stdin.flush().unwrap();
child_stdin.write_all(yes.as_bytes()).unwrap();
child_stdin.flush().unwrap();
child_stdin.write_all(yes.as_bytes()).unwrap();
child_stdin.flush().unwrap();
child_stdin.write_all(yes.as_bytes()).unwrap();
child_stdin.flush().unwrap();
child_stdin.write_all(yes.as_bytes()).unwrap();
child_stdin.flush().unwrap();
child.wait_with_output().unwrap();
child.wait().unwrap();
assert!(!at.dir_exists("a/b"));
assert!(!at.dir_exists("a"));
@ -404,7 +393,6 @@ fn test_rm_descend_directory() {
#[test]
fn test_rm_prompts() {
use std::io::Write;
use std::process::Child;
// Needed for talking with stdin on platforms where CRLF or LF matters
const END_OF_LINE: &str = if cfg!(windows) { "\r\n" } else { "\n" };
@ -457,21 +445,15 @@ fn test_rm_prompts() {
.arg(file_2)
.succeeds();
let mut child: Child = scene.ucmd().arg("-ri").arg("a").run_no_wait();
let mut child_stdin = child.stdin.take().unwrap();
let mut child = scene.ucmd().arg("-ri").arg("a").run_no_wait();
for _ in 0..9 {
child_stdin.write_all(yes.as_bytes()).unwrap();
child_stdin.flush().unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
}
let output = child.wait_with_output().unwrap();
let result = child.wait().unwrap();
let mut trimmed_output = Vec::new();
for string in String::from_utf8(output.stderr)
.expect("Couldn't convert output.stderr to string")
.split("rm: ")
{
for string in result.stderr_str().split("rm: ") {
if !string.is_empty() {
let trimmed_string = format!("rm: {}", string).trim().to_string();
trimmed_output.push(trimmed_string);
@ -491,9 +473,6 @@ fn test_rm_prompts() {
#[test]
fn test_rm_force_prompts_order() {
use std::io::Write;
use std::process::Child;
// Needed for talking with stdin on platforms where CRLF or LF matters
const END_OF_LINE: &str = if cfg!(windows) { "\r\n" } else { "\n" };
@ -507,15 +486,11 @@ fn test_rm_force_prompts_order() {
at.touch(empty_file);
// This should cause rm to prompt to remove regular empty file
let mut child: Child = scene.ucmd().arg("-fi").arg(empty_file).run_no_wait();
let mut child = scene.ucmd().arg("-fi").arg(empty_file).run_no_wait();
child.try_write_in(yes.as_bytes()).unwrap();
let mut child_stdin = child.stdin.take().unwrap();
child_stdin.write_all(yes.as_bytes()).unwrap();
child_stdin.flush().unwrap();
let output = child.wait_with_output().unwrap();
let string_output =
String::from_utf8(output.stderr).expect("Couldn't convert output.stderr to string");
let result = child.wait().unwrap();
let string_output = result.stderr_str();
assert_eq!(
string_output.trim(),
"rm: remove regular empty file 'empty'?"

View file

@ -1,6 +1,6 @@
// spell-checker:ignore lmnop xlmnop
use crate::common::util::*;
use std::io::Read;
use std::process::Stdio;
#[test]
fn test_invalid_arg() {
@ -595,12 +595,10 @@ fn test_width_floats() {
/// Run `seq`, capture some of the output, close the pipe, and verify it.
fn run(args: &[&str], expected: &[u8]) {
let mut cmd = new_ucmd!();
let mut child = cmd.args(args).run_no_wait();
let mut stdout = child.stdout.take().unwrap();
let mut buf = vec![0; expected.len()];
stdout.read_exact(&mut buf).unwrap();
drop(stdout);
assert!(child.wait().unwrap().success());
let mut child = cmd.args(args).set_stdout(Stdio::piped()).run_no_wait();
let buf = child.stdout_exact_bytes(expected.len());
child.close_stdout();
child.wait().unwrap().success();
assert_eq!(buf.as_slice(), expected);
}

View file

@ -976,11 +976,8 @@ fn test_sigpipe_panic() {
let mut child = cmd.args(&["ext_sort.txt"]).run_no_wait();
// Dropping the stdout should not lead to an error.
// The "Broken pipe" error should be silently ignored.
drop(child.stdout.take());
assert_eq!(
String::from_utf8(child.wait_with_output().unwrap().stderr),
Ok(String::new())
);
child.close_stdout();
child.wait().unwrap().no_stderr();
}
#[test]
@ -1137,7 +1134,7 @@ fn test_tmp_files_deleted_on_sigint() {
"--buffer-size=1", // with a small buffer size `sort` will be forced to create a temporary directory very soon.
"--temporary-directory=tmp_dir",
]);
let mut child = ucmd.run_no_wait();
let child = ucmd.run_no_wait();
// wait a short amount of time so that `sort` can create a temporary directory.
let mut timeout = Duration::from_millis(100);
for _ in 0..5 {
@ -1152,7 +1149,7 @@ fn test_tmp_files_deleted_on_sigint() {
// kill sort with SIGINT
signal::kill(Pid::from_raw(child.id() as i32), signal::SIGINT).unwrap();
// wait for `sort` to exit
assert_eq!(child.wait().unwrap().code(), Some(2));
child.wait().unwrap().code_is(2);
// `sort` should have deleted the temporary directory again.
assert!(read_dir(at.plus("tmp_dir")).unwrap().next().is_none());
}

View file

@ -7,6 +7,8 @@ fn test_invalid_arg() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_stdin_default() {
new_ucmd!()
.pipe_in("100\n200\n300\n400\n500")
@ -15,6 +17,8 @@ fn test_stdin_default() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_stdin_non_newline_separator() {
new_ucmd!()
.args(&["-s", ":"])
@ -24,6 +28,8 @@ fn test_stdin_non_newline_separator() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_stdin_non_newline_separator_before() {
new_ucmd!()
.args(&["-b", "-s", ":"])
@ -76,11 +82,14 @@ fn test_invalid_input() {
}
#[test]
#[cfg(not(windows))] // FIXME: https://github.com/uutils/coreutils/issues/4204
fn test_no_line_separators() {
new_ucmd!().pipe_in("a").succeeds().stdout_is("a");
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_before_trailing_separator_no_leading_separator() {
new_ucmd!()
.arg("-b")
@ -90,6 +99,8 @@ fn test_before_trailing_separator_no_leading_separator() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_before_trailing_separator_and_leading_separator() {
new_ucmd!()
.arg("-b")
@ -99,6 +110,8 @@ fn test_before_trailing_separator_and_leading_separator() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_before_leading_separator_no_trailing_separator() {
new_ucmd!()
.arg("-b")
@ -108,6 +121,8 @@ fn test_before_leading_separator_no_trailing_separator() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_before_no_separator() {
new_ucmd!()
.arg("-b")
@ -117,11 +132,15 @@ fn test_before_no_separator() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_before_empty_file() {
new_ucmd!().arg("-b").pipe_in("").succeeds().stdout_is("");
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_multi_char_separator() {
new_ucmd!()
.args(&["-s", "xx"])
@ -131,6 +150,8 @@ fn test_multi_char_separator() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_multi_char_separator_overlap() {
// The right-most pair of "x" characters in the input is treated as
// the only line separator. That is, "axxx" is interpreted as having
@ -161,6 +182,8 @@ fn test_multi_char_separator_overlap() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_multi_char_separator_overlap_before() {
// With the "-b" option, the line separator is assumed to be at the
// beginning of the line. In this case, That is, "axxx" is
@ -203,6 +226,8 @@ fn test_multi_char_separator_overlap_before() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_null_separator() {
new_ucmd!()
.args(&["-s", ""])
@ -212,6 +237,8 @@ fn test_null_separator() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_regex() {
new_ucmd!()
.args(&["-r", "-s", "[xyz]+"])
@ -240,6 +267,8 @@ fn test_regex() {
}
#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_regex_before() {
new_ucmd!()
.args(&["-b", "-r", "-s", "[xyz]+"])

File diff suppressed because it is too large Load diff

View file

@ -111,9 +111,7 @@ mod linux_only {
use crate::common::util::*;
use std::fs::File;
use std::io::Write;
use std::process::Output;
use std::thread;
fn make_broken_pipe() -> File {
use libc::c_int;
@ -134,21 +132,11 @@ mod linux_only {
fn run_tee(proc: &mut UCommand) -> (String, Output) {
let content = (1..=100000).map(|x| format!("{}\n", x)).collect::<String>();
let mut prog = proc.run_no_wait();
let mut stdin = prog
.stdin
.take()
.unwrap_or_else(|| panic!("Could not take child process stdin"));
let c = content.clone();
let thread = thread::spawn(move || {
let _ = stdin.write_all(c.as_bytes());
});
let output = prog.wait_with_output().unwrap();
thread.join().unwrap();
#[allow(deprecated)]
let output = proc
.ignore_stdin_write_error()
.run_no_wait()
.pipe_in_and_wait_with_output(content.as_bytes());
(content, output)
}

View file

@ -26,37 +26,29 @@ fn test_dev_null_silent() {
#[test]
fn test_close_stdin() {
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"));
child.close_stdin();
child.wait().unwrap().code_is(1).stdout_is("not a tty\n");
}
#[test]
fn test_close_stdin_silent() {
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());
child.close_stdin();
child.wait().unwrap().code_is(1).no_stdout();
}
#[test]
fn test_close_stdin_silent_long() {
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());
child.close_stdin();
child.wait().unwrap().code_is(1).no_stdout();
}
#[test]
fn test_close_stdin_silent_alias() {
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());
child.close_stdin();
child.wait().unwrap().code_is(1).no_stdout();
}
#[test]

View file

@ -1,5 +1,4 @@
use std::io::Read;
use std::process::ExitStatus;
use std::process::{ExitStatus, Stdio};
#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
@ -19,12 +18,12 @@ fn check_termination(result: &ExitStatus) {
/// Run `yes`, capture some of the output, close the pipe, and verify it.
fn run(args: &[&str], expected: &[u8]) {
let mut cmd = new_ucmd!();
let mut child = cmd.args(args).run_no_wait();
let mut stdout = child.stdout.take().unwrap();
let mut buf = vec![0; expected.len()];
stdout.read_exact(&mut buf).unwrap();
drop(stdout);
check_termination(&child.wait().unwrap());
let mut child = cmd.args(args).set_stdout(Stdio::piped()).run_no_wait();
let buf = child.stdout_exact_bytes(expected.len());
child.close_stdout();
#[allow(deprecated)]
check_termination(&child.wait_with_output().unwrap().status);
assert_eq!(buf.as_slice(), expected);
}

File diff suppressed because it is too large Load diff