Skip to content

Commit

Permalink
add tests with the None before the layer
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Sep 26, 2022
1 parent 582f3e9 commit abd1030
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions tracing-subscriber/tests/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ fn just_option_none_subscriber() {
assert_eq!(collector.max_level_hint(), Some(LevelFilter::ERROR));
}

/// Test that the `None` max level hint only applies if its the only layer
// Test that the `None` max level hint only applies if its the only layer
#[test]
fn doesnt_override_none() {
fn none_outside_doesnt_override_max_level() {
// None means the other layer takes control
let subscriber = tracing_subscriber::registry()
.with(BasicLayer(None))
Expand Down Expand Up @@ -107,6 +107,58 @@ fn doesnt_override_none() {
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::INFO));
}

// Test that the `None` max level hint only applies if its the only layer
#[test]
fn none_inside_doesnt_override_max_level() {
// None means the other layer takes control
let subscriber = tracing_subscriber::registry()
.with(None::<LevelFilter>)
.with(BasicLayer(None));
assert_eq!(subscriber.max_level_hint(), None);

// The `None`-returning layer still wins
let subscriber = tracing_subscriber::registry()
.with(Some(LevelFilter::ERROR))
.with(BasicLayer(None));
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::ERROR));

// Check that we aren't doing anything truly wrong
let subscriber = tracing_subscriber::registry()
.with(None::<LevelFilter>)
.with(BasicLayer(Some(LevelFilter::DEBUG)));
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG));

// Test that per-subscriber filters aren't affected

// One layer is None so it "wins"
let subscriber = tracing_subscriber::registry()
.with(None::<LevelFilter>.with_filter(LevelFilter::DEBUG))
.with(BasicLayer(None));
assert_eq!(subscriber.max_level_hint(), None);

// The max-levels wins
let subscriber = tracing_subscriber::registry()
.with(None::<LevelFilter>.with_filter(LevelFilter::DEBUG))
.with(BasicLayer(Some(LevelFilter::INFO)));
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG));

// Test filter on the other layer
let subscriber = tracing_subscriber::registry()
.with(None::<LevelFilter>)
.with(BasicLayer(Some(LevelFilter::INFO)).with_filter(LevelFilter::DEBUG));
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG));
let subscriber = tracing_subscriber::registry()
.with(None::<LevelFilter>)
.with(BasicLayer(None).with_filter(LevelFilter::DEBUG));
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG));

// The `OFF` from `None` over overridden.
let subscriber = tracing_subscriber::registry()
.with(None::<LevelFilter>)
.with(BasicLayer(Some(LevelFilter::INFO)));
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::INFO));
}

/// Tests that the logic tested in `doesnt_override_none` works through the reload subscriber
#[test]
fn reload_works_with_none() {
Expand Down

0 comments on commit abd1030

Please sign in to comment.