Skip to content

Commit 8f0f9ec

Browse files
committed
tracing: generate macro tests
There are a large number of combinations of field types which currently are not matched by the level event macros (e.g. `info!`). Some recent examples from #3407 is the following that doesn't work: ```rust info!(name: "order.received.ok", order.id = 123, "order received"); ``` However, the corresponding `event!` macro does work: ```rust event!(name: "order.received.ok", Level::INFO, order.id = 123, "order received"); ``` And other variants also work: ```rust // Without `name:` directive info!(order.id = 123, "order received"); // With another field before the dotted one info!(name: "order.received.ok", foo = true, order.id = 123, "order received"); ``` Many such cases have been fixed in the past (#2983, #2883, #2879). However, this has been a bit like wack-a-mole, where we keep finding issues and then fixing those issues, adding extra tests for them and then going on our way. Since the complexity is often in combinations (as above, only when using the `name:` directive together with a dotted field name on the first field), it would be useful to have some extensive tests that attempt to cover all possible combinations. It turns out that there are **a lot** of combiantions. This change adds an `xtask` that generates tests for event macros (span macros are out of scope for this change) similar to the ones found in `tracing/tests/macros.rs`. Which is to say, tests containing macros which should compile, but don't need to run. Owing to the large number of combinations, the tests are split into test files by macro (e.g. `event!`, `info!`) and directive combination (e.g. no directives, just `name:`, `name:` and `target:`). The tests are kept in a separate crate outside the main workspace to avoid rust-analyzer trying to parse the files - as they are quite large. Specifically, there are 1220 macro invocations per test file. When run against the unmodified macros in `tracing`, there were 504 `info!` macro invocations which failed to compile, out of a total of 9760 (including all combinations of directives). The test files are generated (no need to check them in) and then validated by `cargo check` on CI. The CI job has not been made blocking because none of the errors have been fixed yet!
1 parent cc44064 commit 8f0f9ec

File tree

8 files changed

+740
-24
lines changed

8 files changed

+740
-24
lines changed

.github/workflows/CI.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,20 @@ jobs:
314314
- name: "Test tracing-mock with all features enabled"
315315
run: cargo test --all-features -p tracing-mock
316316

317+
check-tracing-macros-stable:
318+
# Feature flag tests that run on stable Rust.
319+
name: cargo check (tracing macros)
320+
needs: check
321+
runs-on: ubuntu-latest
322+
steps:
323+
- uses: actions/checkout@v4
324+
- uses: dtolnay/rust-toolchain@stable
325+
- name: "Generate macro tests"
326+
run: cargo run --bin xtask -- gen-macro-tests
327+
- name: "Test static max level"
328+
run: cargo check --tests
329+
working-directory: "tracing/test_macros"
330+
317331
# all required checks except for the main test run (which we only require
318332
# specific matrix combinations from)
319333
all_required:
@@ -326,5 +340,6 @@ jobs:
326340
- test-build-wasm
327341
- test-wasm
328342
- test-features-stable
343+
- check-tracing-macros-stable
329344
steps:
330345
- run: exit 0

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ members = [
1616
"tracing-test",
1717
"tracing-appender",
1818
"tracing-journald",
19-
"examples"
19+
"examples",
20+
"xtask",
2021
]
2122

2223
# This will be ignored with Rust older than 1.74, but for now that's okay;

tracing/test-macros/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[workspace]
2+
3+
[package]
4+
name = "test-macros"
5+
version = "0.1.0"
6+
publish = false
7+
edition = "2018"
8+
9+
[dependencies]
10+
tracing = { path = ".." }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

tracing/tests/macro_imports.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

xtask/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "xtask"
3+
version = "0.1.0"
4+
edition = "2018"
5+
publish = false
6+
7+
[dependencies]
8+
clap = { version = "4.5", features = ["derive"] }
9+
10+
[lints]
11+
workspace = true

0 commit comments

Comments
 (0)