From 2d9185363d8c59241ed4da162eb1098e78d4d2b4 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 2 Jun 2024 17:09:00 +0200 Subject: [PATCH] test: update error message formatting --- Cargo.lock | 12 +++++++++++- Cargo.toml | 1 + tools/tester/Cargo.toml | 1 + tools/tester/src/context.rs | 23 ++++++++++++++--------- tools/tester/src/errors.rs | 12 ++++++++++++ tools/tester/src/lib.rs | 2 ++ tools/tester/src/utils.rs | 6 ++++++ 7 files changed, 47 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad0d8f43..71b2ae72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -152,7 +152,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "367fd0ad87307588d087544707bc5fbf4805ded96c7db922b70d368fa1cb5702" dependencies = [ "unicode-width", - "yansi", + "yansi 0.5.1", ] [[package]] @@ -2288,6 +2288,7 @@ dependencies = [ "tester", "unified-diff", "walkdir", + "yansi 1.0.1", ] [[package]] @@ -2969,6 +2970,15 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" +[[package]] +name = "yansi" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" +dependencies = [ + "is-terminal", +] + [[package]] name = "zerocopy" version = "0.7.34" diff --git a/Cargo.toml b/Cargo.toml index bb20f3a5..2b510feb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -123,3 +123,4 @@ smallvec = "1" tikv-jemallocator = "0.5.4" unicode-width = "0.1.11" vergen = "8.3" +yansi = "1.0" diff --git a/tools/tester/Cargo.toml b/tools/tester/Cargo.toml index d7df3768..1d42153c 100644 --- a/tools/tester/Cargo.toml +++ b/tools/tester/Cargo.toml @@ -27,6 +27,7 @@ tempfile.workspace = true tester.workspace = true unified-diff.workspace = true walkdir.workspace = true +yansi = { workspace = true, features = ["detect-env", "detect-tty"] } [features] nightly = [] diff --git a/tools/tester/src/context.rs b/tools/tester/src/context.rs index c1866c23..8c3ff74e 100644 --- a/tools/tester/src/context.rs +++ b/tools/tester/src/context.rs @@ -367,19 +367,24 @@ impl TestCx<'_> { } if !unexpected.is_empty() || !not_found.is_empty() { - self.error(&format!( - "{} unexpected errors found, {} expected errors not found", - unexpected.len(), - not_found.len() - )); - println!("status: {}\ncommand: {}", output.status, output.cmdline); + use yansi::Paint; + + println!("status: {}\ncommand: {}\n", output.status, output.cmdline); if !unexpected.is_empty() { - println!("unexpected errors (from JSON output): {unexpected:#?}\n"); + println!("{}", "--- unexpected errors (from JSON output) ---".green()); + for error in &unexpected { + println!("{}", error.render_for_expected()); + } + println!("{}", "---".green()); } if !not_found.is_empty() { - println!("not found errors (from test file): {not_found:#?}\n"); + println!("{}", "--- not found errors (from test file) ---".red()); + for error in ¬_found { + println!("{}", error.render_for_expected()); + } + println!("{}", "---\n".red()); } - panic!(); + panic!("errors differ from expected"); } } diff --git a/tools/tester/src/errors.rs b/tools/tester/src/errors.rs index ff19797a..230005d6 100644 --- a/tools/tester/src/errors.rs +++ b/tools/tester/src/errors.rs @@ -130,6 +130,18 @@ impl Error { } } +impl Error { + pub fn render_for_expected(&self) -> String { + use yansi::Paint; + format!( + "{: <10}line {: >3}: {}", + self.kind.map(|kind| kind.to_string()).unwrap_or_default().to_uppercase(), + self.line_num, + self.msg.cyan(), + ) + } +} + #[derive(PartialEq, Debug)] enum WhichLine { ThisLine, diff --git a/tools/tester/src/lib.rs b/tools/tester/src/lib.rs index c4ec8f9a..0956b5c5 100644 --- a/tools/tester/src/lib.rs +++ b/tools/tester/src/lib.rs @@ -38,6 +38,8 @@ mod utils; use utils::TestResult; pub fn run_tests(cmd: &'static Path) -> i32 { + utils::enable_paint(); + let args = std::env::args().collect::>(); let mut opts = match test::test::parse_opts(&args) { Some(Ok(o)) => o, diff --git a/tools/tester/src/utils.rs b/tools/tester/src/utils.rs index f0d16f27..d7e889ba 100644 --- a/tools/tester/src/utils.rs +++ b/tools/tester/src/utils.rs @@ -24,3 +24,9 @@ pub(crate) fn path_contains(haystack: &Path, needle: &str) -> bool { let s = s.replace('\\', "/"); s.contains(needle) } + +/// Sets the default [`yansi`] color output condition. +pub(crate) fn enable_paint() { + let enable = yansi::Condition::os_support() && yansi::Condition::tty_and_color_live(); + yansi::whenever(yansi::Condition::cached(enable)); +}