Skip to content

Commit

Permalink
tests: Use tracing::subscriber::DefaultGuard to set subscriber (#8023)
Browse files Browse the repository at this point in the history
This avoids `try_init()` failing because a default subscriber is already registered globally. `set_default()` will register a temporary one only for the lifetime of the returned scope guard.
  • Loading branch information
Turbo87 authored Jan 29, 2024
1 parent 3e542d9 commit aa0f784
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/tests/dump_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crates_io_test_db::TestDatabase;

#[test]
fn dump_db_and_reimport_dump() {
crates_io::util::tracing::init_for_test();
let _guard = crates_io::util::tracing::init_for_test();

let db_one = TestDatabase::new();

Expand Down
9 changes: 8 additions & 1 deletion src/tests/util/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ use oauth2::{ClientId, ClientSecret};
use std::collections::HashSet;
use std::{rc::Rc, sync::Arc, time::Duration};
use tokio::runtime::Runtime;
use tracing::subscriber::DefaultGuard;

struct TestAppInner {
#[allow(dead_code)]
tracing_guard: DefaultGuard,

pub runtime: Runtime,

app: Arc<App>,
Expand Down Expand Up @@ -74,9 +78,10 @@ pub struct TestApp(Rc<TestAppInner>);
impl TestApp {
/// Initialize an application with an `Uploader` that panics
pub fn init() -> TestAppBuilder {
crates_io::util::tracing::init_for_test();
let tracing_guard = crates_io::util::tracing::init_for_test();

TestAppBuilder {
tracing_guard,
config: simple_config(),
index: None,
build_job_runner: false,
Expand Down Expand Up @@ -202,6 +207,7 @@ impl TestApp {
}

pub struct TestAppBuilder {
tracing_guard: DefaultGuard,
config: config::Server,
index: Option<UpstreamIndex>,
build_job_runner: bool,
Expand Down Expand Up @@ -286,6 +292,7 @@ impl TestAppBuilder {
};

let test_app_inner = TestAppInner {
tracing_guard: self.tracing_guard,
runtime,
app,
test_database,
Expand Down
9 changes: 6 additions & 3 deletions src/util/tracing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sentry::integrations::tracing::EventFilter;
use tracing::subscriber::DefaultGuard;
use tracing::Level;
use tracing::Metadata;
use tracing_subscriber::filter::LevelFilter;
Expand Down Expand Up @@ -46,15 +47,17 @@ pub fn event_filter(metadata: &Metadata<'_>) -> EventFilter {
}

/// Initializes the `tracing` logging framework for usage in tests.
pub fn init_for_test() {
pub fn init_for_test() -> DefaultGuard {
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();

let _ = tracing_subscriber::fmt()
let subscriber = tracing_subscriber::fmt()
.compact()
.with_env_filter(env_filter)
.without_time()
.with_test_writer()
.try_init();
.finish();

tracing::subscriber::set_default(subscriber)
}

0 comments on commit aa0f784

Please sign in to comment.