Skip to content

Commit

Permalink
fix: make theme test more lenient
Browse files Browse the repository at this point in the history
The test now only considers our part of the backtrace, allowing for
changes in rust std library to not break the test
  • Loading branch information
ten3roberts committed Jan 29, 2024
1 parent eb26e02 commit 8ebc308
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
12 changes: 6 additions & 6 deletions color-eyre/tests/data/theme_error_control_minimal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ Error:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ⋮ 5 frames hidden ⋮ 
6: theme::get_error::create_report::h43540daddae98383
at /home/freja/dev/rust/eyre/color-eyre/tests/theme.rs:17
at /home/username/dev/rust/eyre/color-eyre/tests/theme.rs:17
7: theme::get_error::{{closure}}::h40bbef2f4cd93fab
at /home/freja/dev/rust/eyre/color-eyre/tests/theme.rs:26
at /home/username/dev/rust/eyre/color-eyre/tests/theme.rs:26
8: core::option::Option<T>::ok_or_else::h8aa47839ff49cfbe
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/option.rs:1087
9: theme::get_error::h78b5b4d52bfbbad0
at /home/freja/dev/rust/eyre/color-eyre/tests/theme.rs:26
at /home/username/dev/rust/eyre/color-eyre/tests/theme.rs:26
10: theme::test_error_backwards_compatibility::h9de398ce80defffa
at /home/freja/dev/rust/eyre/color-eyre/tests/theme.rs:45
at /home/username/dev/rust/eyre/color-eyre/tests/theme.rs:45
11: theme::test_error_backwards_compatibility::{{closure}}::hbe7b8ad2562c4dc4
at /home/freja/dev/rust/eyre/color-eyre/tests/theme.rs:43
at /home/username/dev/rust/eyre/color-eyre/tests/theme.rs:43
12: core::ops::function::FnOnce::call_once::hfc715417a1b707c5
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ops/function.rs:248
13: core::ops::function::FnOnce::call_once::h9ee1367930602049
Expand All @@ -43,4 +43,4 @@ Error:
Suggestion: suggestion

Run with COLORBT_SHOW_HIDDEN=1 environment variable to disable frame filtering.
Run with RUST_BACKTRACE=full to include source snippets.
Run with RUST_BACKTRACE=full to include source snippets.
4 changes: 2 additions & 2 deletions color-eyre/tests/data/theme_panic_control_no_spantrace.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `/home/freja/dev/rust/eyre/target/debug/examples/theme_test_helper`
Running `/home/username/dev/rust/eyre/target/debug/examples/theme_test_helper`
The application panicked (crashed).
Message: <non string panic payload>
Location: color-eyre/examples/theme_test_helper.rs:38
Expand All @@ -9,7 +9,7 @@ Location: color-eyre/examples/theme_test_helper.rs:38
7: std::panic::panic_any::h4a05c03c4d0c389c
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panic.rs:61
8: theme_test_helper::main::hfc653b28cad3659d
at /home/freja/dev/rust/eyre/color-eyre/examples/theme_test_helper.rs:38
at /home/username/dev/rust/eyre/color-eyre/examples/theme_test_helper.rs:38
9: core::ops::function::FnOnce::call_once::hb0110cdf4417a5ed
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ops/function.rs:248
 ⋮ 16 frames hidden ⋮ 
Expand Down
48 changes: 32 additions & 16 deletions color-eyre/tests/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ fn test_backwards_compatibility(target: String, file_name: &str) {
// `unwrap` should never fail with files generated by this function
let control = String::from_utf8(fs::read(file_path).unwrap()).unwrap();

fn f(s: &str) -> (Vec<Output>, Vec<AnsiSequence>) {
let all: Vec<_> = s.ansi_parse().collect();
let ansi: Vec<_> = s
fn split_ansi_output(input: &str) -> (Vec<Output>, Vec<AnsiSequence>) {
let all: Vec<_> = input.ansi_parse().collect();
let ansi: Vec<_> = input
.ansi_parse()
.filter_map(|x| {
if let Output::Escape(ansi) = x {
Expand All @@ -165,8 +165,26 @@ fn test_backwards_compatibility(target: String, file_name: &str) {
(all, ansi)
}

let (_control_tokens, control_ansi) = f(&control);
let (_target_tokens, target_ansi) = f(&target);
fn normalize_backtrace(input: &str) -> String {
input
.lines()
.take_while(|v| !v.contains("core::panic"))
.collect::<Vec<_>>()
.join("\n")
}

let control = normalize_backtrace(&control);
let target = normalize_backtrace(&target);
let (_control_tokens, control_ansi) = split_ansi_output(&control);
let (_target_tokens, target_ansi) = split_ansi_output(&target);

fn section(title: &str, content: impl AsRef<str>) -> String {
format!(
"{}\n{}",
format!("-------- {title} --------").red(),
content.as_ref()
)
}

// pretty_assertions::assert_eq!(target, control);
let msg = [
Expand All @@ -175,23 +193,21 @@ fn test_backwards_compatibility(target: String, file_name: &str) {
format!("{}", "\x1b[0m\n\nANSI escape sequences are not identical to control!".red()),
// ^ `\x1b[0m` clears previous ANSI escape sequences

format!("{}", "CONTROL:".red()),
format!("{}{}", "CONTROL STRING =\n".red(), &control),
//format!("{}{:?}", "CONTROL DEBUG STRING =\n".red(), &control),
//format!("{}{:?}", "CONTROL ANSI PARSER OUTPUT =\n".red(), &_control_tokens),
//format!("{}{:?}", "CONTROL ANSI PARSER ANSI =\n".red(), &control_ansi),
section("CONTROL STRING", &control),
// section("CONTROL DEBUG STRING", format!("{control:?}")),
// section("CONTROL ANSI PARSER OUTPUT", format!("{_control_tokens:?}")),
// section("CONTROL ANSI PARSER ANSI", format!("{control_ansi:?}")),

format!("{}", "CURRENT:".red()),
format!("{}{}", "CURRENT STRING =\n".red(), &target),
//format!("{}{:?}", "CURRENT DEBUG STRING =\n".red(), &target),
//format!("{}{:?}", "CURRENT ANSI PARSER OUTPUT =\n".red(), &_target_tokens),
//format!("{}{:?}", "CURRENT ANSI PARSER ANSI =\n".red(), &target_ansi),
section("CURRENT STRING", &target),
// section("CURRENT DEBUG STRING", format!("{target:?}")),
// section("CURRENT ANSI PARSER OUTPUT", format!("{_target_tokens:?}")),
// section("CURRENT ANSI PARSER ANSI", format!("{target_ansi:?}")),

format!("{}", "See the src of this test for more information about the test and ways to include/exclude debugging information.\n\n".red()),

].join("\n\n");

assert_eq!(target_ansi, control_ansi, "{}", &msg);
pretty_assertions::assert_eq!(target_ansi, control_ansi, "{}", &msg);

/*
# Tips for debugging test failures
Expand Down

0 comments on commit 8ebc308

Please sign in to comment.