summaryrefslogtreecommitdiffstats
path: root/vendor/tracing-subscriber/tests/option.rs
blob: c87519c30815cffe8aacea7dcc6c314104716ae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#![cfg(feature = "registry")]
use tracing_core::{subscriber::Interest, LevelFilter, Metadata, Subscriber};
use tracing_subscriber::{layer, prelude::*};

// A basic layer that returns its inner for `max_level_hint`
#[derive(Debug)]
struct BasicLayer(Option<LevelFilter>);
impl<S: Subscriber> tracing_subscriber::Layer<S> for BasicLayer {
    fn register_callsite(&self, _m: &Metadata<'_>) -> Interest {
        Interest::sometimes()
    }

    fn enabled(&self, _m: &Metadata<'_>, _: layer::Context<'_, S>) -> bool {
        true
    }

    fn max_level_hint(&self) -> Option<LevelFilter> {
        self.0
    }
}

// This test is just used to compare to the tests below
#[test]
fn just_layer() {
    let subscriber = tracing_subscriber::registry().with(LevelFilter::INFO);
    assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::INFO));
}

#[test]
fn subscriber_and_option_some_layer() {
    let subscriber = tracing_subscriber::registry()
        .with(LevelFilter::INFO)
        .with(Some(LevelFilter::DEBUG));
    assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG));
}

#[test]
fn subscriber_and_option_none_layer() {
    // None means the other layer takes control
    let subscriber = tracing_subscriber::registry()
        .with(LevelFilter::ERROR)
        .with(None::<LevelFilter>);
    assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::ERROR));
}

#[test]
fn just_option_some_layer() {
    // Just a None means everything is off
    let subscriber = tracing_subscriber::registry().with(None::<LevelFilter>);
    assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF));
}

/// Tests that the logic tested in `doesnt_override_none` works through the reload subscriber
#[test]
fn just_option_none_layer() {
    let subscriber = tracing_subscriber::registry().with(Some(LevelFilter::ERROR));
    assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::ERROR));
}

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

    // The `None`-returning layer still wins
    let subscriber = tracing_subscriber::registry()
        .with(BasicLayer(None))
        .with(Some(LevelFilter::ERROR));
    assert_eq!(
        subscriber.max_level_hint(),
        Some(LevelFilter::ERROR),
        "\n stack: {:#?}",
        subscriber
    );

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

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

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

    // The max-levels wins
    let subscriber = tracing_subscriber::registry()
        .with(BasicLayer(Some(LevelFilter::INFO)))
        .with(None::<LevelFilter>.with_filter(LevelFilter::DEBUG));
    assert_eq!(
        subscriber.max_level_hint(),
        Some(LevelFilter::DEBUG),
        "\n stack: {:#?}",
        subscriber
    );

    // Test filter on the other layer
    let subscriber = tracing_subscriber::registry()
        .with(BasicLayer(Some(LevelFilter::INFO)).with_filter(LevelFilter::DEBUG))
        .with(None::<LevelFilter>);
    assert_eq!(
        subscriber.max_level_hint(),
        Some(LevelFilter::DEBUG),
        "\n stack: {:#?}",
        subscriber
    );
    let subscriber = tracing_subscriber::registry()
        .with(BasicLayer(None).with_filter(LevelFilter::DEBUG))
        .with(None::<LevelFilter>);
    assert_eq!(
        subscriber.max_level_hint(),
        Some(LevelFilter::DEBUG),
        "\n stack: {:#?}",
        subscriber
    );

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

// 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,
        "\n stack: {:#?}",
        subscriber
    );

    // 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),
        "\n stack: {:#?}",
        subscriber
    );

    // 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),
        "\n stack: {:#?}",
        subscriber
    );

    // 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,
        "\n stack: {:#?}",
        subscriber
    );

    // 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),
        "\n stack: {:#?}",
        subscriber
    );

    // 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),
        "\n stack: {:#?}",
        subscriber
    );
    let subscriber = tracing_subscriber::registry()
        .with(None::<LevelFilter>)
        .with(BasicLayer(None).with_filter(LevelFilter::DEBUG));
    assert_eq!(
        subscriber.max_level_hint(),
        Some(LevelFilter::DEBUG),
        "\n stack: {:#?}",
        subscriber
    );

    // 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),
        "\n stack: {:#?}",
        subscriber
    );
}

/// Tests that the logic tested in `doesnt_override_none` works through the reload layer
#[test]
fn reload_works_with_none() {
    let (layer1, handle1) = tracing_subscriber::reload::Layer::new(None::<BasicLayer>);
    let (layer2, _handle2) = tracing_subscriber::reload::Layer::new(None::<BasicLayer>);

    let subscriber = tracing_subscriber::registry().with(layer1).with(layer2);
    assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF));

    // reloading one should pass through correctly.
    handle1.reload(Some(BasicLayer(None))).unwrap();
    assert_eq!(subscriber.max_level_hint(), None);

    // Check pass-through of an actual level as well
    handle1
        .reload(Some(BasicLayer(Some(LevelFilter::DEBUG))))
        .unwrap();
    assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG));
}