Skip to content

Commit 59cd6e5

Browse files
drinkcatsylvestre
authored andcommitted
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.
1 parent b540e18 commit 59cd6e5

3 files changed

Lines changed: 40 additions & 33 deletions

File tree

tests/by-util/test_seq.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// file that was distributed with this source code.
55
// spell-checker:ignore lmnop xlmnop
66
use crate::common::util::TestScenario;
7-
use std::process::Stdio;
87

98
#[test]
109
fn test_invalid_arg() {
@@ -566,51 +565,52 @@ fn test_width_floats() {
566565
.stdout_only("09.0\n10.0\n");
567566
}
568567

569-
// TODO This is duplicated from `test_yes.rs`; refactor them.
570-
/// Run `seq`, capture some of the output, close the pipe, and verify it.
571-
fn run(args: &[&str], expected: &[u8]) {
572-
let mut cmd = new_ucmd!();
573-
let mut child = cmd.args(args).set_stdout(Stdio::piped()).run_no_wait();
574-
let buf = child.stdout_exact_bytes(expected.len());
575-
child.close_stdout();
576-
child.wait().unwrap().success();
577-
assert_eq!(buf.as_slice(), expected);
578-
}
579-
580568
#[test]
581569
fn test_neg_inf() {
582-
run(&["--", "-inf", "0"], b"-inf\n-inf\n-inf\n");
570+
new_ucmd!()
571+
.args(&["--", "-inf", "0"])
572+
.run_stdout_starts_with(b"-inf\n-inf\n-inf\n")
573+
.success();
583574
}
584575

585576
#[test]
586577
fn test_neg_infinity() {
587-
run(&["--", "-infinity", "0"], b"-inf\n-inf\n-inf\n");
578+
new_ucmd!()
579+
.args(&["--", "-infinity", "0"])
580+
.run_stdout_starts_with(b"-inf\n-inf\n-inf\n")
581+
.success();
588582
}
589583

590584
#[test]
591585
fn test_inf() {
592-
run(&["inf"], b"1\n2\n3\n");
586+
new_ucmd!()
587+
.args(&["inf"])
588+
.run_stdout_starts_with(b"1\n2\n3\n")
589+
.success();
593590
}
594591

595592
#[test]
596593
fn test_infinity() {
597-
run(&["infinity"], b"1\n2\n3\n");
594+
new_ucmd!()
595+
.args(&["infinity"])
596+
.run_stdout_starts_with(b"1\n2\n3\n")
597+
.success();
598598
}
599599

600600
#[test]
601601
fn test_inf_width() {
602-
run(
603-
&["-w", "1.000", "inf", "inf"],
604-
b"1.000\n inf\n inf\n inf\n",
605-
);
602+
new_ucmd!()
603+
.args(&["-w", "1.000", "inf", "inf"])
604+
.run_stdout_starts_with(b"1.000\n inf\n inf\n inf\n")
605+
.success();
606606
}
607607

608608
#[test]
609609
fn test_neg_inf_width() {
610-
run(
611-
&["-w", "1.000", "-inf", "-inf"],
612-
b"1.000\n -inf\n -inf\n -inf\n",
613-
);
610+
new_ucmd!()
611+
.args(&["-w", "1.000", "-inf", "-inf"])
612+
.run_stdout_starts_with(b"1.000\n -inf\n -inf\n -inf\n")
613+
.success();
614614
}
615615

616616
#[test]

tests/by-util/test_yes.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55
use std::ffi::OsStr;
6-
use std::process::{ExitStatus, Stdio};
6+
use std::process::ExitStatus;
77

88
#[cfg(unix)]
99
use std::os::unix::process::ExitStatusExt;
@@ -22,15 +22,10 @@ fn check_termination(result: ExitStatus) {
2222

2323
const NO_ARGS: &[&str] = &[];
2424

25-
/// Run `yes`, capture some of the output, close the pipe, and verify it.
25+
/// Run `yes`, capture some of the output, then check exit status.
2626
fn run(args: &[impl AsRef<OsStr>], expected: &[u8]) {
27-
let mut cmd = new_ucmd!();
28-
let mut child = cmd.args(args).set_stdout(Stdio::piped()).run_no_wait();
29-
let buf = child.stdout_exact_bytes(expected.len());
30-
child.close_stdout();
31-
32-
check_termination(child.wait().unwrap().exit_status());
33-
assert_eq!(buf.as_slice(), expected);
27+
let result = new_ucmd!().args(args).run_stdout_starts_with(expected);
28+
check_termination(result.exit_status());
3429
}
3530

3631
#[test]

tests/common/util.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,18 @@ impl UCommand {
18461846
let tmpdir_path = self.tmpd.as_ref().unwrap().path();
18471847
format!("{}/{file_rel_path}", tmpdir_path.to_str().unwrap())
18481848
}
1849+
1850+
/// Runs the command, checks that the stdout starts with "expected",
1851+
/// then terminates the command.
1852+
#[track_caller]
1853+
pub fn run_stdout_starts_with(&mut self, expected: &[u8]) -> CmdResult {
1854+
let mut child = self.set_stdout(Stdio::piped()).run_no_wait();
1855+
let buf = child.stdout_exact_bytes(expected.len());
1856+
child.close_stdout();
1857+
1858+
assert_eq!(buf.as_slice(), expected);
1859+
child.wait().unwrap()
1860+
}
18491861
}
18501862

18511863
impl std::fmt::Display for UCommand {

0 commit comments

Comments
 (0)