Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix color-spantrace test due to misinterpreted relative paths #109

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions color-spantrace/tests/themes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@

*/

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 {
Expand All @@ -49,22 +45,37 @@ fn test_capture(x: u8) -> SpanTrace {
}
}

#[cfg(not(miri))]
#[test]
fn test_backwards_compatibility() {
use ansi_parser::{AnsiParser, AnsiSequence, Output};
use std::{fs, path::Path};
use tracing_error::ErrorLayer;
use tracing_subscriber::{prelude::*, registry::Registry};
std::env::set_var("RUST_LIB_BACKTRACE", "full");

// This integration is ran by cargo with cwd="color-spantrace", but the string literals for
// `file!` are relative to the workspace root. This changes the cwd to the workspace root.
//
// The behavior of file! when invoked from a workspace is not documented. See: <https://doc.rust-lang.org/std/macro.file.html>.
//
// Noteworthy: non-member path dependencies will get an absolute path, as will registry and git
// dependencies.
std::env::set_current_dir("..").unwrap();

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();
let control_file_path = ["color-spantrace/tests/data/", control_file_name].concat();

// If `control_file_path` is missing, save corresponding file to current working directory, and panic with the request to move these files to `control_file_path`, and to commit them to Git. Being explicit (instead of saving directly to `control_file_path` to make sure `control_file_path` is committed to Git. These files anyway should never be missing.

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");
.expect("\n\nError saving `colored_spantrace` 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);
}

Expand Down
Loading