implement stdout streaming in render_tests::Renderer

Signed-off-by: ozkanonur <work@onurozkan.dev>
This commit is contained in:
ozkanonur 2023-06-12 12:58:47 +03:00
parent 77dba225c1
commit 6c966dc656
2 changed files with 31 additions and 9 deletions

View File

@ -7,7 +7,7 @@
//! to reimplement all the rendering logic in this module because of that. //! to reimplement all the rendering logic in this module because of that.
use crate::builder::Builder; use crate::builder::Builder;
use std::io::{BufRead, BufReader, Write}; use std::io::{BufRead, BufReader, Read, Write};
use std::process::{ChildStdout, Command, Stdio}; use std::process::{ChildStdout, Command, Stdio};
use std::time::Duration; use std::time::Duration;
use termcolor::{Color, ColorSpec, WriteColor}; use termcolor::{Color, ColorSpec, WriteColor};
@ -20,15 +20,15 @@ pub(crate) fn add_flags_and_try_run_tests(builder: &Builder<'_>, cmd: &mut Comma
} }
cmd.args(&["-Z", "unstable-options", "--format", "json"]); cmd.args(&["-Z", "unstable-options", "--format", "json"]);
try_run_tests(builder, cmd) try_run_tests(builder, cmd, false)
} }
pub(crate) fn try_run_tests(builder: &Builder<'_>, cmd: &mut Command) -> bool { pub(crate) fn try_run_tests(builder: &Builder<'_>, cmd: &mut Command, stream: bool) -> bool {
if builder.config.dry_run() { if builder.config.dry_run() {
return true; return true;
} }
if !run_tests(builder, cmd) { if !run_tests(builder, cmd, stream) {
if builder.fail_fast { if builder.fail_fast {
crate::detail_exit_macro!(1); crate::detail_exit_macro!(1);
} else { } else {
@ -41,7 +41,7 @@ pub(crate) fn try_run_tests(builder: &Builder<'_>, cmd: &mut Command) -> bool {
} }
} }
fn run_tests(builder: &Builder<'_>, cmd: &mut Command) -> bool { fn run_tests(builder: &Builder<'_>, cmd: &mut Command, stream: bool) -> bool {
cmd.stdout(Stdio::piped()); cmd.stdout(Stdio::piped());
builder.verbose(&format!("running: {cmd:?}")); builder.verbose(&format!("running: {cmd:?}"));
@ -50,7 +50,12 @@ fn run_tests(builder: &Builder<'_>, cmd: &mut Command) -> bool {
// This runs until the stdout of the child is closed, which means the child exited. We don't // This runs until the stdout of the child is closed, which means the child exited. We don't
// run this on another thread since the builder is not Sync. // run this on another thread since the builder is not Sync.
Renderer::new(process.stdout.take().unwrap(), builder).render_all(); let renderer = Renderer::new(process.stdout.take().unwrap(), builder);
if stream {
renderer.stream_all();
} else {
renderer.render_all();
}
let result = process.wait_with_output().unwrap(); let result = process.wait_with_output().unwrap();
if !result.status.success() && builder.is_verbose() { if !result.status.success() && builder.is_verbose() {
@ -112,6 +117,23 @@ impl<'a> Renderer<'a> {
} }
} }
/// Renders the stdout characters one by one
fn stream_all(mut self) {
let mut buffer = [0; 1];
loop {
match self.stdout.read(&mut buffer) {
Ok(0) => break,
Ok(_) => {
let mut stdout = std::io::stdout();
stdout.write_all(&buffer).unwrap();
let _ = stdout.flush();
}
Err(err) if err.kind() == std::io::ErrorKind::UnexpectedEof => break,
Err(err) => panic!("failed to read output of test runner: {err}"),
}
}
}
fn render_test_outcome(&mut self, outcome: Outcome<'_>, test: &TestOutcome) { fn render_test_outcome(&mut self, outcome: Outcome<'_>, test: &TestOutcome) {
self.executed_tests += 1; self.executed_tests += 1;

View File

@ -1035,7 +1035,7 @@ impl Step for RustdocGUI {
} }
let _time = util::timeit(&builder); let _time = util::timeit(&builder);
crate::render_tests::try_run_tests(builder, &mut cmd); crate::render_tests::try_run_tests(builder, &mut cmd, true);
} }
} }
@ -1732,7 +1732,7 @@ note: if you're sure you want to do this, please open an issue as to why. In the
suite, mode, &compiler.host, target suite, mode, &compiler.host, target
)); ));
let _time = util::timeit(&builder); let _time = util::timeit(&builder);
crate::render_tests::try_run_tests(builder, &mut cmd); crate::render_tests::try_run_tests(builder, &mut cmd, false);
if let Some(compare_mode) = compare_mode { if let Some(compare_mode) = compare_mode {
cmd.arg("--compare-mode").arg(compare_mode); cmd.arg("--compare-mode").arg(compare_mode);
@ -1755,7 +1755,7 @@ note: if you're sure you want to do this, please open an issue as to why. In the
suite, mode, compare_mode, &compiler.host, target suite, mode, compare_mode, &compiler.host, target
)); ));
let _time = util::timeit(&builder); let _time = util::timeit(&builder);
crate::render_tests::try_run_tests(builder, &mut cmd); crate::render_tests::try_run_tests(builder, &mut cmd, false);
} }
} }
} }