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

tests: Use tracing::subscriber::DefaultGuard to set subscriber #8023

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
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
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)
}