Skip to content

Commit

Permalink
test: update error message formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jun 2, 2024
1 parent ec0727d commit 2d91853
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,4 @@ smallvec = "1"
tikv-jemallocator = "0.5.4"
unicode-width = "0.1.11"
vergen = "8.3"
yansi = "1.0"
1 change: 1 addition & 0 deletions tools/tester/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
23 changes: 14 additions & 9 deletions tools/tester/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 &not_found {
println!("{}", error.render_for_expected());
}
println!("{}", "---\n".red());
}
panic!();
panic!("errors differ from expected");
}
}

Expand Down
12 changes: 12 additions & 0 deletions tools/tester/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions tools/tester/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
let mut opts = match test::test::parse_opts(&args) {
Some(Ok(o)) => o,
Expand Down
6 changes: 6 additions & 0 deletions tools/tester/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

0 comments on commit 2d91853

Please sign in to comment.