Skip to content

Commit

Permalink
tests: put mocking functionality into a crate (tokio-rs#2009)
Browse files Browse the repository at this point in the history
... instead of `#[path = ""]` importing it everywhere.

Make sure to use a diff review tool that understands file renaming 😅
(GitHub's diff view does.)

Transparency: I want to use the mocking functionality in the development
of a tracing component out-of-tree.

Additionally, this reduces the use of `#[path] mod` and file
multiple-inclusion, which aren't that great of a practice.

The tracing test support module was already well self-contained, due to
being `#[path] mod` used in multiple places. As such, extracting it to
its own crate is rather mechanical, with no surprising blockers.

We additionally move the tracing-futures support module contents into
tracing_mock, for convenience. The one function which relies on
tokio-test is made optional.

It's a reasonable result for this functionality to stay unpublished, and
only used inside the repo, but pulling it out into a directly reusable
chunk instead of abusing the module system to reuse it via
multiple-inclusion is an improvement to code structure and
modularization.
  • Loading branch information
CAD97 authored and kaffarell committed May 22, 2024
1 parent ed1c104 commit 2543581
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 1,284 deletions.
1 change: 1 addition & 0 deletions tracing-attributes/tests/ret.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::convert::TryFrom;
use std::num::TryFromIntError;
use tracing_mock::*;

use tracing::{collect::with_default, Level};
use tracing_attributes::instrument;
Expand Down
19 changes: 7 additions & 12 deletions tracing-subscriber/tests/support.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
#![allow(missing_docs, dead_code)]
pub use self::support::{event, field, span, subscriber};
// This has to have the same name as the module in `tracing`.
// path attribute requires referenced module to have same name so allow module inception here
#[allow(clippy::module_inception)]
#[path = "../../tracing/tests/support/mod.rs"]
mod support;

use self::{
event::MockEvent,
span::{MockSpan, NewSpan},
subscriber::{Expect, MockHandle},
};
pub use tracing_mock::{event, field, span, subscriber};

use tracing_core::{
span::{Attributes, Id, Record},
Event, Subscriber,
};
use tracing_mock::{
event::MockEvent,
span::{MockSpan, NewSpan},
subscriber::{Expect, MockHandle},
};
use tracing_subscriber::{
layer::{Context, Layer},
registry::{LookupSpan, SpanRef},
Expand Down
4 changes: 1 addition & 3 deletions tracing/tests/no_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

use tracing::subscriber::{self, NoSubscriber};

mod support;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn no_subscriber_disables_global() {
// Reproduces https://github.com/tokio-rs/tracing/issues/1999
let (subscriber, handle) = support::subscriber::mock().done().run_with_handle();
let (subscriber, handle) = tracing_mock::subscriber::mock().done().run_with_handle();
subscriber::set_global_default(subscriber).expect("setting global default must succeed");
subscriber::with_default(NoSubscriber::default(), || {
tracing::info!("this should not be recorded");
Expand Down
4 changes: 1 addition & 3 deletions tracing/tests/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use tracing::{
Event, Level, Metadata,
};

mod support;

use self::support::*;
use tracing_mock::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
Expand Down
168 changes: 0 additions & 168 deletions tracing/tests/support/event.rs

This file was deleted.

Loading

0 comments on commit 2543581

Please sign in to comment.