Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In tests, keep reading buffered child process output even after it has exited #2923

Merged
merged 2 commits into from
Oct 21, 2021
Merged
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
20 changes: 14 additions & 6 deletions zebra-test/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use tracing::instrument;

#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;

use std::{
convert::Infallible as NoDir,
fmt::{self, Write as _},
io::BufRead,
io::{BufReader, Lines, Read},
io::{BufRead, BufReader, Lines, Read, Write as _},
path::Path,
process::{Child, ChildStderr, ChildStdout, Command, ExitStatus, Output, Stdio},
time::{Duration, Instant},
Expand Down Expand Up @@ -286,25 +286,33 @@ impl<T> TestChild<T> {
L: Iterator<Item = std::io::Result<String>>,
{
let re = regex::Regex::new(regex).expect("regex must be valid");
while !self.past_deadline() && self.is_running() {

// We don't check `is_running` here,
// because we want to read to the end of the buffered output,
// even if the child process has exited.
Comment on lines +290 to +292
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

while !self.past_deadline() {
let line = if let Some(line) = lines.next() {
line?
} else {
// When the child process closes its output,
// and we've read all of the buffered output,
// stop checking for any more output.
break;
};

// Since we're about to discard this line write it to stdout, so it
// can be preserved. May cause weird reordering for stdout / stderr.
// Uses stdout even if the original lines were from stderr.
if self.bypass_test_capture {
// send lines to the terminal (or process stdout file redirect)
use std::io::Write;
// Send lines directly to the terminal (or process stdout file redirect).
#[allow(clippy::explicit_write)]
writeln!(std::io::stdout(), "{}", line).unwrap();
} else {
// if the test fails, the test runner captures and displays it
// If the test fails, the test runner captures and displays this output.
println!("{}", line);
}
// Some OSes require a flush to send all output to the terminal.
std::io::stdout().lock().flush()?;

if re.is_match(&line) {
return Ok(());
Expand Down