Skip to content

Commit

Permalink
refactor(tests): Install color_eyre when running tests
Browse files Browse the repository at this point in the history
Shows nicer error reports when a test fails.
Because the install hook needs to be called once per process, and
cargo runs tests in parallel in the same process, the install hook
needs to be protected by a OnceCell.

Inspired by https://github.com/yaahc/color-eyre/pull/118/files
  • Loading branch information
alcroito committed May 25, 2023
1 parent 9896562 commit c326b07
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/dyndns/src/db/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ mod tests {

#[test]
fn test_do_ops_with_db() -> Result<()> {
crate::logger::init_color_eyre();

let maybe_db_path = None;
let conn = &mut setup_db(maybe_db_path)?;
let domain_record = create_domain_record(
Expand Down
2 changes: 2 additions & 0 deletions crates/dyndns/src/domain_record_api/digital_ocean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ mod tests {
fn test_basic() {
use crate::updater::{get_record_to_update, should_update_domain_ip};

crate::logger::init_color_eyre();

figment::Jail::expect_with(|jail| {
jail.create_file(
"config.toml",
Expand Down
10 changes: 10 additions & 0 deletions crates/dyndns/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ impl Drop for EyreSpanTraceWorkaroundGuard {
std::env::set_var(RUST_SPANTRACE_KEY, "1");
}
}

struct ColorEyreGuard(());
static INIT_COLOR_EYRE: OnceCell<ColorEyreGuard> = OnceCell::new();

pub fn init_color_eyre() {
INIT_COLOR_EYRE.get_or_init(|| {
color_eyre::install().expect("Failed to initialize color_eyre");
ColorEyreGuard(())
});
}
2 changes: 2 additions & 0 deletions crates/dyndns/src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ mod tests {

#[test]
fn generate_open_api_schema() {
crate::logger::init_color_eyre();

let (_, api) = get_pure_router_and_open_api();

let mut buf = Vec::with_capacity(128);
Expand Down

0 comments on commit c326b07

Please sign in to comment.