Skip to content

Commit

Permalink
Condense test logs by hiding successful tests using ANSI codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lysxia committed Dec 3, 2024
1 parent 14f28d2 commit 44b357f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
19 changes: 13 additions & 6 deletions creusot/tests/creusot-contracts.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -82,7 +90,6 @@ fn main() {
};
out.reset().unwrap();

buf.reset().unwrap();
let wrt = BufferWriter::stdout(ColorChoice::Always);
wrt.print(&buf).unwrap();
}
Expand Down
3 changes: 2 additions & 1 deletion creusot/tests/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ pub fn differ(
stdout: &Path,
stderr: Option<&Path>,
should_succeed: bool,
enable_color: bool,
) -> Result<(bool, Buffer), Box<dyn Error>> {
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());
Expand Down
25 changes: 16 additions & 9 deletions creusot/tests/ui.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
env,
fs::File,
io::{BufRead, BufReader, Write},
io::{BufRead, BufReader, IsTerminal, Write},
path::{Path, PathBuf},
process::Command,
};
Expand Down Expand Up @@ -148,7 +148,9 @@ fn glob_runner<B>(s: &str, command_builder: B, should_succeed: bool)
where
B: Fn(&Path) -> Option<std::process::Command>,
{
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;
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -213,15 +220,15 @@ where
};
out.reset().unwrap();

buf.reset().unwrap();
let wrt = BufferWriter::stdout(ColorChoice::Always);
wrt.print(&buf).unwrap();
}
}

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);
}
}

0 comments on commit 44b357f

Please sign in to comment.