use color_eyre::{eyre::Report, Section};
#[rustfmt::skip]
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
struct TestError(&'static str);
#[rustfmt::skip]
#[tracing::instrument]
fn get_error(msg: &'static str) -> Report {
#[rustfmt::skip]
#[inline(never)]
fn create_report(msg: &'static str) -> Report {
Report::msg(msg)
.note("note")
.warning("warning")
.suggestion("suggestion")
.error(TestError("error"))
}
None::<Option<()>>.ok_or_else(|| create_report(msg)).unwrap_err()
}
#[cfg(all(not(feature = "track-caller"), not(feature = "capture-spantrace"),))]
static ERROR_FILE_NAME: &str = "theme_error_control_minimal.txt";
#[cfg(all(feature = "track-caller", not(feature = "capture-spantrace"),))]
static ERROR_FILE_NAME: &str = "theme_error_control_location.txt";
#[cfg(all(not(feature = "track-caller"), feature = "capture-spantrace",))]
static ERROR_FILE_NAME: &str = "theme_error_control_spantrace.txt";
#[cfg(all(feature = "capture-spantrace", feature = "track-caller",))]
static ERROR_FILE_NAME: &str = "theme_error_control.txt";
#[test]
fn test_error_backwards_compatibility() {
setup();
let error = get_error("test");
let target = format!("{:?}", error);
test_backwards_compatibility(target, ERROR_FILE_NAME)
}
#[cfg(not(feature = "capture-spantrace"))]
static PANIC_FILE_NAME: &str = "theme_panic_control_no_spantrace.txt";
#[cfg(feature = "capture-spantrace")]
static PANIC_FILE_NAME: &str = "theme_panic_control.txt";
#[test]
#[allow(unused_mut)]
#[allow(clippy::vec_init_then_push)]
fn test_panic_backwards_compatibility() {
let mut features: Vec<&str> = vec![];
#[cfg(feature = "capture-spantrace")]
features.push("capture-spantrace");
#[cfg(feature = "issue-url")]
features.push("issue-url");
#[cfg(feature = "track-caller")]
features.push("track-caller");
let features = features.join(",");
let features = if !features.is_empty() {
vec!["--features", &features]
} else {
vec![]
};
let output = std::process::Command::new("cargo")
.args(&["run", "--example", "theme_test_helper"])
.arg("--no-default-features")
.args(&features)
.output()
.expect("failed to execute process");
let target = String::from_utf8(output.stderr).expect("failed to convert output to `String`");
println!("{}", target);
test_backwards_compatibility(target, PANIC_FILE_NAME)
}
fn test_backwards_compatibility(target: String, file_name: &str) {
use ansi_parser::{AnsiParser, AnsiSequence, Output};
use owo_colors::OwoColorize;
use std::{fs, path::Path};
let file_path = ["tests/data/", file_name].concat();
if !Path::new(&file_path).is_file() {
std::fs::write(file_name, &target)
.expect("\n\nError saving missing `control target` 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", file_name, file_path);
}
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
.ansi_parse()
.into_iter()
.filter_map(|x| {
if let Output::Escape(ansi) = x {
Some(ansi)
} else {
None
}
})
.collect();
(all, ansi)
}
let (_control_tokens, control_ansi) = f(&control);
let (_target_tokens, target_ansi) = f(&target);
let msg = [
format!("{}", "\x1b[0m\n\nANSI escape sequences are not identical to control!".red()),
format!("{}", "CONTROL:".red()),
format!("{}{}", "CONTROL STRING =\n".red(), &control),
format!("{}", "CURRENT:".red()),
format!("{}{}", "CURRENT STRING =\n".red(), &target),
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);
}
fn setup() {
std::env::set_var("RUST_LIB_BACKTRACE", "1");
#[cfg(feature = "capture-spantrace")]
{
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};
let fmt_layer = fmt::layer().with_target(false);
let filter_layer = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
.unwrap();
tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.with(tracing_error::ErrorLayer::default())
.init();
}
color_eyre::install().expect("Failed to install `color_eyre`");
}