Skip to content

Commit

Permalink
tests: put mocking functionality into a crate (#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.)

## Motivation

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.

## Solution

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 committed Mar 22, 2022
1 parent 44dc9f5 commit ced9302
Show file tree
Hide file tree
Showing 47 changed files with 120 additions and 147 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"tracing-tower",
"tracing-log",
"tracing-macros",
"tracing-mock",
"tracing-opentelemetry",
"tracing-subscriber",
"tracing-serde",
Expand Down
1 change: 1 addition & 0 deletions tracing-attributes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ quote = "1"

[dev-dependencies]
tracing = { path = "../tracing", version = "0.2" }
tracing-mock = { path = "../tracing-mock", features = ["tokio-test"] }
tokio-test = { version = "0.2.0" }
tracing-core = { path = "../tracing-core", version = "0.2"}
async-trait = "0.1.44"
Expand Down
6 changes: 1 addition & 5 deletions tracing-attributes/tests/async_fn.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#[path = "../../tracing-futures/tests/support.rs"]
// we don't use some of the test support functions, but `tracing-futures` does.
#[allow(dead_code)]
mod support;
use support::*;
use tracing_mock::*;

use std::{future::Future, pin::Pin, sync::Arc};
use tracing::collect::with_default;
Expand Down
4 changes: 1 addition & 3 deletions tracing-attributes/tests/destructuring.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod support;
use support::*;

use tracing::collect::with_default;
use tracing_attributes::instrument;
use tracing_mock::*;

#[test]
fn destructure_tuples() {
Expand Down
7 changes: 1 addition & 6 deletions tracing-attributes/tests/err.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#[path = "../../tracing-futures/tests/support.rs"]
// we don't use some of the test support functions, but `tracing-futures` does.
#[allow(dead_code)]
mod support;
use support::*;

use tracing::collect::with_default;
use tracing::Level;
use tracing_attributes::instrument;
use tracing_mock::*;

use std::convert::TryFrom;
use std::num::TryFromIntError;
Expand Down
8 changes: 3 additions & 5 deletions tracing-attributes/tests/fields.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod support;
use support::*;

use crate::support::field::mock;
use crate::support::span::NewSpan;
use tracing::collect::with_default;
use tracing_attributes::instrument;
use tracing_mock::field::mock;
use tracing_mock::span::NewSpan;
use tracing_mock::*;

#[instrument(fields(foo = "bar", dsa = true, num = 1))]
fn fn_no_param() {}
Expand Down
4 changes: 1 addition & 3 deletions tracing-attributes/tests/instrument.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod support;
use support::*;

use tracing::collect::with_default;
use tracing::Level;
use tracing_attributes::instrument;
use tracing_mock::*;

#[test]
fn override_everything() {
Expand Down
4 changes: 1 addition & 3 deletions tracing-attributes/tests/levels.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod support;
use support::*;

use tracing::collect::with_default;
use tracing::Level;
use tracing_attributes::instrument;
use tracing_mock::*;

#[test]
fn named_levels() {
Expand Down
4 changes: 1 addition & 3 deletions tracing-attributes/tests/names.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod support;
use support::*;

use tracing::collect::with_default;
use tracing_attributes::instrument;
use tracing_mock::*;

#[instrument]
fn default_name() {}
Expand Down
7 changes: 1 addition & 6 deletions tracing-attributes/tests/ret.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#[path = "../../tracing-futures/tests/support.rs"]
// we don't use some of the test support functions, but `tracing-futures` does.
#[allow(dead_code)]
mod support;
use support::*;

use std::convert::TryFrom;
use std::num::TryFromIntError;
use tracing_mock::*;

use tracing::{collect::with_default, Level};
use tracing_attributes::instrument;
Expand Down
5 changes: 0 additions & 5 deletions tracing-attributes/tests/support.rs

This file was deleted.

4 changes: 1 addition & 3 deletions tracing-attributes/tests/targets.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod support;
use support::*;

use tracing::collect::with_default;
use tracing_attributes::instrument;
use tracing_mock::*;

#[instrument]
fn default_target() {}
Expand Down
1 change: 1 addition & 0 deletions tracing-futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ tokio = { version = "0.1", optional = true }
tokio = "0.1.22"
tokio-test = "0.2"
tracing-core = { path = "../tracing-core", version = "0.2" }
tracing-mock = { path = "../tracing-mock" }

[badges]
maintenance = { status = "actively-developed" }
Expand Down
11 changes: 2 additions & 9 deletions tracing-futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,17 +512,10 @@ impl<T> WithDispatch<T> {
}
}

#[cfg(test)]
pub(crate) use self::support as test_support;
// This has to have the same name as the module in `tracing`.
#[path = "../../tracing/tests/support/mod.rs"]
#[cfg(test)]
#[allow(unreachable_pub)]
pub(crate) mod support;

#[cfg(test)]
mod tests {
use super::{test_support::*, *};
use super::*;
use tracing_mock::*;

#[cfg(feature = "futures-01")]
mod futures_01_tests {
Expand Down
4 changes: 1 addition & 3 deletions tracing-futures/tests/std_future.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod support;
use support::*;

use tracing::Instrument;
use tracing::{collect::with_default, Level};
use tracing_mock::*;

#[test]
fn enter_exit_is_reasonable() {
Expand Down
27 changes: 27 additions & 0 deletions tracing-mock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## BIG SCARY NOTE
# This Cargo.toml does not match the repo conventions YET
# Before releasing to crates.io: make it so!

[package]
name = "tracing-mock"
version = "0.2.0"
authors = [
"Eliza Weisman <eliza@buoyant.io>",
"Tokio Contributors <team@tokio.rs>",
]
license = "MIT"
readme = "README.md"
repository = "https://github.com/tokio-rs/tracing"
homepage = "https://tokio.rs"
edition = "2018"
rust-version = "1.49.0"
publish = false

[dependencies]
tracing = { path = "../tracing", version = "0.2", default-features = false }
tracing-core = { path = "../tracing-core", version = "0.2", default-features = false }
tokio-test = { version = "0.2.0", optional = true }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
25 changes: 25 additions & 0 deletions tracing-mock/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) 2019 Tokio Contributors

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
File renamed without changes.
4 changes: 2 additions & 2 deletions tracing/tests/support/event.rs → tracing-mock/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::fmt;
#[derive(Debug, Default, Eq, PartialEq)]
pub struct MockEvent {
pub fields: Option<field::Expect>,
pub(in crate::support) parent: Option<Parent>,
pub(crate) parent: Option<Parent>,
metadata: metadata::Expect,
}

Expand Down Expand Up @@ -78,7 +78,7 @@ impl MockEvent {
}
}

pub(in crate::support) fn check(&mut self, event: &tracing::Event<'_>) {
pub(crate) fn check(&mut self, event: &tracing::Event<'_>) {
let meta = event.metadata();
let name = meta.name();
self.metadata
Expand Down
File renamed without changes.
27 changes: 18 additions & 9 deletions tracing-futures/tests/support.rs → tracing-mock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// this is the only way to make the path attribute play nice with `in
// crate::support`...
#[allow(clippy::module_inception)]
#[path = "../../tracing/tests/support/mod.rs"]
mod support;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
pub use support::*;
use tokio_test::task;

pub mod collector;
pub mod event;
pub mod field;
mod metadata;
pub mod span;

#[derive(Debug, Eq, PartialEq)]
pub(crate) enum Parent {
ContextualRoot,
Contextual(String),
ExplicitRoot,
Explicit(String),
}

pub struct PollN<T, E> {
and_return: Option<Result<T, E>>,
Expand Down Expand Up @@ -56,10 +62,13 @@ impl PollN<(), ()> {
}
}

#[cfg(feature = "tokio-test")]
pub fn block_on_future<F>(future: F) -> F::Output
where
F: Future,
F: std::future::Future,
{
use tokio_test::task;

let mut task = task::spawn(future);
loop {
if let Poll::Ready(v) = task.poll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Expect {
}

impl Expect {
pub(in crate::support) fn check(&self, actual: &Metadata<'_>, ctx: fmt::Arguments<'_>) {
pub(crate) fn check(&self, actual: &Metadata<'_>, ctx: fmt::Arguments<'_>) {
if let Some(ref expected_name) = self.name {
let name = actual.name();
assert!(
Expand Down
13 changes: 4 additions & 9 deletions tracing/tests/support/span.rs → tracing-mock/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use std::fmt;
/// `subscriber` module.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MockSpan {
pub(in crate::support) metadata: metadata::Expect,
pub(crate) metadata: metadata::Expect,
}

#[derive(Debug, Default, Eq, PartialEq)]
pub struct NewSpan {
pub(in crate::support) span: MockSpan,
pub(in crate::support) fields: field::Expect,
pub(in crate::support) parent: Option<Parent>,
pub(crate) span: MockSpan,
pub(crate) fields: field::Expect,
pub(crate) parent: Option<Parent>,
}

pub fn mock() -> MockSpan {
Expand Down Expand Up @@ -96,11 +96,6 @@ impl MockSpan {
..Default::default()
}
}

pub(in crate::support) fn check_metadata(&self, actual: &tracing::Metadata<'_>) {
self.metadata.check(actual, format_args!("span {}", self));
assert!(actual.is_span(), "expected a span but got {:?}", actual);
}
}

impl fmt::Display for MockSpan {
Expand Down
1 change: 1 addition & 0 deletions tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ thread_local = { version = "1.1.4", optional = true }

[dev-dependencies]
tracing = { path = "../tracing", version = "0.2" }
tracing-mock = { path = "../tracing-mock" }
log = "0.4"
tracing-log = { path = "../tracing-log", version = "0.2" }
criterion = { version = "0.3", default_features = false }
Expand Down
1 change: 0 additions & 1 deletion tracing-subscriber/tests/duplicate_spans.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![cfg(all(feature = "env-filter", feature = "fmt"))]
mod support;
use tracing::{self, collect::with_default, Span};
use tracing_subscriber::{filter::EnvFilter, fmt::Collector};

Expand Down
4 changes: 2 additions & 2 deletions tracing-subscriber/tests/field_filter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(feature = "env-filter")]
mod support;
use self::support::*;

use tracing::{self, collect::with_default, Level};
use tracing_mock::*;
use tracing_subscriber::{filter::EnvFilter, prelude::*};

#[test]
Expand Down
3 changes: 1 addition & 2 deletions tracing-subscriber/tests/filter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![cfg(feature = "env-filter")]

mod support;
use self::support::*;
use tracing::{self, collect::with_default, Level};
use tracing_mock::*;
use tracing_subscriber::{
filter::{EnvFilter, LevelFilter},
prelude::*,
Expand Down
4 changes: 2 additions & 2 deletions tracing-subscriber/tests/filter_log.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(all(feature = "env-filter", feature = "tracing-log"))]
mod support;
use self::support::*;

use tracing::{self, Level};
use tracing_mock::*;
use tracing_subscriber::{filter::EnvFilter, prelude::*};

mod my_module {
Expand Down
3 changes: 1 addition & 2 deletions tracing-subscriber/tests/reload_max_log_level.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![cfg(all(feature = "env-filter", feature = "tracing-log"))]

mod support;
use self::support::*;
use tracing::{self, Level};
use tracing_mock::*;
use tracing_subscriber::{filter::LevelFilter, prelude::*, reload};

#[test]
Expand Down
4 changes: 2 additions & 2 deletions tracing-subscriber/tests/same_len_filters.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// These tests include field filters with no targets, so they have to go in a
// separate file.
#![cfg(feature = "env-filter")]
mod support;
use self::support::*;

use tracing::{self, collect::with_default, Level};
use tracing_mock::*;
use tracing_subscriber::{filter::EnvFilter, prelude::*};

#[test]
Expand Down
8 changes: 0 additions & 8 deletions tracing-subscriber/tests/support.rs

This file was deleted.

Loading

0 comments on commit ced9302

Please sign in to comment.