Skip to content

Commit

Permalink
chore: port #1532 to master (#1533)
Browse files Browse the repository at this point in the history
* subscriber: add feature flags to tests (#1532)

## Motivation

Currently we can't run

```bash
cargo test -p tracing-subscriber --no-default-features
```

successfully, because there are a bunch of tests for feature flagged
APIs that are always enabled.

## Solution

This commit adds feature flags to the modules and/or individual test
cases that test feature-flagged APIs.

There are still a few doctests that use feature-flagged APIs, and will
fail with `--no-default-features`. These are primarily the examples for
various `Layer::context` methods that rely on `LookupSpan`, and use the
`Registry` type, as it's the only subscriber that *implements*
`LookupSpan`. We could consider changing these examples, either by
removing the actual use of the layers in them, or by changing them to
use a mocked-out version of the registry. However, I think it's nicer to
show how the API would be used in real life. Perhaps we should just run

```bash
cargo test -p tracing-subscriber --no-default-features--tests --lib
```

to ignore doctests when testing without default features.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Co-authored-by: David Barsky <me@davidbarsky.com>
  • Loading branch information
davidbarsky authored Sep 2, 2021
1 parent fe2c841 commit 0be67ad
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tracing-subscriber/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ where
}
}

#[cfg(test)]
#[cfg(all(test, feature = "registry"))]
mod tests {
use crate::{
prelude::*,
Expand Down
3 changes: 2 additions & 1 deletion tracing-subscriber/src/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,6 @@ impl Identity {

#[cfg(test)]
pub(crate) mod tests {
use std::sync::{Arc, Mutex};

use super::*;

Expand Down Expand Up @@ -1354,7 +1353,9 @@ pub(crate) mod tests {
}

#[test]
#[cfg(feature = "registry")]
fn context_event_span() {
use std::sync::{Arc, Mutex};
let last_event_span = Arc::new(Mutex::new(None));

struct RecordingSubscriber {
Expand Down
1 change: 1 addition & 0 deletions tracing-subscriber/tests/duplicate_spans.rs
Original file line number Diff line number Diff line change
@@ -1,3 +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
1 change: 1 addition & 0 deletions tracing-subscriber/tests/field_filter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "env-filter")]
mod support;
use self::support::*;
use tracing::{self, collect::with_default, Level};
Expand Down
2 changes: 2 additions & 0 deletions tracing-subscriber/tests/filter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "env-filter")]

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

#[test]
Expand Down
1 change: 1 addition & 0 deletions tracing-subscriber/tests/registry_max_level_hint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(all(feature = "registry", feature = "fmt"))]
use tracing_subscriber::{filter::LevelFilter, prelude::*};

#[test]
Expand Down
1 change: 1 addition & 0 deletions tracing-subscriber/tests/registry_with_subscriber.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "registry")]
use tracing_futures::{Instrument, WithCollector};
use tracing_subscriber::prelude::*;

Expand Down
1 change: 1 addition & 0 deletions tracing-subscriber/tests/reload.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "reload")]
use std::sync::atomic::{AtomicUsize, Ordering};
use tracing_core::{
collect::Interest,
Expand Down
2 changes: 2 additions & 0 deletions tracing-subscriber/tests/reload_max_log_level.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(all(feature = "env-filter", feature = "tracing-log"))]

mod support;
use self::support::*;
use tracing::{self, Level};
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/tests/same_len_filters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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};
Expand Down
2 changes: 2 additions & 0 deletions tracing-subscriber/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn init_ext_works() {
}

#[test]
#[cfg(feature = "fmt")]
fn builders_are_init_ext() {
tracing_subscriber::fmt().set_default();
let _ = tracing_subscriber::fmt()
Expand All @@ -28,6 +29,7 @@ fn builders_are_init_ext() {
}

#[test]
#[cfg(all(feature = "fmt", feature = "env-filter"))]
fn layered_is_init_ext() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::subscriber())
Expand Down

0 comments on commit 0be67ad

Please sign in to comment.