From 44b357fdf67f3ce74f3d87dc1d37530ca43c1202 Mon Sep 17 00:00:00 2001 From: Li-yao Xia Date: Tue, 3 Dec 2024 12:26:37 +0100 Subject: [PATCH] Condense test logs by hiding successful tests using ANSI codes --- creusot/tests/creusot-contracts.rs | 19 +++++++++++++------ creusot/tests/diff.rs | 3 ++- creusot/tests/ui.rs | 25 ++++++++++++++++--------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/creusot/tests/creusot-contracts.rs b/creusot/tests/creusot-contracts.rs index 37b692c39..ddab3e91c 100644 --- a/creusot/tests/creusot-contracts.rs +++ b/creusot/tests/creusot-contracts.rs @@ -1,4 +1,7 @@ -use std::{io::Write as _, path::PathBuf}; +use std::{ + io::{IsTerminal, Write as _}, + path::PathBuf, +}; use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, StandardStream, WriteColor as _}; mod diff; @@ -43,9 +46,14 @@ fn main() { "creusot-contracts", ]); - let output = cargo_creusot.output().unwrap(); - let mut out = StandardStream::stdout(ColorChoice::Always); + let is_tty = std::io::stdout().is_terminal(); + let mut out = + StandardStream::stdout(if is_tty { ColorChoice::Always } else { ColorChoice::Never }); + + write!(out, "Testing creusot-contracts ... ").unwrap(); + out.flush().unwrap(); + let output = cargo_creusot.output().unwrap(); let stdout = PathBuf::from("tests/creusot-contracts/creusot-contracts.coma"); let mut failed = false; @@ -59,7 +67,7 @@ fn main() { out.set_color(ColorSpec::new().set_fg(Some(Color::Blue))).unwrap(); writeln!(&mut out, "blessed").unwrap(); out.reset().unwrap(); - let (success, _) = differ(output.clone(), &stdout, None, true).unwrap(); + let (success, _) = differ(output.clone(), &stdout, None, true, is_tty).unwrap(); if !success { out.set_color(ColorSpec::new().set_fg(Some(Color::Red))).unwrap(); @@ -69,7 +77,7 @@ fn main() { std::fs::write(stdout, &output.stdout).unwrap(); } else { - let (success, mut buf) = differ(output.clone(), &stdout, None, true).unwrap(); + let (success, buf) = differ(output.clone(), &stdout, None, true, is_tty).unwrap(); if success { out.set_color(ColorSpec::new().set_fg(Some(Color::Green))).unwrap(); @@ -82,7 +90,6 @@ fn main() { }; out.reset().unwrap(); - buf.reset().unwrap(); let wrt = BufferWriter::stdout(ColorChoice::Always); wrt.print(&buf).unwrap(); } diff --git a/creusot/tests/diff.rs b/creusot/tests/diff.rs index 78ef7769e..1e533d441 100644 --- a/creusot/tests/diff.rs +++ b/creusot/tests/diff.rs @@ -19,8 +19,9 @@ pub fn differ( stdout: &Path, stderr: Option<&Path>, should_succeed: bool, + enable_color: bool, ) -> Result<(bool, Buffer), Box> { - let mut buf = Buffer::ansi(); + let mut buf = if enable_color { Buffer::ansi() } else { Buffer::no_color() }; match output.clone().ok() { Ok(output) => { let expect_out = &std::fs::read(stdout).unwrap_or_else(|_| Vec::new()); diff --git a/creusot/tests/ui.rs b/creusot/tests/ui.rs index 434963f56..8fe1504e9 100644 --- a/creusot/tests/ui.rs +++ b/creusot/tests/ui.rs @@ -1,7 +1,7 @@ use std::{ env, fs::File, - io::{BufRead, BufReader, Write}, + io::{BufRead, BufReader, IsTerminal, Write}, path::{Path, PathBuf}, process::Command, }; @@ -148,7 +148,9 @@ fn glob_runner(s: &str, command_builder: B, should_succeed: bool) where B: Fn(&Path) -> Option, { - let mut out = StandardStream::stdout(ColorChoice::Always); + let is_tty = std::io::stdout().is_terminal(); + let mut out = + StandardStream::stdout(if is_tty { ColorChoice::Always } else { ColorChoice::Never }); let mut test_count = 0; let mut test_failures = 0; @@ -173,13 +175,14 @@ where let stdout = entry.with_extension("coma"); write!(&mut out, "Testing {} ... ", entry.display()).unwrap(); + out.flush().unwrap(); if bless { out.set_color(ColorSpec::new().set_fg(Some(Color::Blue))).unwrap(); writeln!(&mut out, "blessed").unwrap(); out.reset().unwrap(); let (success, _) = - differ(output.clone(), &stdout, Some(&stderr), should_succeed).unwrap(); + differ(output.clone(), &stdout, Some(&stderr), should_succeed, is_tty).unwrap(); if !success { out.set_color(ColorSpec::new().set_fg(Some(Color::Red))).unwrap(); @@ -199,12 +202,16 @@ where std::fs::write(stderr, &output.stderr).unwrap(); } } else { - let (success, mut buf) = - differ(output.clone(), &stdout, Some(&stderr), should_succeed).unwrap(); + let (success, buf) = + differ(output.clone(), &stdout, Some(&stderr), should_succeed, is_tty).unwrap(); if success { - out.set_color(ColorSpec::new().set_fg(Some(Color::Green))).unwrap(); - writeln!(&mut out, "ok").unwrap(); + if is_tty { + // Move to beginning of line and clear line. + write!(out, "\x1b[G\x1b[2K").unwrap(); + } else { + writeln!(out, "ok").unwrap(); + } } else { out.set_color(ColorSpec::new().set_fg(Some(Color::Red))).unwrap(); writeln!(&mut out, "failure").unwrap(); @@ -213,7 +220,6 @@ where }; out.reset().unwrap(); - buf.reset().unwrap(); let wrt = BufferWriter::stdout(ColorChoice::Always); wrt.print(&buf).unwrap(); } @@ -221,7 +227,8 @@ where if test_failures > 0 { out.set_color(ColorSpec::new().set_fg(Some(Color::Red))).unwrap(); + writeln!(&mut out, "{test_failures} failures out of {test_count} tests").unwrap(); drop(out); - panic!("{} failures out of {} tests", test_failures, test_count); + std::process::exit(1); } }