Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions tests/by-util/test_cat.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
Expand All @@ -415,37 +415,43 @@ 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.
/// Wikipedia says there is support on Linux, FreeBSD, and NetBSD.
#[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]
Expand Down Expand Up @@ -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();
}
Expand Down
5 changes: 1 addition & 4 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Comment thread
tertsdiepraam marked this conversation as resolved.
Outdated
assert_eq!(at.read("outfile"), "foo");
}

Expand Down
23 changes: 13 additions & 10 deletions tests/by-util/test_dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -1381,18 +1382,20 @@ 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 {
for j in 0..8 {
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.
Expand Down
4 changes: 2 additions & 2 deletions tests/by-util/test_factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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!())
Expand Down
59 changes: 17 additions & 42 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand All @@ -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();

// 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();
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();

child.wait().unwrap();

assert!(!at.dir_exists("a/b"));
assert!(!at.dir_exists("a"));
Expand All @@ -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" };
Expand Down Expand Up @@ -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);
Expand All @@ -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" };

Expand All @@ -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_stdin = child.stdin.take().unwrap();
child_stdin.write_all(yes.as_bytes()).unwrap();
child_stdin.flush().unwrap();
let mut child = scene.ucmd().arg("-fi").arg(empty_file).run_no_wait();
child.try_write_in(yes.as_bytes()).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'?"
Expand Down
12 changes: 5 additions & 7 deletions tests/by-util/test_seq.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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);
}

Expand Down
11 changes: 4 additions & 7 deletions tests/by-util/test_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand All @@ -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());
}
Expand Down
Loading