Skip to content

Commit 91fedc1

Browse files
authored
mock: complete API documentation including expect module (#2494)
## Motivation There has been interest around publishing tracing-mock to crates.io for some time. In order to make this possible, documentation and some code clean up is needed. The `expect` module, which contains constructor functions for many of the other `tracing-mock` modules needs documentation and examples. This change adds documentation to the `expect` module and all the public APIs within it. This includes doctests on all the methods which serve as examples. ## Solution The lint for `missing_docs` has been enabled for the entire `tracing-mock` crate! This has been done together with all the other lints that are enabled on the other crates in this project. The `event::msg("message")` constructor was removed, in favor of requiring an explicit construction via `expect::event().with_fields(expect::msg("message"))`. This is appropriate to reduce the API surface that would need to be supported in the future and also because the `event::msg` constructor could be overridden by a subsequent usage of `with_fields`. The shorthand `expect::message()` was renamed to `expect::msg` to make this change less burdensome. The `span::named("name")` constructor was removed, in favor of requiring an explicit construction via `expect::span.with_name("name")`. The latter isn't much longer and since #3097, a string with the name can be passed directly everywhere that an `ExpectedSpan` is required. This change also sets the `missing_docs` lint to warn for the entire `tracing-mock` crate, making it ready to publish (once backported). Refs: #539
1 parent 21c5ce0 commit 91fedc1

File tree

15 files changed

+387
-126
lines changed

15 files changed

+387
-126
lines changed

tracing-mock/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# tracing-mock
66

7-
Utilities for testing [`tracing`][tracing] and crates that uses it.
7+
Utilities for testing [`tracing`] and crates that uses it.
88

99
[![Documentation (master)][docs-master-badge]][docs-master-url]
1010
[![MIT licensed][mit-badge]][mit-url]
@@ -78,7 +78,7 @@ fn yak_shaving() {
7878
}
7979

8080
let (collector, handle) = collector::mock()
81-
.event(expect::event().with_fields(expect::message("preparing to shave yaks")))
81+
.event(expect::event().with_fields(expect::msg("preparing to shave yaks")))
8282
.only()
8383
.run_with_handle();
8484

@@ -128,15 +128,15 @@ let (collector, handle) = collector::mock()
128128
expect::event().with_fields(
129129
expect::field("number_of_yaks")
130130
.with_value(&yak_count)
131-
.and(expect::message("preparing to shave yaks"))
131+
.and(expect::msg("preparing to shave yaks"))
132132
.only(),
133133
),
134134
)
135135
.event(
136136
expect::event().with_fields(
137137
expect::field("all_yaks_shaved")
138138
.with_value(&true)
139-
.and(expect::message("yak shaving completed."))
139+
.and(expect::msg("yak shaving completed."))
140140
.only(),
141141
),
142142
)
@@ -173,4 +173,4 @@ This project is licensed under the [MIT license][mit-url].
173173

174174
Unless you explicitly state otherwise, any contribution intentionally submitted
175175
for inclusion in Tracing by you, shall be licensed as MIT, without any additional
176-
terms or conditions.
176+
terms or conditions.

tracing-mock/src/collector.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//!
1313
//! let (collector, handle) = collector::mock()
1414
//! // Expect a single event with a specified message
15-
//! .event(expect::event().with_fields(expect::message("droids")))
15+
//! .event(expect::event().with_fields(expect::msg("droids")))
1616
//! .only()
1717
//! .run_with_handle();
1818
//!
@@ -40,7 +40,7 @@
4040
//! // Enter a matching span
4141
//! .enter(&span)
4242
//! // Record an event with message "collect parting message"
43-
//! .event(expect::event().with_fields(expect::message("collect parting message")))
43+
//! .event(expect::event().with_fields(expect::msg("collect parting message")))
4444
//! // Record a value for the field `parting` on a matching span
4545
//! .record(&span, expect::field("parting").with_value(&"goodbye world!"))
4646
//! // Exit a matching span
@@ -81,7 +81,7 @@
8181
//! .named("my_span");
8282
//! let (collector, handle) = collector::mock()
8383
//! .enter(&span)
84-
//! .event(expect::event().with_fields(expect::message("collect parting message")))
84+
//! .event(expect::event().with_fields(expect::msg("collect parting message")))
8585
//! .record(&span, expect::field("parting").with_value(&"goodbye world!"))
8686
//! .exit(span)
8787
//! .only()
@@ -137,13 +137,6 @@
137137
//!
138138
//! [`Collect`]: trait@tracing::Collect
139139
//! [`MockCollector`]: struct@crate::collector::MockCollector
140-
use crate::{
141-
ancestry::get_ancestry,
142-
event::ExpectedEvent,
143-
expect::Expect,
144-
field::ExpectedFields,
145-
span::{ActualSpan, ExpectedSpan, NewSpan},
146-
};
147140
use std::{
148141
collections::{HashMap, VecDeque},
149142
sync::{
@@ -152,13 +145,22 @@ use std::{
152145
},
153146
thread,
154147
};
148+
155149
use tracing::{
156150
collect::Interest,
157151
level_filters::LevelFilter,
158152
span::{self, Attributes, Id},
159153
Collect, Event, Metadata,
160154
};
161155

156+
use crate::{
157+
ancestry::get_ancestry,
158+
event::ExpectedEvent,
159+
expect::Expect,
160+
field::ExpectedFields,
161+
span::{ActualSpan, ExpectedSpan, NewSpan},
162+
};
163+
162164
pub(crate) struct SpanState {
163165
id: Id,
164166
name: &'static str,
@@ -188,6 +190,7 @@ struct Running<F: Fn(&Metadata<'_>) -> bool> {
188190
/// for the methods and the [`collector`] module.
189191
///
190192
/// [`collector`]: mod@crate::collector
193+
#[derive(Debug)]
191194
pub struct MockCollector<F: Fn(&Metadata<'_>) -> bool> {
192195
expected: VecDeque<Expect>,
193196
max_level: Option<LevelFilter>,
@@ -204,6 +207,7 @@ pub struct MockCollector<F: Fn(&Metadata<'_>) -> bool> {
204207
/// module documentation.
205208
///
206209
/// [`collector`]: mod@crate::collector
210+
#[derive(Debug)]
207211
pub struct MockHandle(Arc<Mutex<VecDeque<Expect>>>, String);
208212

209213
/// Create a new [`MockCollector`].
@@ -223,7 +227,7 @@ pub struct MockHandle(Arc<Mutex<VecDeque<Expect>>>, String);
223227
/// // Enter a matching span
224228
/// .enter(&span)
225229
/// // Record an event with message "collect parting message"
226-
/// .event(expect::event().with_fields(expect::message("collect parting message")))
230+
/// .event(expect::event().with_fields(expect::msg("collect parting message")))
227231
/// // Record a value for the field `parting` on a matching span
228232
/// .record(&span, expect::field("parting").with_value(&"goodbye world!"))
229233
/// // Exit a matching span

tracing-mock/src/event.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@
2828
//!
2929
//! [`collector`]: mod@crate::collector
3030
//! [`expect::event`]: fn@crate::expect::event
31-
#![allow(missing_docs)]
31+
use std::fmt;
32+
3233
use crate::{
3334
ancestry::{ActualAncestry, ExpectedAncestry},
34-
expect, field,
35+
field,
3536
metadata::ExpectedMetadata,
3637
span,
3738
};
3839

39-
use std::fmt;
40-
4140
/// An expected event.
4241
///
4342
/// For a detailed description and examples, see the documentation for
@@ -52,10 +51,6 @@ pub struct ExpectedEvent {
5251
pub(super) metadata: ExpectedMetadata,
5352
}
5453

55-
pub fn msg(message: impl fmt::Display) -> ExpectedEvent {
56-
expect::event().with_fields(expect::message(message))
57-
}
58-
5954
impl ExpectedEvent {
6055
/// Sets a name to expect when matching an event.
6156
///

tracing-mock/src/expect.rs

Lines changed: 198 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
//! Construct expectations for traces which should be received
2+
//!
3+
//! This module contains constructors for expectations defined
4+
//! in the [`event`], [`span`], and [`field`] modules.
5+
//!
6+
//! # Examples
7+
//!
8+
//! ```
9+
//! use tracing_mock::{collector, expect};
10+
//!
11+
//! let (collector, handle) = collector::mock()
12+
//! // Expect an event with message
13+
//! .event(expect::event().with_fields(expect::msg("message")))
14+
//! .only()
15+
//! .run_with_handle();
16+
//!
17+
//! tracing::collect::with_default(collector, || {
18+
//! tracing::info!("message");
19+
//! });
20+
//!
21+
//! handle.assert_finished();
22+
//! ```
123
use std::fmt;
224

325
use crate::{
@@ -23,12 +45,141 @@ pub(crate) enum Expect {
2345
Nothing,
2446
}
2547

48+
/// Create a new [`ExpectedEvent`].
49+
///
50+
/// For details on how to add additional assertions to the expected
51+
/// event, see the [`event`] module and the [`ExpectedEvent`] struct.
52+
///
53+
/// # Examples
54+
///
55+
/// ```
56+
/// use tracing_mock::{collector, expect};
57+
///
58+
/// let (collector, handle) = collector::mock()
59+
/// .event(expect::event())
60+
/// .run_with_handle();
61+
///
62+
/// tracing::collect::with_default(collector, || {
63+
/// tracing::info!(field.name = "field_value");
64+
/// });
65+
///
66+
/// handle.assert_finished();
67+
/// ```
68+
///
69+
/// If we expect an event and instead record something else, the test
70+
/// will fail:
71+
///
72+
/// ```should_panic
73+
/// use tracing_mock::{collector, expect};
74+
///
75+
/// let (collector, handle) = collector::mock()
76+
/// .event(expect::event())
77+
/// .run_with_handle();
78+
///
79+
/// tracing::collect::with_default(collector, || {
80+
/// let span = tracing::info_span!("span");
81+
/// let _guard = span.enter();
82+
/// });
83+
///
84+
/// handle.assert_finished();
85+
/// ```
2686
pub fn event() -> ExpectedEvent {
2787
ExpectedEvent {
2888
..Default::default()
2989
}
3090
}
3191

92+
/// Construct a new [`ExpectedSpan`].
93+
///
94+
/// For details on how to add additional assertions to the expected
95+
/// span, see the [`span`] module and the [`ExpectedSpan`] and
96+
/// [`NewSpan`] structs.
97+
///
98+
/// # Examples
99+
///
100+
/// ```
101+
/// use tracing_mock::{collector, expect};
102+
///
103+
/// let (collector, handle) = collector::mock()
104+
/// .new_span(expect::span())
105+
/// .enter(expect::span())
106+
/// .run_with_handle();
107+
///
108+
/// tracing::collect::with_default(collector, || {
109+
/// let span = tracing::info_span!("span");
110+
/// let _guard = span.enter();
111+
/// });
112+
///
113+
/// handle.assert_finished();
114+
/// ```
115+
///
116+
/// If we expect to enter a span and instead record something else, the test
117+
/// will fail:
118+
///
119+
/// ```should_panic
120+
/// use tracing_mock::{collector, expect};
121+
///
122+
/// let (collector, handle) = collector::mock()
123+
/// .enter(expect::span())
124+
/// .run_with_handle();
125+
///
126+
/// tracing::collect::with_default(collector, || {
127+
/// tracing::info!(field.name = "field_value");
128+
/// });
129+
///
130+
/// handle.assert_finished();
131+
/// ```
132+
pub fn span() -> ExpectedSpan {
133+
ExpectedSpan {
134+
..Default::default()
135+
}
136+
}
137+
138+
/// Construct a new [`ExpectedField`].
139+
///
140+
/// For details on how to set the value of the expected field and
141+
/// how to expect multiple fields, see the [`field`] module and the
142+
/// [`ExpectedField`] and [`ExpectedFields`] structs.
143+
/// span, see the [`span`] module and the [`ExpectedSpan`] and
144+
/// [`NewSpan`] structs.
145+
///
146+
/// # Examples
147+
///
148+
/// ```
149+
/// use tracing_mock::{collector, expect};
150+
///
151+
/// let event = expect::event()
152+
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
153+
///
154+
/// let (collector, handle) = collector::mock()
155+
/// .event(event)
156+
/// .run_with_handle();
157+
///
158+
/// tracing::collect::with_default(collector, || {
159+
/// tracing::info!(field.name = "field_value");
160+
/// });
161+
///
162+
/// handle.assert_finished();
163+
/// ```
164+
///
165+
/// A different field value will cause the test to fail:
166+
///
167+
/// ```should_panic
168+
/// use tracing_mock::{collector, expect};
169+
///
170+
/// let event = expect::event()
171+
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
172+
///
173+
/// let (collector, handle) = collector::mock()
174+
/// .event(event)
175+
/// .run_with_handle();
176+
///
177+
/// tracing::collect::with_default(collector, || {
178+
/// tracing::info!(field.name = "different_field_value");
179+
/// });
180+
///
181+
/// handle.assert_finished();
182+
/// ```
32183
pub fn field<K>(name: K) -> ExpectedField
33184
where
34185
String: From<K>,
@@ -39,19 +190,59 @@ where
39190
}
40191
}
41192

42-
pub fn message(message: impl fmt::Display) -> ExpectedField {
193+
/// Construct a new message [`ExpectedField`].
194+
///
195+
/// For details on how to set the value of the message field and
196+
/// how to expect multiple fields, see the [`field`] module and the
197+
/// [`ExpectedField`] and [`ExpectedFields`] structs.
198+
///
199+
/// This is equivalent to
200+
/// `expect::field("message").with_value(message)`.
201+
///
202+
/// # Examples
203+
///
204+
/// ```
205+
/// use tracing_mock::{collector, expect};
206+
///
207+
/// let event = expect::event().with_fields(
208+
/// expect::msg("message"));
209+
///
210+
/// let (collector, handle) = collector::mock()
211+
/// .event(event)
212+
/// .run_with_handle();
213+
///
214+
/// tracing::collect::with_default(collector, || {
215+
/// tracing::info!("message");
216+
/// });
217+
///
218+
/// handle.assert_finished();
219+
/// ```
220+
///
221+
/// A different message value will cause the test to fail:
222+
///
223+
/// ```should_panic
224+
/// use tracing_mock::{collector, expect};
225+
///
226+
/// let event = expect::event().with_fields(
227+
/// expect::msg("message"));
228+
///
229+
/// let (collector, handle) = collector::mock()
230+
/// .event(event)
231+
/// .run_with_handle();
232+
///
233+
/// tracing::collect::with_default(collector, || {
234+
/// tracing::info!("different message");
235+
/// });
236+
///
237+
/// handle.assert_finished();
238+
/// ```
239+
pub fn msg(message: impl fmt::Display) -> ExpectedField {
43240
ExpectedField {
44241
name: "message".to_string(),
45242
value: ExpectedValue::Debug(message.to_string()),
46243
}
47244
}
48245

49-
pub fn span() -> ExpectedSpan {
50-
ExpectedSpan {
51-
..Default::default()
52-
}
53-
}
54-
55246
/// Returns a new, unset `ExpectedId`.
56247
///
57248
/// The `ExpectedId` needs to be attached to a [`NewSpan`] or an

0 commit comments

Comments
 (0)