Skip to content

Commit aa0f784

Browse files
authored
tests: Use tracing::subscriber::DefaultGuard to set subscriber (#8023)
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.
1 parent 3e542d9 commit aa0f784

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/tests/dump_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crates_io_test_db::TestDatabase;
33

44
#[test]
55
fn dump_db_and_reimport_dump() {
6-
crates_io::util::tracing::init_for_test();
6+
let _guard = crates_io::util::tracing::init_for_test();
77

88
let db_one = TestDatabase::new();
99

src/tests/util/test_app.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ use oauth2::{ClientId, ClientSecret};
2020
use std::collections::HashSet;
2121
use std::{rc::Rc, sync::Arc, time::Duration};
2222
use tokio::runtime::Runtime;
23+
use tracing::subscriber::DefaultGuard;
2324

2425
struct TestAppInner {
26+
#[allow(dead_code)]
27+
tracing_guard: DefaultGuard,
28+
2529
pub runtime: Runtime,
2630

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

7983
TestAppBuilder {
84+
tracing_guard,
8085
config: simple_config(),
8186
index: None,
8287
build_job_runner: false,
@@ -202,6 +207,7 @@ impl TestApp {
202207
}
203208

204209
pub struct TestAppBuilder {
210+
tracing_guard: DefaultGuard,
205211
config: config::Server,
206212
index: Option<UpstreamIndex>,
207213
build_job_runner: bool,
@@ -286,6 +292,7 @@ impl TestAppBuilder {
286292
};
287293

288294
let test_app_inner = TestAppInner {
295+
tracing_guard: self.tracing_guard,
289296
runtime,
290297
app,
291298
test_database,

src/util/tracing.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use sentry::integrations::tracing::EventFilter;
2+
use tracing::subscriber::DefaultGuard;
23
use tracing::Level;
34
use tracing::Metadata;
45
use tracing_subscriber::filter::LevelFilter;
@@ -46,15 +47,17 @@ pub fn event_filter(metadata: &Metadata<'_>) -> EventFilter {
4647
}
4748

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

54-
let _ = tracing_subscriber::fmt()
55+
let subscriber = tracing_subscriber::fmt()
5556
.compact()
5657
.with_env_filter(env_filter)
5758
.without_time()
5859
.with_test_writer()
59-
.try_init();
60+
.finish();
61+
62+
tracing::subscriber::set_default(subscriber)
6063
}

0 commit comments

Comments
 (0)