mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
tests: Move seq/yes run function to Ucommand::run_stdout_starts_with
Tests for both `seq` and `yes` run a command that never terminates, and check the beggining of their output in stdout, move the copied parts of the wrapper function to common/util. We still need to use slightly different logic to parse exit value as `seq` returns success if stdout gets closed, while `yes` fails.
This commit is contained in:
parent
b540e18dec
commit
59cd6e5e41
3 changed files with 40 additions and 33 deletions
|
@ -4,7 +4,6 @@
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
// spell-checker:ignore lmnop xlmnop
|
// spell-checker:ignore lmnop xlmnop
|
||||||
use crate::common::util::TestScenario;
|
use crate::common::util::TestScenario;
|
||||||
use std::process::Stdio;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_arg() {
|
fn test_invalid_arg() {
|
||||||
|
@ -566,51 +565,52 @@ fn test_width_floats() {
|
||||||
.stdout_only("09.0\n10.0\n");
|
.stdout_only("09.0\n10.0\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO This is duplicated from `test_yes.rs`; refactor them.
|
|
||||||
/// 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).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);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_neg_inf() {
|
fn test_neg_inf() {
|
||||||
run(&["--", "-inf", "0"], b"-inf\n-inf\n-inf\n");
|
new_ucmd!()
|
||||||
|
.args(&["--", "-inf", "0"])
|
||||||
|
.run_stdout_starts_with(b"-inf\n-inf\n-inf\n")
|
||||||
|
.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_neg_infinity() {
|
fn test_neg_infinity() {
|
||||||
run(&["--", "-infinity", "0"], b"-inf\n-inf\n-inf\n");
|
new_ucmd!()
|
||||||
|
.args(&["--", "-infinity", "0"])
|
||||||
|
.run_stdout_starts_with(b"-inf\n-inf\n-inf\n")
|
||||||
|
.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_inf() {
|
fn test_inf() {
|
||||||
run(&["inf"], b"1\n2\n3\n");
|
new_ucmd!()
|
||||||
|
.args(&["inf"])
|
||||||
|
.run_stdout_starts_with(b"1\n2\n3\n")
|
||||||
|
.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_infinity() {
|
fn test_infinity() {
|
||||||
run(&["infinity"], b"1\n2\n3\n");
|
new_ucmd!()
|
||||||
|
.args(&["infinity"])
|
||||||
|
.run_stdout_starts_with(b"1\n2\n3\n")
|
||||||
|
.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_inf_width() {
|
fn test_inf_width() {
|
||||||
run(
|
new_ucmd!()
|
||||||
&["-w", "1.000", "inf", "inf"],
|
.args(&["-w", "1.000", "inf", "inf"])
|
||||||
b"1.000\n inf\n inf\n inf\n",
|
.run_stdout_starts_with(b"1.000\n inf\n inf\n inf\n")
|
||||||
);
|
.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_neg_inf_width() {
|
fn test_neg_inf_width() {
|
||||||
run(
|
new_ucmd!()
|
||||||
&["-w", "1.000", "-inf", "-inf"],
|
.args(&["-w", "1.000", "-inf", "-inf"])
|
||||||
b"1.000\n -inf\n -inf\n -inf\n",
|
.run_stdout_starts_with(b"1.000\n -inf\n -inf\n -inf\n")
|
||||||
);
|
.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// For the full copyright and license information, please view the LICENSE
|
// For the full copyright and license information, please view the LICENSE
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::process::{ExitStatus, Stdio};
|
use std::process::ExitStatus;
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::os::unix::process::ExitStatusExt;
|
use std::os::unix::process::ExitStatusExt;
|
||||||
|
@ -22,15 +22,10 @@ fn check_termination(result: ExitStatus) {
|
||||||
|
|
||||||
const NO_ARGS: &[&str] = &[];
|
const NO_ARGS: &[&str] = &[];
|
||||||
|
|
||||||
/// Run `yes`, capture some of the output, close the pipe, and verify it.
|
/// Run `yes`, capture some of the output, then check exit status.
|
||||||
fn run(args: &[impl AsRef<OsStr>], expected: &[u8]) {
|
fn run(args: &[impl AsRef<OsStr>], expected: &[u8]) {
|
||||||
let mut cmd = new_ucmd!();
|
let result = new_ucmd!().args(args).run_stdout_starts_with(expected);
|
||||||
let mut child = cmd.args(args).set_stdout(Stdio::piped()).run_no_wait();
|
check_termination(result.exit_status());
|
||||||
let buf = child.stdout_exact_bytes(expected.len());
|
|
||||||
child.close_stdout();
|
|
||||||
|
|
||||||
check_termination(child.wait().unwrap().exit_status());
|
|
||||||
assert_eq!(buf.as_slice(), expected);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -1846,6 +1846,18 @@ impl UCommand {
|
||||||
let tmpdir_path = self.tmpd.as_ref().unwrap().path();
|
let tmpdir_path = self.tmpd.as_ref().unwrap().path();
|
||||||
format!("{}/{file_rel_path}", tmpdir_path.to_str().unwrap())
|
format!("{}/{file_rel_path}", tmpdir_path.to_str().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Runs the command, checks that the stdout starts with "expected",
|
||||||
|
/// then terminates the command.
|
||||||
|
#[track_caller]
|
||||||
|
pub fn run_stdout_starts_with(&mut self, expected: &[u8]) -> CmdResult {
|
||||||
|
let mut child = self.set_stdout(Stdio::piped()).run_no_wait();
|
||||||
|
let buf = child.stdout_exact_bytes(expected.len());
|
||||||
|
child.close_stdout();
|
||||||
|
|
||||||
|
assert_eq!(buf.as_slice(), expected);
|
||||||
|
child.wait().unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for UCommand {
|
impl std::fmt::Display for UCommand {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue