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 25, 2024
1 parent eb26e02 commit e3fa771
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 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,23 @@ 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),
format!("{}", "CONTROL:"),

Check failure on line 196 in color-eyre/tests/theme.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

useless use of `format!`
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),
format!("{}", "CURRENT:"),

Check failure on line 202 in color-eyre/tests/theme.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

useless use of `format!`
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 e3fa771

Please sign in to comment.