diff --git a/Cargo.toml b/Cargo.toml index 79b600dbfb..9cdb070c8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,5 +15,6 @@ members = [ "tracing-serde", "tracing-appender", "tracing-journald", + "tracing-test", "examples" ] diff --git a/tracing-attributes/Cargo.toml b/tracing-attributes/Cargo.toml index a7774c4615..714b1f0390 100644 --- a/tracing-attributes/Cargo.toml +++ b/tracing-attributes/Cargo.toml @@ -46,6 +46,7 @@ quote = "1" [dev-dependencies] tracing = { path = "../tracing", version = "0.1" } tracing-futures = { path = "../tracing-futures", version = "0.2" } +tracing-test = { path = "../tracing-test", version = "0.1" } tokio-test = { version = "0.2.0" } tracing-core = { path = "../tracing-core", version = "0.1"} async-trait = "0.1" diff --git a/tracing-attributes/tests/async_fn.rs b/tracing-attributes/tests/async_fn.rs index 7e2465e1cc..8a544244ff 100644 --- a/tracing-attributes/tests/async_fn.rs +++ b/tracing-attributes/tests/async_fn.rs @@ -16,14 +16,14 @@ async fn test_async_fn(polls: usize) -> Result<(), ()> { #[test] fn async_fn_only_enters_for_polls() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("test_async_fn")) - .enter(span::mock().named("test_async_fn")) - .event(event::mock().with_fields(field::mock("awaiting").with_value(&true))) - .exit(span::mock().named("test_async_fn")) - .enter(span::mock().named("test_async_fn")) - .exit(span::mock().named("test_async_fn")) - .drop_span(span::mock().named("test_async_fn")) + let (subscriber, handle) = subscriber::expect() + .new_span(span("test_async_fn")) + .enter(span("test_async_fn")) + .event(event().with_fields(field("awaiting").with_value(&true))) + .exit(span("test_async_fn")) + .enter(span("test_async_fn")) + .exit(span("test_async_fn")) + .drop_span(span("test_async_fn")) .done() .run_with_handle(); with_default(subscriber, || { @@ -44,18 +44,18 @@ fn async_fn_nested() { tracing::trace!(nested = true); } - let span = span::mock().named("test_async_fns_nested"); - let span2 = span::mock().named("test_async_fns_nested_other"); - let (subscriber, handle) = subscriber::mock() - .new_span(span.clone()) - .enter(span.clone()) + let span1 = span("test_async_fns_nested"); + let span2 = span("test_async_fns_nested_other"); + let (subscriber, handle) = subscriber::expect() + .new_span(span1.clone()) + .enter(span1.clone()) .new_span(span2.clone()) .enter(span2.clone()) - .event(event::mock().with_fields(field::mock("nested").with_value(&true))) + .event(event().with_fields(field("nested").with_value(&true))) .exit(span2.clone()) .drop_span(span2) - .exit(span.clone()) - .drop_span(span) + .exit(span1.clone()) + .drop_span(span1) .done() .run_with_handle(); @@ -118,28 +118,29 @@ fn async_fn_with_async_trait() { } } - let span = span::mock().named("foo"); - let span2 = span::mock().named("bar"); - let span3 = span::mock().named("baz"); - let (subscriber, handle) = subscriber::mock() + let span1 = span("foo"); + let span2 = span("bar"); + let span3 = span("baz"); + let (subscriber, handle) = subscriber::expect() .new_span( - span.clone() - .with_field(field::mock("self")) - .with_field(field::mock("v")), + span1 + .clone() + .with_field(field("self")) + .with_field(field("v")), ) - .enter(span.clone()) + .enter(span1.clone()) .new_span(span3.clone()) .enter(span3.clone()) - .event(event::mock().with_fields(field::mock("val").with_value(&2u64))) + .event(event().with_fields(field("val").with_value(&2u64))) .exit(span3.clone()) .drop_span(span3) - .new_span(span2.clone().with_field(field::mock("self"))) + .new_span(span2.clone().with_field(field("self"))) .enter(span2.clone()) - .event(event::mock().with_fields(field::mock("val").with_value(&5u64))) + .event(event().with_fields(field("val").with_value(&5u64))) .exit(span2.clone()) .drop_span(span2) - .exit(span.clone()) - .drop_span(span) + .exit(span1.clone()) + .drop_span(span1) .done() .run_with_handle(); diff --git a/tracing-attributes/tests/destructuring.rs b/tracing-attributes/tests/destructuring.rs index 9d01d415ed..e79d318743 100644 --- a/tracing-attributes/tests/destructuring.rs +++ b/tracing-attributes/tests/destructuring.rs @@ -1,22 +1,20 @@ -mod support; -use support::*; - use tracing::subscriber::with_default; use tracing_attributes::instrument; +use tracing_test::*; #[test] fn destructure_tuples() { #[instrument] fn my_fn((arg1, arg2): (usize, usize)) {} - let span = span::mock().named("my_fn"); + let span = span("my_fn"); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( span.clone().with_field( - field::mock("arg1") + field("arg1") .with_value(&format_args!("1")) - .and(field::mock("arg2").with_value(&format_args!("2"))) + .and(field("arg2").with_value(&format_args!("2"))) .only(), ), ) @@ -38,16 +36,16 @@ fn destructure_nested_tuples() { #[instrument] fn my_fn(((arg1, arg2), (arg3, arg4)): ((usize, usize), (usize, usize))) {} - let span = span::mock().named("my_fn"); + let span = span("my_fn"); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( span.clone().with_field( - field::mock("arg1") + field("arg1") .with_value(&format_args!("1")) - .and(field::mock("arg2").with_value(&format_args!("2"))) - .and(field::mock("arg3").with_value(&format_args!("3"))) - .and(field::mock("arg4").with_value(&format_args!("4"))) + .and(field("arg2").with_value(&format_args!("2"))) + .and(field("arg3").with_value(&format_args!("3"))) + .and(field("arg4").with_value(&format_args!("4"))) .only(), ), ) @@ -69,12 +67,12 @@ fn destructure_refs() { #[instrument] fn my_fn(&arg1: &usize) {} - let span = span::mock().named("my_fn"); + let span = span("my_fn"); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( span.clone() - .with_field(field::mock("arg1").with_value(&format_args!("1")).only()), + .with_field(field("arg1").with_value(&format_args!("1")).only()), ) .enter(span.clone()) .exit(span.clone()) @@ -96,14 +94,14 @@ fn destructure_tuple_structs() { #[instrument] fn my_fn(Foo(arg1, arg2): Foo) {} - let span = span::mock().named("my_fn"); + let span = span("my_fn"); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( span.clone().with_field( - field::mock("arg1") + field("arg1") .with_value(&format_args!("1")) - .and(field::mock("arg2").with_value(&format_args!("2"))) + .and(field("arg2").with_value(&format_args!("2"))) .only(), ), ) @@ -137,14 +135,14 @@ fn destructure_structs() { let _ = (arg1, arg2); } - let span = span::mock().named("my_fn"); + let span = span("my_fn"); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( span.clone().with_field( - field::mock("arg1") + field("arg1") .with_value(&format_args!("1")) - .and(field::mock("arg2").with_value(&format_args!("2"))) + .and(field("arg2").with_value(&format_args!("2"))) .only(), ), ) @@ -182,16 +180,16 @@ fn destructure_everything() { let _ = (arg1, arg2, arg3, arg4); } - let span = span::mock().named("my_fn"); + let span = span("my_fn"); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( span.clone().with_field( - field::mock("arg1") + field("arg1") .with_value(&format_args!("1")) - .and(field::mock("arg2").with_value(&format_args!("2"))) - .and(field::mock("arg3").with_value(&format_args!("3"))) - .and(field::mock("arg4").with_value(&format_args!("4"))) + .and(field("arg2").with_value(&format_args!("2"))) + .and(field("arg3").with_value(&format_args!("3"))) + .and(field("arg4").with_value(&format_args!("4"))) .only(), ), ) diff --git a/tracing-attributes/tests/err.rs b/tracing-attributes/tests/err.rs index eb8b00218d..2f7de9a97f 100644 --- a/tracing-attributes/tests/err.rs +++ b/tracing-attributes/tests/err.rs @@ -18,11 +18,11 @@ fn err() -> Result { #[test] fn test() { - let span = span::mock().named("err"); - let (subscriber, handle) = subscriber::mock() + let span = span("err"); + let (subscriber, handle) = subscriber::expect() .new_span(span.clone()) .enter(span.clone()) - .event(event::mock().at_level(Level::ERROR)) + .event(event().at_level(Level::ERROR)) .exit(span.clone()) .drop_span(span) .done() @@ -41,18 +41,18 @@ async fn err_async(polls: usize) -> Result { #[test] fn test_async() { - let span = span::mock().named("err_async"); - let (subscriber, handle) = subscriber::mock() + let span = span("err_async"); + let (subscriber, handle) = subscriber::expect() .new_span(span.clone()) .enter(span.clone()) .event( - event::mock() - .with_fields(field::mock("awaiting").with_value(&true)) + event() + .with_fields(field("awaiting").with_value(&true)) .at_level(Level::TRACE), ) .exit(span.clone()) .enter(span.clone()) - .event(event::mock().at_level(Level::ERROR)) + .event(event().at_level(Level::ERROR)) .exit(span.clone()) .drop_span(span) .done() diff --git a/tracing-attributes/tests/fields.rs b/tracing-attributes/tests/fields.rs index de038ca7b1..f2287cdb09 100644 --- a/tracing-attributes/tests/fields.rs +++ b/tracing-attributes/tests/fields.rs @@ -1,10 +1,7 @@ -mod support; -use support::*; - -use crate::support::field::mock; -use crate::support::span::NewSpan; use tracing::subscriber::with_default; use tracing_attributes::instrument; +use tracing_test::span::NewSpan; +use tracing_test::*; #[instrument(fields(foo = "bar", dsa = true, num = 1))] fn fn_no_param() {} @@ -45,11 +42,11 @@ impl HasField { #[test] fn fields() { - let span = span::mock().with_field( - mock("foo") + let span = span::new().with_field( + field("foo") .with_value(&"bar") - .and(mock("dsa").with_value(&true)) - .and(mock("num").with_value(&1)) + .and(field("dsa").with_value(&true)) + .and(field("num").with_value(&1)) .only(), ); run_test(span, || { @@ -59,10 +56,10 @@ fn fields() { #[test] fn expr_field() { - let span = span::mock().with_field( - mock("s") + let span = span::new().with_field( + field("s") .with_value(&tracing::field::debug("hello world")) - .and(mock("len").with_value(&"hello world".len())) + .and(field("len").with_value(&"hello world".len())) .only(), ); run_test(span, || { @@ -72,11 +69,11 @@ fn expr_field() { #[test] fn two_expr_fields() { - let span = span::mock().with_field( - mock("s") + let span = span::new().with_field( + field("s") .with_value(&tracing::field::debug("hello world")) - .and(mock("s.len").with_value(&"hello world".len())) - .and(mock("s.is_empty").with_value(&false)) + .and(field("s.len").with_value(&"hello world".len())) + .and(field("s.is_empty").with_value(&false)) .only(), ); run_test(span, || { @@ -86,27 +83,27 @@ fn two_expr_fields() { #[test] fn clashy_expr_field() { - let span = span::mock().with_field( + let span1 = span::new().with_field( // Overriding the `s` field should record `s` as a `Display` value, // rather than as a `Debug` value. - mock("s") + field("s") .with_value(&tracing::field::display("hello world")) - .and(mock("s.len").with_value(&"hello world".len())) + .and(field("s.len").with_value(&"hello world".len())) .only(), ); - run_test(span, || { + run_test(span1, || { fn_clashy_expr_field(&"hello world"); }); - let span = span::mock().with_field(mock("s").with_value(&"s").only()); - run_test(span, || { + let span1 = span::new().with_field(field("s").with_value(&"s").only()); + run_test(span1, || { fn_clashy_expr_field2(&"hello world"); }); } #[test] fn self_expr_field() { - let span = span::mock().with_field(mock("my_field").with_value(&"hello world").only()); + let span = span::new().with_field(field("my_field").with_value(&"hello world").only()); run_test(span, || { let has_field = HasField { my_field: "hello world", @@ -117,10 +114,10 @@ fn self_expr_field() { #[test] fn parameters_with_fields() { - let span = span::mock().with_field( - mock("foo") + let span = span::new().with_field( + field("foo") .with_value(&"bar") - .and(mock("param").with_value(&format_args!("1"))) + .and(field("param").with_value(&format_args!("1"))) .only(), ); run_test(span, || { @@ -130,17 +127,17 @@ fn parameters_with_fields() { #[test] fn empty_field() { - let span = span::mock().with_field(mock("foo").with_value(&"bar").only()); + let span = span::new().with_field(field("foo").with_value(&"bar").only()); run_test(span, || { fn_empty_field(); }); } -fn run_test T, T>(span: NewSpan, fun: F) { - let (subscriber, handle) = subscriber::mock() - .new_span(span) - .enter(span::mock()) - .exit(span::mock()) +fn run_test T, T>(span1: NewSpan, fun: F) { + let (subscriber, handle) = subscriber::expect() + .new_span(span1) + .enter(span::new()) + .exit(span::new()) .done() .run_with_handle(); diff --git a/tracing-attributes/tests/instrument.rs b/tracing-attributes/tests/instrument.rs index d4ebcdba25..1b857eb54f 100644 --- a/tracing-attributes/tests/instrument.rs +++ b/tracing-attributes/tests/instrument.rs @@ -1,9 +1,7 @@ -mod support; -use support::*; - use tracing::subscriber::with_default; use tracing::Level; use tracing_attributes::instrument; +use tracing_test::*; #[test] fn override_everything() { @@ -13,19 +11,17 @@ fn override_everything() { #[instrument(level = "debug", target = "my_target")] fn my_other_fn() {} - let span = span::mock() - .named("my_fn") + let span1 = span("my_fn") .at_level(Level::DEBUG) .with_target("my_target"); - let span2 = span::mock() - .named("my_other_fn") + let span2 = span("my_other_fn") .at_level(Level::DEBUG) .with_target("my_target"); - let (subscriber, handle) = subscriber::mock() - .new_span(span.clone()) - .enter(span.clone()) - .exit(span.clone()) - .drop_span(span) + let (subscriber, handle) = subscriber::expect() + .new_span(span1.clone()) + .enter(span1.clone()) + .exit(span1.clone()) + .drop_span(span1) .new_span(span2.clone()) .enter(span2.clone()) .exit(span2.clone()) @@ -46,32 +42,30 @@ fn fields() { #[instrument(target = "my_target", level = "debug")] fn my_fn(arg1: usize, arg2: bool) {} - let span = span::mock() - .named("my_fn") + let span1 = span("my_fn") .at_level(Level::DEBUG) .with_target("my_target"); - let span2 = span::mock() - .named("my_fn") + let span2 = span("my_fn") .at_level(Level::DEBUG) .with_target("my_target"); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span.clone().with_field( - field::mock("arg1") + span1.clone().with_field( + field("arg1") .with_value(&format_args!("2")) - .and(field::mock("arg2").with_value(&format_args!("false"))) + .and(field("arg2").with_value(&format_args!("false"))) .only(), ), ) - .enter(span.clone()) - .exit(span.clone()) - .drop_span(span) + .enter(span1.clone()) + .exit(span1.clone()) + .drop_span(span1) .new_span( span2.clone().with_field( - field::mock("arg1") + field("arg1") .with_value(&format_args!("3")) - .and(field::mock("arg2").with_value(&format_args!("true"))) + .and(field("arg2").with_value(&format_args!("true"))) .only(), ), ) @@ -96,27 +90,26 @@ fn skip() { #[instrument(target = "my_target", level = "debug", skip(_arg2, _arg3))] fn my_fn(arg1: usize, _arg2: UnDebug, _arg3: UnDebug) {} - let span = span::mock() - .named("my_fn") + let span1 = span("my_fn") .at_level(Level::DEBUG) .with_target("my_target"); - let span2 = span::mock() - .named("my_fn") + let span2 = span("my_fn") .at_level(Level::DEBUG) .with_target("my_target"); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span.clone() - .with_field(field::mock("arg1").with_value(&format_args!("2")).only()), + span1 + .clone() + .with_field(field("arg1").with_value(&format_args!("2")).only()), ) - .enter(span.clone()) - .exit(span.clone()) - .drop_span(span) + .enter(span1.clone()) + .exit(span1.clone()) + .drop_span(span1) .new_span( span2 .clone() - .with_field(field::mock("arg1").with_value(&format_args!("3")).only()), + .with_field(field("arg1").with_value(&format_args!("3")).only()), ) .enter(span2.clone()) .exit(span2.clone()) @@ -144,14 +137,14 @@ fn generics() { { } - let span = span::mock().named("my_fn"); + let span = span("my_fn"); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( span.clone().with_field( - field::mock("arg1") + field("arg1") .with_value(&format_args!("Foo")) - .and(field::mock("arg2").with_value(&format_args!("false"))), + .and(field("arg2").with_value(&format_args!("false"))), ), ) .enter(span.clone()) @@ -177,14 +170,14 @@ fn methods() { fn my_fn(&self, arg1: usize) {} } - let span = span::mock().named("my_fn"); + let span = span("my_fn"); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( span.clone().with_field( - field::mock("self") + field("self") .with_value(&format_args!("Foo")) - .and(field::mock("arg1").with_value(&format_args!("42"))), + .and(field("arg1").with_value(&format_args!("42"))), ), ) .enter(span.clone()) diff --git a/tracing-attributes/tests/levels.rs b/tracing-attributes/tests/levels.rs index d1ce6b53b6..9804d6f026 100644 --- a/tracing-attributes/tests/levels.rs +++ b/tracing-attributes/tests/levels.rs @@ -1,9 +1,7 @@ -mod support; -use support::*; - use tracing::subscriber::with_default; use tracing::Level; use tracing_attributes::instrument; +use tracing_test::*; #[test] fn named_levels() { @@ -21,22 +19,22 @@ fn named_levels() { #[instrument(level = "eRrOr")] fn error() {} - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("trace").at_level(Level::TRACE)) - .enter(span::mock().named("trace").at_level(Level::TRACE)) - .exit(span::mock().named("trace").at_level(Level::TRACE)) - .new_span(span::mock().named("debug").at_level(Level::DEBUG)) - .enter(span::mock().named("debug").at_level(Level::DEBUG)) - .exit(span::mock().named("debug").at_level(Level::DEBUG)) - .new_span(span::mock().named("info").at_level(Level::INFO)) - .enter(span::mock().named("info").at_level(Level::INFO)) - .exit(span::mock().named("info").at_level(Level::INFO)) - .new_span(span::mock().named("warn").at_level(Level::WARN)) - .enter(span::mock().named("warn").at_level(Level::WARN)) - .exit(span::mock().named("warn").at_level(Level::WARN)) - .new_span(span::mock().named("error").at_level(Level::ERROR)) - .enter(span::mock().named("error").at_level(Level::ERROR)) - .exit(span::mock().named("error").at_level(Level::ERROR)) + let (subscriber, handle) = subscriber::expect() + .new_span(span("trace").at_level(Level::TRACE)) + .enter(span("trace").at_level(Level::TRACE)) + .exit(span("trace").at_level(Level::TRACE)) + .new_span(span("debug").at_level(Level::DEBUG)) + .enter(span("debug").at_level(Level::DEBUG)) + .exit(span("debug").at_level(Level::DEBUG)) + .new_span(span("info").at_level(Level::INFO)) + .enter(span("info").at_level(Level::INFO)) + .exit(span("info").at_level(Level::INFO)) + .new_span(span("warn").at_level(Level::WARN)) + .enter(span("warn").at_level(Level::WARN)) + .exit(span("warn").at_level(Level::WARN)) + .new_span(span("error").at_level(Level::ERROR)) + .enter(span("error").at_level(Level::ERROR)) + .exit(span("error").at_level(Level::ERROR)) .done() .run_with_handle(); @@ -67,22 +65,22 @@ fn numeric_levels() { #[instrument(level = 5)] fn error() {} - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("trace").at_level(Level::TRACE)) - .enter(span::mock().named("trace").at_level(Level::TRACE)) - .exit(span::mock().named("trace").at_level(Level::TRACE)) - .new_span(span::mock().named("debug").at_level(Level::DEBUG)) - .enter(span::mock().named("debug").at_level(Level::DEBUG)) - .exit(span::mock().named("debug").at_level(Level::DEBUG)) - .new_span(span::mock().named("info").at_level(Level::INFO)) - .enter(span::mock().named("info").at_level(Level::INFO)) - .exit(span::mock().named("info").at_level(Level::INFO)) - .new_span(span::mock().named("warn").at_level(Level::WARN)) - .enter(span::mock().named("warn").at_level(Level::WARN)) - .exit(span::mock().named("warn").at_level(Level::WARN)) - .new_span(span::mock().named("error").at_level(Level::ERROR)) - .enter(span::mock().named("error").at_level(Level::ERROR)) - .exit(span::mock().named("error").at_level(Level::ERROR)) + let (subscriber, handle) = subscriber::expect() + .new_span(span("trace").at_level(Level::TRACE)) + .enter(span("trace").at_level(Level::TRACE)) + .exit(span("trace").at_level(Level::TRACE)) + .new_span(span("debug").at_level(Level::DEBUG)) + .enter(span("debug").at_level(Level::DEBUG)) + .exit(span("debug").at_level(Level::DEBUG)) + .new_span(span("info").at_level(Level::INFO)) + .enter(span("info").at_level(Level::INFO)) + .exit(span("info").at_level(Level::INFO)) + .new_span(span("warn").at_level(Level::WARN)) + .enter(span("warn").at_level(Level::WARN)) + .exit(span("warn").at_level(Level::WARN)) + .new_span(span("error").at_level(Level::ERROR)) + .enter(span("error").at_level(Level::ERROR)) + .exit(span("error").at_level(Level::ERROR)) .done() .run_with_handle(); diff --git a/tracing-attributes/tests/names.rs b/tracing-attributes/tests/names.rs index 15497784ca..fd0996b3dc 100644 --- a/tracing-attributes/tests/names.rs +++ b/tracing-attributes/tests/names.rs @@ -1,8 +1,6 @@ -mod support; -use support::*; - use tracing::subscriber::with_default; use tracing_attributes::instrument; +use tracing_test::*; #[instrument] fn default_name() {} @@ -18,10 +16,10 @@ fn custom_name_no_equals() {} #[test] fn default_name_test() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("default_name")) - .enter(span::mock().named("default_name")) - .exit(span::mock().named("default_name")) + let (subscriber, handle) = subscriber::expect() + .new_span(span("default_name")) + .enter(span("default_name")) + .exit(span("default_name")) .done() .run_with_handle(); @@ -34,10 +32,10 @@ fn default_name_test() { #[test] fn custom_name_test() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("my_name")) - .enter(span::mock().named("my_name")) - .exit(span::mock().named("my_name")) + let (subscriber, handle) = subscriber::expect() + .new_span(span("my_name")) + .enter(span("my_name")) + .exit(span("my_name")) .done() .run_with_handle(); @@ -50,10 +48,10 @@ fn custom_name_test() { #[test] fn custom_name_no_equals_test() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("my_other_name")) - .enter(span::mock().named("my_other_name")) - .exit(span::mock().named("my_other_name")) + let (subscriber, handle) = subscriber::expect() + .new_span(span("my_other_name")) + .enter(span("my_other_name")) + .exit(span("my_other_name")) .done() .run_with_handle(); diff --git a/tracing-attributes/tests/support.rs b/tracing-attributes/tests/support.rs deleted file mode 100644 index 5514b1d4ad..0000000000 --- a/tracing-attributes/tests/support.rs +++ /dev/null @@ -1,5 +0,0 @@ -#[path = "../../tracing/tests/support/mod.rs"] -// path attribute requires referenced module to have same name so allow module inception here -#[allow(clippy::module_inception)] -mod support; -pub use self::support::*; diff --git a/tracing-attributes/tests/targets.rs b/tracing-attributes/tests/targets.rs index 516a8f4aec..165d5dfa40 100644 --- a/tracing-attributes/tests/targets.rs +++ b/tracing-attributes/tests/targets.rs @@ -1,8 +1,6 @@ -mod support; -use support::*; - use tracing::subscriber::with_default; use tracing_attributes::instrument; +use tracing_test::*; #[instrument] fn default_target() {} @@ -24,37 +22,13 @@ mod my_mod { #[test] fn default_targets() { - let (subscriber, handle) = subscriber::mock() - .new_span( - span::mock() - .named("default_target") - .with_target(module_path!()), - ) - .enter( - span::mock() - .named("default_target") - .with_target(module_path!()), - ) - .exit( - span::mock() - .named("default_target") - .with_target(module_path!()), - ) - .new_span( - span::mock() - .named("default_target") - .with_target(my_mod::MODULE_PATH), - ) - .enter( - span::mock() - .named("default_target") - .with_target(my_mod::MODULE_PATH), - ) - .exit( - span::mock() - .named("default_target") - .with_target(my_mod::MODULE_PATH), - ) + let (subscriber, handle) = subscriber::expect() + .new_span(span("default_target").with_target(module_path!())) + .enter(span("default_target").with_target(module_path!())) + .exit(span("default_target").with_target(module_path!())) + .new_span(span("default_target").with_target(my_mod::MODULE_PATH)) + .enter(span("default_target").with_target(my_mod::MODULE_PATH)) + .exit(span("default_target").with_target(my_mod::MODULE_PATH)) .done() .run_with_handle(); @@ -68,25 +42,13 @@ fn default_targets() { #[test] fn custom_targets() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("custom_target").with_target("my_target")) - .enter(span::mock().named("custom_target").with_target("my_target")) - .exit(span::mock().named("custom_target").with_target("my_target")) - .new_span( - span::mock() - .named("custom_target") - .with_target("my_other_target"), - ) - .enter( - span::mock() - .named("custom_target") - .with_target("my_other_target"), - ) - .exit( - span::mock() - .named("custom_target") - .with_target("my_other_target"), - ) + let (subscriber, handle) = subscriber::expect() + .new_span(span("custom_target").with_target("my_target")) + .enter(span("custom_target").with_target("my_target")) + .exit(span("custom_target").with_target("my_target")) + .new_span(span("custom_target").with_target("my_other_target")) + .enter(span("custom_target").with_target("my_other_target")) + .exit(span("custom_target").with_target("my_other_target")) .done() .run_with_handle(); diff --git a/tracing-futures/Cargo.toml b/tracing-futures/Cargo.toml index d5e2a6a822..0fabee7c35 100644 --- a/tracing-futures/Cargo.toml +++ b/tracing-futures/Cargo.toml @@ -37,6 +37,7 @@ tokio = { version = "0.1", optional = true } tokio = "0.1.22" tokio-test = "0.2" tracing-core = { path = "../tracing-core", version = "0.1.2" } +tracing-test = { path = "../tracing-test", version = "0.1" } [badges] maintenance = { status = "actively-developed" } diff --git a/tracing-futures/src/lib.rs b/tracing-futures/src/lib.rs index 5f3325c3a8..a5799a346c 100644 --- a/tracing-futures/src/lib.rs +++ b/tracing-futures/src/lib.rs @@ -476,17 +476,10 @@ impl WithDispatch { } } -#[cfg(test)] -pub(crate) use self::support as test_support; -// This has to have the same name as the module in `tracing`. -#[path = "../../tracing/tests/support/mod.rs"] -#[cfg(test)] -#[allow(unreachable_pub)] -pub(crate) mod support; - #[cfg(test)] mod tests { - use super::{test_support::*, *}; + use super::*; + use tracing_test::*; #[cfg(feature = "futures-01")] mod futures_01_tests { @@ -538,12 +531,12 @@ mod tests { #[test] fn future_enter_exit_is_reasonable() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); with_default(subscriber, || { @@ -557,12 +550,12 @@ mod tests { #[test] fn future_error_ends_span() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); with_default(subscriber, || { @@ -577,16 +570,16 @@ mod tests { #[test] fn stream_enter_exit_is_reasonable() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .run_with_handle(); with_default(subscriber, || { stream::iter_ok::<_, ()>(&[1, 2, 3]) @@ -600,15 +593,15 @@ mod tests { #[test] fn span_follows_future_onto_threadpool() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("a")) - .enter(span::mock().named("b")) - .exit(span::mock().named("b")) - .enter(span::mock().named("b")) - .exit(span::mock().named("b")) - .drop_span(span::mock().named("b")) - .exit(span::mock().named("a")) - .drop_span(span::mock().named("a")) + let (subscriber, handle) = subscriber::expect() + .enter(span("a")) + .enter(span("b")) + .exit(span("b")) + .enter(span("b")) + .exit(span("b")) + .drop_span(span("b")) + .exit(span("a")) + .drop_span(span("a")) .done() .run_with_handle(); let mut runtime = tokio::runtime::Runtime::new().unwrap(); @@ -638,16 +631,16 @@ mod tests { #[test] fn stream_enter_exit_is_reasonable() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .run_with_handle(); with_default(subscriber, || { stream::iter(&[1, 2, 3]) @@ -661,14 +654,14 @@ mod tests { #[test] fn sink_enter_exit_is_reasonable() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .run_with_handle(); with_default(subscriber, || { sink::drain() diff --git a/tracing-futures/tests/std_future.rs b/tracing-futures/tests/std_future.rs index 5bc402cccb..38c1f4ce95 100644 --- a/tracing-futures/tests/std_future.rs +++ b/tracing-futures/tests/std_future.rs @@ -6,12 +6,12 @@ use tracing_futures::Instrument; #[test] fn enter_exit_is_reasonable() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); with_default(subscriber, || { @@ -23,12 +23,12 @@ fn enter_exit_is_reasonable() { #[test] fn error_ends_span() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); with_default(subscriber, || { diff --git a/tracing-futures/tests/support.rs b/tracing-futures/tests/support.rs index 08bf462728..60df0a878f 100644 --- a/tracing-futures/tests/support.rs +++ b/tracing-futures/tests/support.rs @@ -1,15 +1,10 @@ -// this is the only way to make the path attribute play nice with `in -// crate::support`... -#[allow(clippy::module_inception)] -#[path = "../../tracing/tests/support/mod.rs"] -mod support; use std::{ future::Future, pin::Pin, task::{Context, Poll}, }; -pub use support::*; use tokio_test::task; +pub use tracing_test::*; pub struct PollN { and_return: Option>, diff --git a/tracing-subscriber/Cargo.toml b/tracing-subscriber/Cargo.toml index ac1840efaa..e9131f01cb 100644 --- a/tracing-subscriber/Cargo.toml +++ b/tracing-subscriber/Cargo.toml @@ -61,6 +61,7 @@ log = "0.4" tracing-log = { path = "../tracing-log", version = "0.1" } criterion = { version = "0.3", default_features = false } regex = "1" +tracing-test = { path = "../tracing-test", version = "0.1" } [badges] maintenance = { status = "experimental" } diff --git a/tracing-subscriber/tests/duplicate_spans.rs b/tracing-subscriber/tests/duplicate_spans.rs index 34174d1657..8ff5fd09e2 100644 --- a/tracing-subscriber/tests/duplicate_spans.rs +++ b/tracing-subscriber/tests/duplicate_spans.rs @@ -1,4 +1,3 @@ -mod support; use tracing::{self, subscriber::with_default, Span}; use tracing_subscriber::{filter::EnvFilter, FmtSubscriber}; diff --git a/tracing-subscriber/tests/field_filter.rs b/tracing-subscriber/tests/field_filter.rs index 7c39441739..8e7d97e93e 100644 --- a/tracing-subscriber/tests/field_filter.rs +++ b/tracing-subscriber/tests/field_filter.rs @@ -1,22 +1,13 @@ -mod support; -use self::support::*; use tracing::{self, subscriber::with_default, Level}; use tracing_subscriber::{filter::EnvFilter, prelude::*}; +use tracing_test::*; #[test] fn field_filter_events() { let filter: EnvFilter = "[{thing}]=debug".parse().expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .event( - event::mock() - .at_level(Level::INFO) - .with_fields(field::mock("thing")), - ) - .event( - event::mock() - .at_level(Level::DEBUG) - .with_fields(field::mock("thing")), - ) + let (subscriber, finished) = subscriber::expect() + .event(event().at_level(Level::INFO).with_fields(field("thing"))) + .event(event().at_level(Level::DEBUG).with_fields(field("thing"))) .done() .run_with_handle(); let subscriber = subscriber.with(filter); @@ -37,23 +28,23 @@ fn field_filter_spans() { let filter: EnvFilter = "[{enabled=true}]=debug" .parse() .expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .enter(span::mock().named("span1")) + let (subscriber, finished) = subscriber::expect() + .enter(span("span1")) .event( - event::mock() + event() .at_level(Level::INFO) - .with_fields(field::mock("something")), + .with_fields(field("something")), ) - .exit(span::mock().named("span1")) - .enter(span::mock().named("span2")) - .exit(span::mock().named("span2")) - .enter(span::mock().named("span3")) + .exit(span("span1")) + .enter(span("span2")) + .exit(span("span2")) + .enter(span("span3")) .event( - event::mock() + event() .at_level(Level::DEBUG) - .with_fields(field::mock("something")), + .with_fields(field("something")), ) - .exit(span::mock().named("span3")) + .exit(span("span3")) .done() .run_with_handle(); let subscriber = subscriber.with(filter); @@ -80,16 +71,13 @@ fn record_after_created() { let filter: EnvFilter = "[{enabled=true}]=debug" .parse() .expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .enter(span::mock().named("span")) - .exit(span::mock().named("span")) - .record( - span::mock().named("span"), - field::mock("enabled").with_value(&true), - ) - .enter(span::mock().named("span")) - .event(event::mock().at_level(Level::DEBUG)) - .exit(span::mock().named("span")) + let (subscriber, finished) = subscriber::expect() + .enter(span("span")) + .exit(span("span")) + .record(span("span"), field("enabled").with_value(&true)) + .enter(span("span")) + .event(event().at_level(Level::DEBUG)) + .exit(span("span")) .done() .run_with_handle(); let subscriber = subscriber.with(filter); diff --git a/tracing-subscriber/tests/filter.rs b/tracing-subscriber/tests/filter.rs index 5b6afbb595..ab596c2df2 100644 --- a/tracing-subscriber/tests/filter.rs +++ b/tracing-subscriber/tests/filter.rs @@ -1,18 +1,17 @@ -mod support; -use self::support::*; use tracing::{self, subscriber::with_default, Level}; use tracing_subscriber::{ filter::{EnvFilter, LevelFilter}, prelude::*, }; +use tracing_test::*; #[test] fn level_filter_event() { let filter: EnvFilter = "info".parse().expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .event(event::mock().at_level(Level::INFO)) - .event(event::mock().at_level(Level::WARN)) - .event(event::mock().at_level(Level::ERROR)) + let (subscriber, finished) = subscriber::expect() + .event(event().at_level(Level::INFO)) + .event(event().at_level(Level::WARN)) + .event(event().at_level(Level::ERROR)) .done() .run_with_handle(); let subscriber = subscriber.with(filter); @@ -33,19 +32,9 @@ fn same_name_spans() { let filter: EnvFilter = "[foo{bar}]=trace,[foo{baz}]=trace" .parse() .expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .new_span( - span::mock() - .named("foo") - .at_level(Level::TRACE) - .with_field(field::mock("bar")), - ) - .new_span( - span::mock() - .named("foo") - .at_level(Level::TRACE) - .with_field(field::mock("baz")), - ) + let (subscriber, finished) = subscriber::expect() + .new_span(span("foo").at_level(Level::TRACE).with_field(field("bar"))) + .new_span(span("foo").at_level(Level::TRACE).with_field(field("baz"))) .done() .run_with_handle(); let subscriber = subscriber.with(filter); @@ -60,12 +49,12 @@ fn same_name_spans() { #[test] fn level_filter_event_with_target() { let filter: EnvFilter = "info,stuff=debug".parse().expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .event(event::mock().at_level(Level::INFO)) - .event(event::mock().at_level(Level::DEBUG).with_target("stuff")) - .event(event::mock().at_level(Level::WARN).with_target("stuff")) - .event(event::mock().at_level(Level::ERROR)) - .event(event::mock().at_level(Level::ERROR).with_target("stuff")) + let (subscriber, finished) = subscriber::expect() + .event(event().at_level(Level::INFO)) + .event(event().at_level(Level::DEBUG).with_target("stuff")) + .event(event().at_level(Level::WARN).with_target("stuff")) + .event(event().at_level(Level::ERROR)) + .event(event().at_level(Level::ERROR).with_target("stuff")) .done() .run_with_handle(); let subscriber = subscriber.with(filter); @@ -89,12 +78,12 @@ fn not_order_dependent() { // this test reproduces tokio-rs/tracing#623 let filter: EnvFilter = "stuff=debug,info".parse().expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .event(event::mock().at_level(Level::INFO)) - .event(event::mock().at_level(Level::DEBUG).with_target("stuff")) - .event(event::mock().at_level(Level::WARN).with_target("stuff")) - .event(event::mock().at_level(Level::ERROR)) - .event(event::mock().at_level(Level::ERROR).with_target("stuff")) + let (subscriber, finished) = subscriber::expect() + .event(event().at_level(Level::INFO)) + .event(event().at_level(Level::DEBUG).with_target("stuff")) + .event(event().at_level(Level::WARN).with_target("stuff")) + .event(event().at_level(Level::ERROR)) + .event(event().at_level(Level::ERROR).with_target("stuff")) .done() .run_with_handle(); let subscriber = subscriber.with(filter); @@ -123,9 +112,9 @@ fn add_directive_enables_event() { // overwrite with a more specific directive filter = filter.add_directive("hello=trace".parse().expect("directive should parse")); - let (subscriber, finished) = subscriber::mock() - .event(event::mock().at_level(Level::INFO).with_target("hello")) - .event(event::mock().at_level(Level::TRACE).with_target("hello")) + let (subscriber, finished) = subscriber::expect() + .event(event().at_level(Level::INFO).with_target("hello")) + .event(event().at_level(Level::TRACE).with_target("hello")) .done() .run_with_handle(); let subscriber = subscriber.with(filter); @@ -143,19 +132,19 @@ fn span_name_filter_is_dynamic() { let filter: EnvFilter = "info,[cool_span]=debug" .parse() .expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .event(event::mock().at_level(Level::INFO)) - .enter(span::mock().named("cool_span")) - .event(event::mock().at_level(Level::DEBUG)) - .enter(span::mock().named("uncool_span")) - .event(event::mock().at_level(Level::WARN)) - .event(event::mock().at_level(Level::DEBUG)) - .exit(span::mock().named("uncool_span")) - .exit(span::mock().named("cool_span")) - .enter(span::mock().named("uncool_span")) - .event(event::mock().at_level(Level::WARN)) - .event(event::mock().at_level(Level::ERROR)) - .exit(span::mock().named("uncool_span")) + let (subscriber, finished) = subscriber::expect() + .event(event().at_level(Level::INFO)) + .enter(span("cool_span")) + .event(event().at_level(Level::DEBUG)) + .enter(span("uncool_span")) + .event(event().at_level(Level::WARN)) + .event(event().at_level(Level::DEBUG)) + .exit(span("uncool_span")) + .exit(span("cool_span")) + .enter(span("uncool_span")) + .event(event().at_level(Level::WARN)) + .event(event().at_level(Level::ERROR)) + .exit(span("uncool_span")) .done() .run_with_handle(); let subscriber = subscriber.with(filter); diff --git a/tracing-subscriber/tests/filter_log.rs b/tracing-subscriber/tests/filter_log.rs index 21cbda527a..dfdf390a2f 100644 --- a/tracing-subscriber/tests/filter_log.rs +++ b/tracing-subscriber/tests/filter_log.rs @@ -1,7 +1,6 @@ -mod support; -use self::support::*; use tracing::{self, subscriber::with_default, Level}; use tracing_subscriber::{filter::EnvFilter, prelude::*}; +use tracing_test::*; #[test] fn log_is_enabled() { @@ -19,10 +18,10 @@ fn log_is_enabled() { let filter: EnvFilter = "filter_log::my_module=info" .parse() .expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .event(event::mock().at_level(Level::INFO)) - .event(event::mock().at_level(Level::WARN)) - .event(event::mock().at_level(Level::ERROR)) + let (subscriber, finished) = subscriber::expect() + .event(event().at_level(Level::INFO)) + .event(event().at_level(Level::WARN)) + .event(event().at_level(Level::ERROR)) .done() .run_with_handle(); let subscriber = subscriber.with(filter); diff --git a/tracing-subscriber/tests/same_len_filters.rs b/tracing-subscriber/tests/same_len_filters.rs index 5b85ac7717..78f523da76 100644 --- a/tracing-subscriber/tests/same_len_filters.rs +++ b/tracing-subscriber/tests/same_len_filters.rs @@ -1,17 +1,16 @@ // These tests include field filters with no targets, so they have to go in a // separate file. -mod support; -use self::support::*; use tracing::{self, subscriber::with_default, Level}; use tracing_subscriber::{filter::EnvFilter, prelude::*}; +use tracing_test::*; #[test] fn same_length_targets() { let filter: EnvFilter = "foo=trace,bar=trace".parse().expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .event(event::mock().at_level(Level::TRACE)) - .event(event::mock().at_level(Level::TRACE)) + let (subscriber, finished) = subscriber::expect() + .event(event().at_level(Level::TRACE)) + .event(event().at_level(Level::TRACE)) .done() .run_with_handle(); let subscriber = subscriber.with(filter); @@ -29,17 +28,9 @@ fn same_num_fields_event() { let filter: EnvFilter = "[{foo}]=trace,[{bar}]=trace" .parse() .expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .event( - event::mock() - .at_level(Level::TRACE) - .with_fields(field::mock("foo")), - ) - .event( - event::mock() - .at_level(Level::TRACE) - .with_fields(field::mock("bar")), - ) + let (subscriber, finished) = subscriber::expect() + .event(event().at_level(Level::TRACE).with_fields(field("foo"))) + .event(event().at_level(Level::TRACE).with_fields(field("bar"))) .done() .run_with_handle(); let subscriber = subscriber.with(filter); @@ -56,19 +47,9 @@ fn same_num_fields_and_name_len() { let filter: EnvFilter = "[foo{bar=1}]=trace,[baz{boz=1}]=trace" .parse() .expect("filter should parse"); - let (subscriber, finished) = subscriber::mock() - .new_span( - span::mock() - .named("foo") - .at_level(Level::TRACE) - .with_field(field::mock("bar")), - ) - .new_span( - span::mock() - .named("baz") - .at_level(Level::TRACE) - .with_field(field::mock("boz")), - ) + let (subscriber, finished) = subscriber::expect() + .new_span(span("foo").at_level(Level::TRACE).with_field(field("bar"))) + .new_span(span("baz").at_level(Level::TRACE).with_field(field("boz"))) .done() .run_with_handle(); let subscriber = subscriber.with(filter); diff --git a/tracing-subscriber/tests/support.rs b/tracing-subscriber/tests/support.rs deleted file mode 100644 index a69d95e84d..0000000000 --- a/tracing-subscriber/tests/support.rs +++ /dev/null @@ -1,8 +0,0 @@ -#[cfg(test)] -pub use self::support::*; -// This has to have the same name as the module in `tracing`. -#[path = "../../tracing/tests/support/mod.rs"] -#[cfg(test)] -// path attribute requires referenced module to have same name so allow module inception here -#[allow(clippy::module_inception)] -mod support; diff --git a/tracing-subscriber/tests/utils.rs b/tracing-subscriber/tests/utils.rs index baef27defa..9d85c61c8b 100644 --- a/tracing-subscriber/tests/utils.rs +++ b/tracing-subscriber/tests/utils.rs @@ -1,12 +1,11 @@ -mod support; -use self::support::*; use tracing_subscriber::prelude::*; +use tracing_test::*; #[test] fn init_ext_works() { - let (subscriber, finished) = subscriber::mock() + let (subscriber, finished) = subscriber::expect() .event( - event::mock() + event() .at_level(tracing::Level::INFO) .with_target("init_works"), ) diff --git a/tracing-test/CHANGELOG.md b/tracing-test/CHANGELOG.md new file mode 100644 index 0000000000..9fcff35739 --- /dev/null +++ b/tracing-test/CHANGELOG.md @@ -0,0 +1,3 @@ +# Unreleased + +- Initial release diff --git a/tracing-test/Cargo.toml b/tracing-test/Cargo.toml new file mode 100644 index 0000000000..056bb9efb5 --- /dev/null +++ b/tracing-test/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "tracing-test" +version = "0.1.0" +authors = ["Tokio Contributors "] +edition = "2018" +repository = "https://github.com/tokio-rs/tracing" +homepage = "https://tokio.rs" +description = """ +Provides testing utilities for the tracing ecosystem. +""" +categories = [ + "testing", + "asynchronous", + "no-std", +] +keywords = ["tracing", "testing"] +license = "MIT" +readme = "README.md" + +[features] +default = ["std"] +std = ["tracing/std"] + +[dependencies] +tracing = { path = "../tracing", version = "0.1.2"} +cfg-if = "0.1.10" + +[badges] +maintenance = { status = "actively-maintained" } + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] diff --git a/tracing-test/README.md b/tracing-test/README.md new file mode 100644 index 0000000000..595715ca33 --- /dev/null +++ b/tracing-test/README.md @@ -0,0 +1,43 @@ +# tracing-test + +Testing utilities for the tracing ecosystem + +[![Crates.io][crates-badge]][crates-url] +[![Documentation][docs-badge]][docs-url] +[![Documentation (master)][docs-master-badge]][docs-master-url] +[![MIT licensed][mit-badge]][mit-url] +[![Build Status][actions-badge]][actions-url] +[![Discord chat][discord-badge]][discord-url] + +[Documentation][docs-url] | [Chat][discord-url] + +[crates-badge]: https://img.shields.io/crates/v/tracing.svg +[crates-url]: https://crates.io/crates/tracing/0.1.15 +[docs-badge]: https://docs.rs/tracing/badge.svg +[docs-url]: https://docs.rs/tracing/0.1.15 +[docs-master-badge]: https://img.shields.io/badge/docs-master-blue +[docs-master-url]: https://tracing-rs.netlify.com/tracing +[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg +[mit-url]: LICENSE +[actions-badge]: https://github.com/tokio-rs/tracing/workflows/CI/badge.svg +[actions-url]:https://github.com/tokio-rs/tracing/actions?query=workflow%3ACI +[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white +[discord-url]: https://discord.gg/EeF3cQw + + +## Overview + +`tracing-test` provides testing utilities that are used internally in the +`tracing` codebase and may be useful elsewhere in the `tracing` ecosystem. + +This crate is unstable and does not make any stability guarantees. + +## License + +This project is licensed under the [MIT license](LICENSE). + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in Tokio by you, shall be licensed as MIT, without any additional +terms or conditions. diff --git a/tracing/tests/support/event.rs b/tracing-test/src/event.rs similarity index 93% rename from tracing/tests/support/event.rs rename to tracing-test/src/event.rs index 7033d8a134..c09bbc7b09 100644 --- a/tracing/tests/support/event.rs +++ b/tracing-test/src/event.rs @@ -10,11 +10,11 @@ use std::fmt; #[derive(Debug, Default, Eq, PartialEq)] pub struct MockEvent { pub fields: Option, - pub(in crate::support) parent: Option, + pub(crate) parent: Option, metadata: metadata::Expect, } -pub fn mock() -> MockEvent { +pub fn event() -> MockEvent { MockEvent { ..Default::default() } @@ -78,7 +78,7 @@ impl MockEvent { } } - pub(in crate::support) fn check(&mut self, event: &tracing::Event<'_>) { + pub(crate) fn check(&mut self, event: &tracing::Event<'_>) { let meta = event.metadata(); let name = meta.name(); self.metadata diff --git a/tracing/tests/support/field.rs b/tracing-test/src/field.rs similarity index 99% rename from tracing/tests/support/field.rs rename to tracing-test/src/field.rs index 3667cf08b4..bc79c85a7a 100644 --- a/tracing/tests/support/field.rs +++ b/tracing-test/src/field.rs @@ -29,7 +29,7 @@ pub enum MockValue { Any, } -pub fn mock(name: K) -> MockField +pub fn field(name: K) -> MockField where String: From, { diff --git a/tracing-test/src/lib.rs b/tracing-test/src/lib.rs new file mode 100644 index 0000000000..0f264d3413 --- /dev/null +++ b/tracing-test/src/lib.rs @@ -0,0 +1,87 @@ +//! Testing utilities for `tracing` +//! +//! # Overview +//! +//! `tracing-test` offers mock implementations of `tracing` types such as Spans, Events, +//! and Subscribers. These mocks are intended to be used in testing `tracing`-based crates. +//! +//! # Usage +//! +//! Add the following to your `Cargo.toml`: +//! ```toml +//! tracing-test = "0.1" +//! ``` +//! +//! ## Mock subscriber +//! +//! ```rust +//! # fn docs() { +//! use tracing_test::*; +//! use tracing::{debug, span, Level}; +//! +//! let (subscriber, handle) = subscriber::expect() +//! .enter(span("foo")) +//! .event( +//! event().with_fields(field("message").with_value(&tracing::field::debug( +//! format_args!("hello from my event! yak shaved = {:?}", true), +//! ))), +//! ) +//! .exit(span("foo")) +//! .drop_span(span("foo")) +//! .done() +//! .run_with_handle(); +//! +//! tracing::subscriber::with_default(subscriber, || { +//! let foo = span!(Level::DEBUG, "foo"); +//! foo.in_scope(|| { +//! debug!("hello from my event! yak shaved = {:?}", true); +//! }); +//! }); +//! handle.assert_finished(); +//! # } +//! ``` +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(docsrs, feature(doc_cfg))] +#![doc(html_root_url = "https://docs.rs/tracing-test/0.1.0")] +#![warn( + // missing_docs, + rust_2018_idioms, + unreachable_pub, + bad_style, + const_err, + dead_code, + improper_ctypes, + non_shorthand_field_patterns, + no_mangle_generic_items, + overflowing_literals, + path_statements, + patterns_in_fns_without_body, + private_in_public, + unconditional_recursion, + unused, + unused_allocation, + unused_comparisons, + unused_parens, + while_true +)] + +#[cfg(not(feature = "std"))] +extern crate alloc; + +pub mod event; +pub mod field; +pub mod metadata; +pub mod span; +pub mod subscriber; + +pub use event::event; +pub use field::field; +pub use span::span; + +#[derive(Debug, Eq, PartialEq)] +pub(crate) enum Parent { + ContextualRoot, + Contextual(String), + ExplicitRoot, + Explicit(String), +} diff --git a/tracing/tests/support/metadata.rs b/tracing-test/src/metadata.rs similarity index 94% rename from tracing/tests/support/metadata.rs rename to tracing-test/src/metadata.rs index 2c3606b05e..f648c46141 100644 --- a/tracing/tests/support/metadata.rs +++ b/tracing-test/src/metadata.rs @@ -9,7 +9,7 @@ pub struct Expect { } impl Expect { - pub(in crate::support) fn check(&self, actual: &Metadata<'_>, ctx: fmt::Arguments<'_>) { + pub(crate) fn check(&self, actual: &Metadata<'_>, ctx: fmt::Arguments<'_>) { if let Some(ref expected_name) = self.name { let name = actual.name(); assert!( diff --git a/tracing/tests/support/span.rs b/tracing-test/src/span.rs similarity index 85% rename from tracing/tests/support/span.rs rename to tracing-test/src/span.rs index 023e5b7079..c12e9efd7d 100644 --- a/tracing/tests/support/span.rs +++ b/tracing-test/src/span.rs @@ -8,35 +8,33 @@ use std::fmt; /// `subscriber` module. #[derive(Clone, Debug, Default, Eq, PartialEq)] pub struct MockSpan { - pub(in crate::support) metadata: metadata::Expect, + pub(crate) metadata: metadata::Expect, } #[derive(Debug, Default, Eq, PartialEq)] pub struct NewSpan { - pub(in crate::support) span: MockSpan, - pub(in crate::support) fields: field::Expect, - pub(in crate::support) parent: Option, + pub(crate) span: MockSpan, + pub(crate) fields: field::Expect, + pub(crate) parent: Option, } -pub fn mock() -> MockSpan { +pub fn span(name: I) -> MockSpan +where + I: Into, +{ MockSpan { - ..Default::default() + metadata: metadata::Expect { + name: Some(name.into()), + ..Default::default() + }, } } -impl MockSpan { - pub fn named(self, name: I) -> Self - where - I: Into, - { - Self { - metadata: metadata::Expect { - name: Some(name.into()), - ..self.metadata - }, - } - } +pub fn new() -> MockSpan { + Default::default() +} +impl MockSpan { pub fn at_level(self, level: tracing::Level) -> Self { Self { metadata: metadata::Expect { @@ -97,7 +95,7 @@ impl MockSpan { } } - pub(in crate::support) fn check_metadata(&self, actual: &tracing::Metadata<'_>) { + pub fn check_metadata(&self, actual: &tracing::Metadata<'_>) { self.metadata.check(actual, format_args!("span {}", self)); assert!(actual.is_span(), "expected a span but got {:?}", actual); } diff --git a/tracing/tests/support/subscriber.rs b/tracing-test/src/subscriber.rs similarity index 99% rename from tracing/tests/support/subscriber.rs rename to tracing-test/src/subscriber.rs index e10d363395..2222ab7307 100644 --- a/tracing/tests/support/subscriber.rs +++ b/tracing-test/src/subscriber.rs @@ -50,13 +50,17 @@ pub struct MockSubscriber) -> bool> { pub struct MockHandle(Arc>>); -pub fn mock() -> MockSubscriber) -> bool> { +pub fn expect() -> MockSubscriber) -> bool> { MockSubscriber { expected: VecDeque::new(), filter: (|_: &Metadata<'_>| true) as for<'r, 's> fn(&'r Metadata<'s>) -> _, } } +pub fn expect_nothing() -> impl Subscriber { + expect().done().run() +} + impl MockSubscriber where F: Fn(&Metadata<'_>) -> bool + 'static, diff --git a/tracing/Cargo.toml b/tracing/Cargo.toml index 0f31addc3e..96711c584b 100644 --- a/tracing/Cargo.toml +++ b/tracing/Cargo.toml @@ -36,6 +36,7 @@ cfg-if = "0.1.10" futures = "0.1" criterion = { version = "0.3", default_features = false } log = "0.4" +tracing-test = { path = "../tracing-test", version = "0.1.0" } [features] default = ["std", "attributes"] diff --git a/tracing/tests/event.rs b/tracing/tests/event.rs index 5128090e73..79580b8da2 100644 --- a/tracing/tests/event.rs +++ b/tracing/tests/event.rs @@ -8,27 +8,25 @@ #[macro_use] extern crate tracing; -mod support; - -use self::support::*; use tracing::{ field::{debug, display}, subscriber::with_default, Level, }; +use tracing_test::*; macro_rules! event_without_message { ($name:ident: $e:expr) => { #[test] fn $name() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .event( - event::mock().with_fields( - field::mock("answer") + event().with_fields( + field("answer") .with_value(&42) .and( - field::mock("to_question") + field("to_question") .with_value(&"life, the universe, and everything"), ) .only(), @@ -57,12 +55,15 @@ event_without_message! {nonzeroi32_event_without_message: std::num::NonZeroI32:: #[test] fn event_with_message() { - let (subscriber, handle) = subscriber::mock() - .event(event::mock().with_fields(field::mock("message").with_value( - &tracing::field::debug(format_args!("hello from my event! yak shaved = {:?}", true)), - ))) - .done() - .run_with_handle(); + let (subscriber, handle) = + subscriber::expect() + .event( + event().with_fields(field("message").with_value(&tracing::field::debug( + format_args!("hello from my event! yak shaved = {:?}", true), + ))), + ) + .done() + .run_with_handle(); with_default(subscriber, || { debug!("hello from my event! yak shaved = {:?}", true); @@ -73,14 +74,14 @@ fn event_with_message() { #[test] fn message_without_delims() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .event( - event::mock().with_fields( - field::mock("answer") + event().with_fields( + field("answer") .with_value(&42) - .and(field::mock("question").with_value(&"life, the universe, and everything")) + .and(field("question").with_value(&"life, the universe, and everything")) .and( - field::mock("message").with_value(&tracing::field::debug(format_args!( + field("message").with_value(&tracing::field::debug(format_args!( "hello from my event! tricky? {:?}!", true ))), @@ -101,14 +102,14 @@ fn message_without_delims() { #[test] fn string_message_without_delims() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .event( - event::mock().with_fields( - field::mock("answer") + event().with_fields( + field("answer") .with_value(&42) - .and(field::mock("question").with_value(&"life, the universe, and everything")) + .and(field("question").with_value(&"life, the universe, and everything")) .and( - field::mock("message").with_value(&tracing::field::debug(format_args!( + field("message").with_value(&tracing::field::debug(format_args!( "hello from my event" ))), ) @@ -128,18 +129,18 @@ fn string_message_without_delims() { #[test] fn one_with_everything() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .event( - event::mock() + event() .with_fields( - field::mock("message") + field("message") .with_value(&tracing::field::debug(format_args!( "{:#x} make me one with{what:.>20}", 4_277_009_102u64, what = "everything" ))) - .and(field::mock("foo").with_value(&666)) - .and(field::mock("bar").with_value(&false)) + .and(field("foo").with_value(&666)) + .and(field("bar").with_value(&false)) .only(), ) .at_level(Level::ERROR) @@ -162,10 +163,10 @@ fn one_with_everything() { #[test] fn moved_field() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .event( - event::mock().with_fields( - field::mock("foo") + event().with_fields( + field("foo") .with_value(&display("hello from my event")) .only(), ), @@ -182,12 +183,12 @@ fn moved_field() { #[test] fn dotted_field_name() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .event( - event::mock().with_fields( - field::mock("foo.bar") + event().with_fields( + field("foo.bar") .with_value(&true) - .and(field::mock("foo.baz").with_value(&false)) + .and(field("foo.baz").with_value(&false)) .only(), ), ) @@ -202,10 +203,10 @@ fn dotted_field_name() { #[test] fn borrowed_field() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .event( - event::mock().with_fields( - field::mock("foo") + event().with_fields( + field("foo") .with_value(&display("hello from my event")) .only(), ), @@ -238,16 +239,16 @@ fn move_field_out_of_struct() { x: 3.234, y: -1.223, }; - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .event( - event::mock().with_fields( - field::mock("x") + event().with_fields( + field("x") .with_value(&debug(3.234)) - .and(field::mock("y").with_value(&debug(-1.223))) + .and(field("y").with_value(&debug(-1.223))) .only(), ), ) - .event(event::mock().with_fields(field::mock("position").with_value(&debug(&pos)))) + .event(event().with_fields(field("position").with_value(&debug(&pos)))) .done() .run_with_handle(); @@ -264,14 +265,8 @@ fn move_field_out_of_struct() { #[test] fn display_shorthand() { - let (subscriber, handle) = subscriber::mock() - .event( - event::mock().with_fields( - field::mock("my_field") - .with_value(&display("hello world")) - .only(), - ), - ) + let (subscriber, handle) = subscriber::expect() + .event(event().with_fields(field("my_field").with_value(&display("hello world")).only())) .done() .run_with_handle(); with_default(subscriber, || { @@ -283,14 +278,8 @@ fn display_shorthand() { #[test] fn debug_shorthand() { - let (subscriber, handle) = subscriber::mock() - .event( - event::mock().with_fields( - field::mock("my_field") - .with_value(&debug("hello world")) - .only(), - ), - ) + let (subscriber, handle) = subscriber::expect() + .event(event().with_fields(field("my_field").with_value(&debug("hello world")).only())) .done() .run_with_handle(); with_default(subscriber, || { @@ -302,12 +291,12 @@ fn debug_shorthand() { #[test] fn both_shorthands() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .event( - event::mock().with_fields( - field::mock("display_field") + event().with_fields( + field("display_field") .with_value(&display("hello world")) - .and(field::mock("debug_field").with_value(&debug("hello world"))) + .and(field("debug_field").with_value(&debug("hello world"))) .only(), ), ) @@ -322,9 +311,9 @@ fn both_shorthands() { #[test] fn explicit_child() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("foo")) - .event(event::mock().with_explicit_parent(Some("foo"))) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo")) + .event(event().with_explicit_parent(Some("foo"))) .done() .run_with_handle(); @@ -338,13 +327,13 @@ fn explicit_child() { #[test] fn explicit_child_at_levels() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("foo")) - .event(event::mock().with_explicit_parent(Some("foo"))) - .event(event::mock().with_explicit_parent(Some("foo"))) - .event(event::mock().with_explicit_parent(Some("foo"))) - .event(event::mock().with_explicit_parent(Some("foo"))) - .event(event::mock().with_explicit_parent(Some("foo"))) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo")) + .event(event().with_explicit_parent(Some("foo"))) + .event(event().with_explicit_parent(Some("foo"))) + .event(event().with_explicit_parent(Some("foo"))) + .event(event().with_explicit_parent(Some("foo"))) + .event(event().with_explicit_parent(Some("foo"))) .done() .run_with_handle(); diff --git a/tracing/tests/filter_caching_is_lexically_scoped.rs b/tracing/tests/filter_caching_is_lexically_scoped.rs index db148e658e..be770b111d 100644 --- a/tracing/tests/filter_caching_is_lexically_scoped.rs +++ b/tracing/tests/filter_caching_is_lexically_scoped.rs @@ -9,10 +9,9 @@ extern crate std; #[macro_use] extern crate tracing; -mod support; -use self::support::*; use tracing::Level; +use tracing_test::*; use std::sync::{ atomic::{AtomicUsize, Ordering}, @@ -32,7 +31,7 @@ fn filter_caching_is_lexically_scoped() { let count = Arc::new(AtomicUsize::new(0)); let count2 = count.clone(); - let subscriber = subscriber::mock() + let subscriber = subscriber::expect() .with_filter(move |meta| match meta.name() { "emily" | "frank" => { count2.fetch_add(1, Ordering::Relaxed); diff --git a/tracing/tests/filters_are_not_reevaluated_for_the_same_span.rs b/tracing/tests/filters_are_not_reevaluated_for_the_same_span.rs index 1689f0d9e8..199d21bb16 100644 --- a/tracing/tests/filters_are_not_reevaluated_for_the_same_span.rs +++ b/tracing/tests/filters_are_not_reevaluated_for_the_same_span.rs @@ -8,10 +8,9 @@ extern crate std; #[macro_use] extern crate tracing; -mod support; -use self::support::*; use tracing::Level; +use tracing_test::*; use std::sync::{ atomic::{AtomicUsize, Ordering}, @@ -27,7 +26,7 @@ fn filters_are_not_reevaluated_for_the_same_span() { let alice_count2 = alice_count.clone(); let bob_count2 = bob_count.clone(); - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .with_filter(move |meta| match meta.name() { "alice" => { alice_count2.fetch_add(1, Ordering::Relaxed); diff --git a/tracing/tests/filters_are_reevaluated_for_different_call_sites.rs b/tracing/tests/filters_are_reevaluated_for_different_call_sites.rs index 6cea052535..8ec58b39c1 100644 --- a/tracing/tests/filters_are_reevaluated_for_different_call_sites.rs +++ b/tracing/tests/filters_are_reevaluated_for_different_call_sites.rs @@ -8,10 +8,9 @@ extern crate std; #[macro_use] extern crate tracing; -mod support; -use self::support::*; use tracing::Level; +use tracing_test::*; use std::sync::{ atomic::{AtomicUsize, Ordering}, @@ -27,7 +26,7 @@ fn filters_are_reevaluated_for_different_call_sites() { let charlie_count2 = charlie_count.clone(); let dave_count2 = dave_count.clone(); - let subscriber = subscriber::mock() + let subscriber = subscriber::expect() .with_filter(move |meta| { println!("Filter: {:?}", meta.name()); match meta.name() { diff --git a/tracing/tests/span.rs b/tracing/tests/span.rs index b0cd0ef4b6..bbd8cedfaf 100644 --- a/tracing/tests/span.rs +++ b/tracing/tests/span.rs @@ -5,15 +5,14 @@ #[macro_use] extern crate tracing; -mod support; -use self::support::*; use std::thread; use tracing::{ field::{debug, display}, subscriber::with_default, Level, Span, }; +use tracing_test::*; #[test] fn handles_to_the_same_span_are_equal() { @@ -21,7 +20,7 @@ fn handles_to_the_same_span_are_equal() { // `Subscriber::enabled`, so that the spans will be constructed. We // won't enter any spans in this test, so the subscriber won't actually // expect to see any spans. - with_default(subscriber::mock().run(), || { + with_default(subscriber::expect_nothing(), || { let foo1 = span!(Level::TRACE, "foo"); let foo2 = foo1.clone(); // Two handles that point to the same span are equal. @@ -31,7 +30,7 @@ fn handles_to_the_same_span_are_equal() { #[test] fn handles_to_different_spans_are_not_equal() { - with_default(subscriber::mock().run(), || { + with_default(subscriber::expect_nothing(), || { // Even though these spans have the same name and fields, they will have // differing metadata, since they were created on different lines. let foo1 = span!(Level::TRACE, "foo", bar = 1u64, baz = false); @@ -49,7 +48,7 @@ fn handles_to_different_spans_with_the_same_metadata_are_not_equal() { span!(Level::TRACE, "foo", bar = 1u64, baz = false) } - with_default(subscriber::mock().run(), || { + with_default(subscriber::expect_nothing(), || { let foo1 = make_span(); let foo2 = make_span(); @@ -60,15 +59,15 @@ fn handles_to_different_spans_with_the_same_metadata_are_not_equal() { #[test] fn spans_always_go_to_the_subscriber_that_tagged_them() { - let subscriber1 = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let subscriber1 = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run(); - let subscriber2 = subscriber::mock().run(); + let subscriber2 = subscriber::expect_nothing(); let foo = with_default(subscriber1, || { let foo = span!(Level::TRACE, "foo"); @@ -82,12 +81,12 @@ fn spans_always_go_to_the_subscriber_that_tagged_them() { #[test] fn spans_always_go_to_the_subscriber_that_tagged_them_even_across_threads() { - let subscriber1 = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let subscriber1 = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run(); let foo = with_default(subscriber1, || { @@ -99,7 +98,7 @@ fn spans_always_go_to_the_subscriber_that_tagged_them_even_across_threads() { // Even though we enter subscriber 2's context, the subscriber that // tagged the span should see the enter/exit. thread::spawn(move || { - with_default(subscriber::mock().run(), || { + with_default(subscriber::expect_nothing(), || { foo.in_scope(|| {}); }) }) @@ -109,10 +108,10 @@ fn spans_always_go_to_the_subscriber_that_tagged_them_even_across_threads() { #[test] fn dropping_a_span_calls_drop_span() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); with_default(subscriber, || { @@ -126,11 +125,11 @@ fn dropping_a_span_calls_drop_span() { #[test] fn span_closes_after_event() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .event(event::mock()) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .event(event()) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); with_default(subscriber, || { @@ -144,14 +143,14 @@ fn span_closes_after_event() { #[test] fn new_span_after_event() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .event(event::mock()) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) - .enter(span::mock().named("bar")) - .exit(span::mock().named("bar")) - .drop_span(span::mock().named("bar")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .event(event()) + .exit(span("foo")) + .drop_span(span("foo")) + .enter(span("bar")) + .exit(span("bar")) + .drop_span(span("bar")) .done() .run_with_handle(); with_default(subscriber, || { @@ -166,11 +165,11 @@ fn new_span_after_event() { #[test] fn event_outside_of_span() { - let (subscriber, handle) = subscriber::mock() - .event(event::mock()) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .event(event()) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); with_default(subscriber, || { @@ -183,8 +182,8 @@ fn event_outside_of_span() { #[test] fn cloning_a_span_calls_clone_span() { - let (subscriber, handle) = subscriber::mock() - .clone_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .clone_span(span("foo")) .run_with_handle(); with_default(subscriber, || { let span = span!(Level::TRACE, "foo"); @@ -198,10 +197,10 @@ fn cloning_a_span_calls_clone_span() { #[test] fn drop_span_when_exiting_dispatchers_context() { - let (subscriber, handle) = subscriber::mock() - .clone_span(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .clone_span(span("foo")) + .drop_span(span("foo")) + .drop_span(span("foo")) .run_with_handle(); with_default(subscriber, || { let span = span!(Level::TRACE, "foo"); @@ -214,16 +213,16 @@ fn drop_span_when_exiting_dispatchers_context() { #[test] fn clone_and_drop_span_always_go_to_the_subscriber_that_tagged_the_span() { - let (subscriber1, handle1) = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .clone_span(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber1, handle1) = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .clone_span(span("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) + .drop_span(span("foo")) .run_with_handle(); - let subscriber2 = subscriber::mock().done().run(); + let subscriber2 = subscriber::expect().done().run(); let foo = with_default(subscriber1, || { let foo = span!(Level::TRACE, "foo"); @@ -244,10 +243,10 @@ fn clone_and_drop_span_always_go_to_the_subscriber_that_tagged_the_span() { #[test] fn span_closes_when_exited() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); with_default(subscriber, || { @@ -263,11 +262,11 @@ fn span_closes_when_exited() { #[test] fn enter() { - let (subscriber, handle) = subscriber::mock() - .enter(span::mock().named("foo")) - .event(event::mock()) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .enter(span("foo")) + .event(event()) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); with_default(subscriber, || { @@ -281,17 +280,17 @@ fn enter() { #[test] fn moved_field() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span::mock().named("foo").with_field( - field::mock("bar") + span("foo").with_field( + field("bar") .with_value(&display("hello from my span")) .only(), ), ) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); with_default(subscriber, || { @@ -309,12 +308,8 @@ fn moved_field() { #[test] fn dotted_field_name() { - let (subscriber, handle) = subscriber::mock() - .new_span( - span::mock() - .named("foo") - .with_field(field::mock("fields.bar").with_value(&true).only()), - ) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo").with_field(field("fields.bar").with_value(&true).only())) .done() .run_with_handle(); with_default(subscriber, || { @@ -326,17 +321,17 @@ fn dotted_field_name() { #[test] fn borrowed_field() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span::mock().named("foo").with_field( - field::mock("bar") + span("foo").with_field( + field("bar") .with_value(&display("hello from my span")) .only(), ), ) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); @@ -368,20 +363,16 @@ fn move_field_out_of_struct() { x: 3.234, y: -1.223, }; - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span::mock().named("foo").with_field( - field::mock("x") + span("foo").with_field( + field("x") .with_value(&debug(3.234)) - .and(field::mock("y").with_value(&debug(-1.223))) + .and(field("y").with_value(&debug(-1.223))) .only(), ), ) - .new_span( - span::mock() - .named("bar") - .with_field(field::mock("position").with_value(&debug(&pos)).only()), - ) + .new_span(span("bar").with_field(field("position").with_value(&debug(&pos)).only())) .run_with_handle(); with_default(subscriber, || { @@ -403,20 +394,19 @@ fn move_field_out_of_struct() { /* #[test] fn add_field_after_new_span() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span::mock() - .named("foo") - .with_field(field::mock("bar").with_value(&5) - .and(field::mock("baz").with_value).only()), + span("foo") + .with_field(field("bar").with_value(&5) + .and(field("baz").with_value).only()), ) .record( - span::mock().named("foo"), - field::mock("baz").with_value(&true).only(), + span("foo"), + field("baz").with_value(&true).only(), ) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); @@ -431,19 +421,19 @@ fn add_field_after_new_span() { #[test] fn add_fields_only_after_new_span() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo")) .record( - span::mock().named("foo"), - field::mock("bar").with_value(&5).only(), + span("foo"), + field("bar").with_value(&5).only(), ) .record( - span::mock().named("foo"), - field::mock("baz").with_value(&true).only(), + span("foo"), + field("baz").with_value(&true).only(), ) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); @@ -460,22 +450,19 @@ fn add_fields_only_after_new_span() { #[test] fn record_new_value_for_field() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span::mock().named("foo").with_field( - field::mock("bar") + span("foo").with_field( + field("bar") .with_value(&5) - .and(field::mock("baz").with_value(&false)) + .and(field("baz").with_value(&false)) .only(), ), ) - .record( - span::mock().named("foo"), - field::mock("baz").with_value(&true).only(), - ) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + .record(span("foo"), field("baz").with_value(&true).only()) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); @@ -490,26 +477,20 @@ fn record_new_value_for_field() { #[test] fn record_new_values_for_fields() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span::mock().named("foo").with_field( - field::mock("bar") + span("foo").with_field( + field("bar") .with_value(&4) - .and(field::mock("baz").with_value(&false)) + .and(field("baz").with_value(&false)) .only(), ), ) - .record( - span::mock().named("foo"), - field::mock("bar").with_value(&5).only(), - ) - .record( - span::mock().named("foo"), - field::mock("baz").with_value(&true).only(), - ) - .enter(span::mock().named("foo")) - .exit(span::mock().named("foo")) - .drop_span(span::mock().named("foo")) + .record(span("foo"), field("bar").with_value(&5).only()) + .record(span("foo"), field("baz").with_value(&true).only()) + .enter(span("foo")) + .exit(span("foo")) + .drop_span(span("foo")) .done() .run_with_handle(); @@ -525,13 +506,8 @@ fn record_new_values_for_fields() { #[test] fn new_span_with_target_and_log_level() { - let (subscriber, handle) = subscriber::mock() - .new_span( - span::mock() - .named("foo") - .with_target("app_span") - .at_level(Level::DEBUG), - ) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo").with_target("app_span").at_level(Level::DEBUG)) .done() .run_with_handle(); @@ -544,8 +520,8 @@ fn new_span_with_target_and_log_level() { #[test] fn explicit_root_span_is_root() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("foo").with_explicit_parent(None)) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo").with_explicit_parent(None)) .done() .run_with_handle(); @@ -558,11 +534,11 @@ fn explicit_root_span_is_root() { #[test] fn explicit_root_span_is_root_regardless_of_ctx() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .new_span(span::mock().named("bar").with_explicit_parent(None)) - .exit(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo")) + .enter(span("foo")) + .new_span(span("bar").with_explicit_parent(None)) + .exit(span("foo")) .done() .run_with_handle(); @@ -577,9 +553,9 @@ fn explicit_root_span_is_root_regardless_of_ctx() { #[test] fn explicit_child() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("foo")) - .new_span(span::mock().named("bar").with_explicit_parent(Some("foo"))) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo")) + .new_span(span("bar").with_explicit_parent(Some("foo"))) .done() .run_with_handle(); @@ -593,13 +569,13 @@ fn explicit_child() { #[test] fn explicit_child_at_levels() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("foo")) - .new_span(span::mock().named("a").with_explicit_parent(Some("foo"))) - .new_span(span::mock().named("b").with_explicit_parent(Some("foo"))) - .new_span(span::mock().named("c").with_explicit_parent(Some("foo"))) - .new_span(span::mock().named("d").with_explicit_parent(Some("foo"))) - .new_span(span::mock().named("e").with_explicit_parent(Some("foo"))) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo")) + .new_span(span("a").with_explicit_parent(Some("foo"))) + .new_span(span("b").with_explicit_parent(Some("foo"))) + .new_span(span("c").with_explicit_parent(Some("foo"))) + .new_span(span("d").with_explicit_parent(Some("foo"))) + .new_span(span("e").with_explicit_parent(Some("foo"))) .done() .run_with_handle(); @@ -617,12 +593,12 @@ fn explicit_child_at_levels() { #[test] fn explicit_child_regardless_of_ctx() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("foo")) - .new_span(span::mock().named("bar")) - .enter(span::mock().named("bar")) - .new_span(span::mock().named("baz").with_explicit_parent(Some("foo"))) - .exit(span::mock().named("bar")) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo")) + .new_span(span("bar")) + .enter(span("bar")) + .new_span(span("baz").with_explicit_parent(Some("foo"))) + .exit(span("bar")) .done() .run_with_handle(); @@ -636,8 +612,8 @@ fn explicit_child_regardless_of_ctx() { #[test] fn contextual_root() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("foo").with_contextual_parent(None)) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo").with_contextual_parent(None)) .done() .run_with_handle(); @@ -650,15 +626,11 @@ fn contextual_root() { #[test] fn contextual_child() { - let (subscriber, handle) = subscriber::mock() - .new_span(span::mock().named("foo")) - .enter(span::mock().named("foo")) - .new_span( - span::mock() - .named("bar") - .with_contextual_parent(Some("foo")), - ) - .exit(span::mock().named("foo")) + let (subscriber, handle) = subscriber::expect() + .new_span(span("foo")) + .enter(span("foo")) + .new_span(span("bar").with_contextual_parent(Some("foo"))) + .exit(span("foo")) .done() .run_with_handle(); @@ -673,13 +645,10 @@ fn contextual_child() { #[test] fn display_shorthand() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span::mock().named("my_span").with_field( - field::mock("my_field") - .with_value(&display("hello world")) - .only(), - ), + span("my_span") + .with_field(field("my_field").with_value(&display("hello world")).only()), ) .done() .run_with_handle(); @@ -692,13 +661,9 @@ fn display_shorthand() { #[test] fn debug_shorthand() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span::mock().named("my_span").with_field( - field::mock("my_field") - .with_value(&debug("hello world")) - .only(), - ), + span("my_span").with_field(field("my_field").with_value(&debug("hello world")).only()), ) .done() .run_with_handle(); @@ -711,12 +676,12 @@ fn debug_shorthand() { #[test] fn both_shorthands() { - let (subscriber, handle) = subscriber::mock() + let (subscriber, handle) = subscriber::expect() .new_span( - span::mock().named("my_span").with_field( - field::mock("display_field") + span("my_span").with_field( + field("display_field") .with_value(&display("hello world")) - .and(field::mock("debug_field").with_value(&debug("hello world"))) + .and(field("debug_field").with_value(&debug("hello world"))) .only(), ), ) diff --git a/tracing/tests/support/mod.rs b/tracing/tests/support/mod.rs deleted file mode 100644 index 7900f38c76..0000000000 --- a/tracing/tests/support/mod.rs +++ /dev/null @@ -1,14 +0,0 @@ -#![allow(dead_code)] -pub mod event; -pub mod field; -mod metadata; -pub mod span; -pub mod subscriber; - -#[derive(Debug, Eq, PartialEq)] -pub(in crate::support) enum Parent { - ContextualRoot, - Contextual(String), - ExplicitRoot, - Explicit(String), -}