Skip to content

Commit 4cda338

Browse files
authored
mock: document public APIs in subscriber module (#2446)
## 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 `subscriber` module needs documentation and examples. ## Solution This change adds documentation to the `subscriber` module and all the public APIs within it. This includes doctests on all the methods which serve as examples. The `MockSubscriberBuilder::record` method was removed as its functionality is not implemented. Previously, the `MockSubscriber` would verify the scope of an `ExpectedEvent`, even if `in_scope` hadn't been called. In this case, that would check that an event was not in a span if `in_scope` had not been called. `tracing-subscriber` all adhere to this pattern. However it is different to the behavior of all other expectation methods, where an explicit call is needed to expect something, otherwise nothing is checked. As such, the behavior has been modified to align with the rest of the crate. The previous behavior can be achieved by calling `in_scope(None)` to verify that an event has no scope. The documentation for `in_scope` has been updated with an example for this case. The tests in `tracing-subscriber` which previously verified *implicitly* that an event had no scope (by not calling `in_scope` at all) have *not* been modified. It is my opinion that this implicit behavior was never required. Refs: #539
1 parent dd67660 commit 4cda338

File tree

3 files changed

+755
-70
lines changed

3 files changed

+755
-70
lines changed

tracing-mock/src/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ where
231231
Some(Expect::Event(mut expected)) => {
232232
#[cfg(feature = "tracing-subscriber")]
233233
{
234-
if !expected.scope_mut().is_empty() {
234+
if expected.scope_mut().is_some() {
235235
unimplemented!(
236236
"Expected scope for events is not supported with `MockCollector`."
237237
)

tracing-mock/src/event.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::fmt;
4343
pub struct ExpectedEvent {
4444
pub(super) fields: Option<field::ExpectedFields>,
4545
pub(super) parent: Option<Parent>,
46-
pub(super) in_spans: Vec<span::ExpectedSpan>,
46+
pub(super) in_spans: Option<Vec<span::ExpectedSpan>>,
4747
pub(super) metadata: ExpectedMetadata,
4848
}
4949

@@ -458,9 +458,7 @@ impl ExpectedEvent {
458458
///
459459
/// ```
460460
/// use tracing_mock::{expect, subscriber};
461-
/// use tracing_subscriber::{
462-
/// filter::filter_fn, registry, subscribe::CollectExt, util::SubscriberInitExt, Subscribe,
463-
/// };
461+
/// use tracing_subscriber::{subscribe::CollectExt, util::SubscriberInitExt, Subscribe};
464462
///
465463
/// let event = expect::event().in_scope([
466464
/// expect::span().named("parent_span"),
@@ -473,8 +471,8 @@ impl ExpectedEvent {
473471
/// .event(event)
474472
/// .run_with_handle();
475473
///
476-
/// let _collect = registry()
477-
/// .with(subscriber.with_filter(filter_fn(move |_meta| true)))
474+
/// let _collect = tracing_subscriber::registry()
475+
/// .with(subscriber.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
478476
/// .set_default();
479477
///
480478
/// let grandparent = tracing::info_span!("grandparent_span");
@@ -490,9 +488,7 @@ impl ExpectedEvent {
490488
///
491489
/// ```should_panic
492490
/// use tracing_mock::{expect, subscriber};
493-
/// use tracing_subscriber::{
494-
/// filter::filter_fn, registry, subscribe::CollectExt, util::SubscriberInitExt, Subscribe,
495-
/// };
491+
/// use tracing_subscriber::{subscribe::CollectExt, util::SubscriberInitExt, Subscribe};
496492
///
497493
/// let event = expect::event().in_scope([
498494
/// expect::span().named("parent_span"),
@@ -504,8 +500,8 @@ impl ExpectedEvent {
504500
/// .event(event)
505501
/// .run_with_handle();
506502
///
507-
/// let _collect = registry()
508-
/// .with(subscriber.with_filter(filter_fn(move |_meta| true)))
503+
/// let _collect = tracing_subscriber::registry()
504+
/// .with(subscriber.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
509505
/// .set_default();
510506
///
511507
/// let parent = tracing::info_span!("parent_span");
@@ -514,21 +510,48 @@ impl ExpectedEvent {
514510
///
515511
/// handle.assert_finished();
516512
/// ```
513+
///
514+
/// It is also possible to test that an event has no parent spans
515+
/// by passing `None` to `in_scope`. If the event is within a
516+
/// span, the test will fail:
517+
///
518+
/// ```should_panic
519+
/// use tracing_mock::{expect, subscriber};
520+
/// use tracing_subscriber::{subscribe::CollectExt, util::SubscriberInitExt, Subscribe};
521+
///
522+
/// let event = expect::event().in_scope(None);
523+
///
524+
/// let (subscriber, handle) = subscriber::mock()
525+
/// .enter(expect::span())
526+
/// .event(event)
527+
/// .run_with_handle();
528+
///
529+
/// let _collect = tracing_subscriber::registry()
530+
/// .with(subscriber.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
531+
/// .set_default();
532+
///
533+
/// let parent = tracing::info_span!("parent_span");
534+
/// let _guard = parent.enter();
535+
/// tracing::info!(field = &"value");
536+
///
537+
/// handle.assert_finished();
538+
/// ```
539+
///
517540
/// [`MockSubscriber`]: struct@crate::subscriber::MockSubscriber
518541
/// [`MockCollector`]: struct@crate::collector::MockCollector
519542
#[cfg(feature = "tracing-subscriber")]
520543
pub fn in_scope(self, spans: impl IntoIterator<Item = span::ExpectedSpan>) -> Self {
521544
Self {
522-
in_spans: spans.into_iter().collect(),
545+
in_spans: Some(spans.into_iter().collect()),
523546
..self
524547
}
525548
}
526549

527550
/// Provides access to the expected scope (spans) for this expected
528551
/// event.
529552
#[cfg(feature = "tracing-subscriber")]
530-
pub(crate) fn scope_mut(&mut self) -> &mut [span::ExpectedSpan] {
531-
&mut self.in_spans[..]
553+
pub(crate) fn scope_mut(&mut self) -> Option<&mut [span::ExpectedSpan]> {
554+
self.in_spans.as_mut().map(|s| &mut s[..])
532555
}
533556

534557
pub(crate) fn check(
@@ -596,8 +619,8 @@ impl fmt::Debug for ExpectedEvent {
596619
s.field("parent", &format_args!("{:?}", parent));
597620
}
598621

599-
if !self.in_spans.is_empty() {
600-
s.field("in_spans", &self.in_spans);
622+
if let Some(in_spans) = &self.in_spans {
623+
s.field("in_spans", in_spans);
601624
}
602625

603626
s.finish()

0 commit comments

Comments
 (0)