coreutils/tests/by-util/test_yes.rs

116 lines
2.8 KiB
Rust
Raw Normal View History

use std::ffi::OsStr;
tests: Use UChild in tests. Rename run_no_wait_child to run_no_wait and return UChild tests/tail: * test_stdin_redirect_file:. Test fails now when assert_alive()! The follow test `tail -f < file` where file's content is `foo` fails with: Assertion failed. Expected 'tail' to be running but exited with status=exit status: 0 I also tried on the command line and can confirm that tail isn't runnning when following by descriptor. The test is deactivated until the implementation is fixed. * test_follow_stdin_descriptor * test_follow_stdin_explicit_indefinitely. * test_follow_single * test_follow_non_utf8_bytes * test_follow_multiple * test_follow_name_multiple * test_follow_invalid_pid * test_single_big_args * test_retry3 * test_retry4 * test_retry5 * test_retry7 * test_retry8 * test_retry9 * test_follow_descriptor_vs_rename1 * test_follow_descriptor_vs_rename2 * test_follow_name_retry_headers * test_follow_name_remove * test_follow_name_truncate1 * test_follow_name_truncate2 * test_follow_name_truncate3 * test_follow_name_truncate4 * test_follow_truncate_fast * test_follow_name_move_create1 * test_follow_name_move_create2 * test_follow_name_move1 * test_follow_name_move2 * test_follow_name_move_retry1 * test_follow_name_move_retry2 * test_follow_inotify_only_regular * test_fifo * test_illegal_seek tests/cat: * test_dev_full * test_dev_full_show_all * test_dev_random * test_fifo_symlink tests/dd: * test_random_73k_test_lazy_fullblock * test_sync_delayed_reader tests/factor: * test_parallel tests/rm: * test_rm_force_prompts_order * test_rm_descend_directory * test_rm_prompts tests/seq: * the helper run method tests/sort: * test_sigpipe_panic tests/tee: * the helper run_tee method tests/tty: * test_tty module tests/yes: * the helper run method
2022-11-18 00:25:43 +00:00
use std::process::{ExitStatus, Stdio};
#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
2023-03-20 13:51:19 +00:00
use crate::common::util::TestScenario;
#[cfg(unix)]
2023-04-20 21:00:46 +00:00
fn check_termination(result: ExitStatus) {
assert_eq!(result.signal(), Some(libc::SIGPIPE));
}
#[cfg(not(unix))]
2023-04-20 21:00:46 +00:00
fn check_termination(result: ExitStatus) {
assert!(result.success(), "yes did not exit successfully");
}
const NO_ARGS: &[&str] = &[];
/// Run `yes`, capture some of the output, close the pipe, and verify it.
fn run(args: &[impl AsRef<OsStr>], expected: &[u8]) {
let mut cmd = new_ucmd!();
tests: Use UChild in tests. Rename run_no_wait_child to run_no_wait and return UChild tests/tail: * test_stdin_redirect_file:. Test fails now when assert_alive()! The follow test `tail -f < file` where file's content is `foo` fails with: Assertion failed. Expected 'tail' to be running but exited with status=exit status: 0 I also tried on the command line and can confirm that tail isn't runnning when following by descriptor. The test is deactivated until the implementation is fixed. * test_follow_stdin_descriptor * test_follow_stdin_explicit_indefinitely. * test_follow_single * test_follow_non_utf8_bytes * test_follow_multiple * test_follow_name_multiple * test_follow_invalid_pid * test_single_big_args * test_retry3 * test_retry4 * test_retry5 * test_retry7 * test_retry8 * test_retry9 * test_follow_descriptor_vs_rename1 * test_follow_descriptor_vs_rename2 * test_follow_name_retry_headers * test_follow_name_remove * test_follow_name_truncate1 * test_follow_name_truncate2 * test_follow_name_truncate3 * test_follow_name_truncate4 * test_follow_truncate_fast * test_follow_name_move_create1 * test_follow_name_move_create2 * test_follow_name_move1 * test_follow_name_move2 * test_follow_name_move_retry1 * test_follow_name_move_retry2 * test_follow_inotify_only_regular * test_fifo * test_illegal_seek tests/cat: * test_dev_full * test_dev_full_show_all * test_dev_random * test_fifo_symlink tests/dd: * test_random_73k_test_lazy_fullblock * test_sync_delayed_reader tests/factor: * test_parallel tests/rm: * test_rm_force_prompts_order * test_rm_descend_directory * test_rm_prompts tests/seq: * the helper run method tests/sort: * test_sigpipe_panic tests/tee: * the helper run_tee method tests/tty: * test_tty module tests/yes: * the helper run method
2022-11-18 00:25:43 +00:00
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)]
2023-04-20 21:00:46 +00:00
check_termination(child.wait_with_output().unwrap().status);
assert_eq!(buf.as_slice(), expected);
}
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
#[test]
fn test_version() {
new_ucmd!().arg("--version").succeeds();
}
#[test]
fn test_simple() {
run(NO_ARGS, b"y\ny\ny\ny\n");
}
#[test]
fn test_args() {
run(&["a", "bar", "c"], b"a bar c\na bar c\na ba");
}
#[test]
fn test_long_output() {
run(NO_ARGS, "y\n".repeat(512 * 1024).as_bytes());
}
/// Test with an output that seems likely to get mangled in case of incomplete writes.
#[test]
fn test_long_odd_output() {
run(&["abcdef"], "abcdef\n".repeat(1024 * 1024).as_bytes());
}
/// Test with an input that doesn't fit in the standard buffer.
#[test]
fn test_long_input() {
#[cfg(not(windows))]
const TIMES: usize = 14000;
// On Windows the command line is limited to 8191 bytes.
// This is not actually enough to fill the buffer, but it's still nice to
// try something long.
#[cfg(windows)]
const TIMES: usize = 500;
2021-11-13 05:23:57 +00:00
let arg = "abcdef".repeat(TIMES) + "\n";
let expected_out = arg.repeat(30);
run(&[&arg[..arg.len() - 1]], expected_out.as_bytes());
}
#[test]
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
fn test_piped_to_dev_full() {
use std::fs::OpenOptions;
2022-04-02 08:47:37 +00:00
for append in [true, false] {
{
let dev_full = OpenOptions::new()
.write(true)
.append(append)
.open("/dev/full")
.unwrap();
new_ucmd!()
.set_stdout(dev_full)
.fails()
.stderr_contains("No space left on device");
}
}
}
#[test]
#[cfg(any(unix, target_os = "wasi"))]
fn test_non_utf8() {
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi::OsStrExt;
run(
&[
OsStr::from_bytes(b"\xbf\xff\xee"),
OsStr::from_bytes(b"bar"),
],
&b"\xbf\xff\xee bar\n".repeat(5000),
);
}