use ansi_parser::{AnsiParser, AnsiSequence, Output};
use std::{fs, path::Path};
use tracing::instrument;
use tracing_error::ErrorLayer;
use tracing_error::SpanTrace;
use tracing_subscriber::{prelude::*, registry::Registry};
#[instrument]
fn test_capture(x: u8) -> SpanTrace {
#[allow(clippy::if_same_then_else)]
if x == 42 {
SpanTrace::capture()
} else {
SpanTrace::capture()
}
}
#[test]
fn test_backwards_compatibility() {
std::env::set_var("RUST_LIB_BACKTRACE", "full");
Registry::default().with(ErrorLayer::default()).init();
let spantrace = test_capture(42);
let colored_spantrace = format!("{}", color_spantrace::colorize(&spantrace));
let control_file_name = "theme_control.txt";
let control_file_path = ["tests/data/", control_file_name].concat();
if !Path::new(&control_file_path).is_file() {
std::fs::write(control_file_name, &colored_spantrace)
.expect("\n\nError saving `colored_spanntrace` to a file");
panic!("Required test data missing! Fix this, by moving '{}' to '{}', and commit it to Git.\n\nNote: '{0}' was just generated in the current working directory.\n\n", control_file_name, control_file_path);
}
let colored_spantrace_control =
String::from_utf8(fs::read(control_file_path).unwrap()).unwrap();
fn get_ansi(s: &str) -> impl Iterator<Item = AnsiSequence> + '_ {
s.ansi_parse().filter_map(|x| {
if let Output::Escape(ansi) = x {
Some(ansi)
} else {
None
}
})
}
let colored_spantrace_ansi = get_ansi(&colored_spantrace);
let colored_spantrace_control_ansi = get_ansi(&colored_spantrace_control);
assert!(
colored_spantrace_ansi.eq(colored_spantrace_control_ansi),
"\x1b[0mANSI escape sequences are not identical to control!\n\nCONTROL:\n\n{}\n\n\n\n{:?}\n\nCURRENT:\n\n{}\n\n\n\n{:?}\n\n", &colored_spantrace_control, &colored_spantrace_control, &colored_spantrace, &colored_spantrace
);
}