Skip to content

Commit cf7260e

Browse files
committed
feat: implement testing log
1 parent da70a0a commit cf7260e

File tree

7 files changed

+234
-35
lines changed

7 files changed

+234
-35
lines changed

.claude/ref/standards/logging.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
3. **Choose appropriate levels**: error for failures, debug for detailed flow
88
4. **Include context**: What operation, which file/module, relevant values
99
5. **NEVER log in hot paths**: No logs inside tight loops
10+
6. **Use test macros in test code**: `test_debug!`, `test_info!`, `test_error!`, `test_warn!`
1011

1112
## Log Level Guidelines
1213

@@ -15,6 +16,13 @@
1516
- `info!`: High-level operations (1-2 per command max)
1617
- `debug!`: Detailed flow (Git commands, transformations, RON dumps - everything)
1718

19+
### Test Code Logging
20+
21+
- `test_error!`: Test failures and setup errors
22+
- `test_warn!`: Test warnings (slow operations, skipped tests)
23+
- `test_info!`: Test progress and important milestones
24+
- `test_debug!`: Detailed test flow and intermediate results
25+
1826
## Usage
1927

2028
```bash
@@ -36,10 +44,21 @@ RUST_LOG=trace zerv version
3644

3745
# Test debugging
3846
RUST_LOG=debug cargo test test_name
47+
48+
# Test-only logs (using custom macros)
49+
RUST_LOG=zerv_test=info cargo test test_name
50+
51+
# Test logs + specific source modules
52+
RUST_LOG=zerv_test=info,zerv::cli::flow=debug cargo test test_name
53+
54+
# Show all logs (test + source)
55+
RUST_LOG=debug cargo test test_name
3956
```
4057

4158
## Examples
4259

60+
### Source Code Logging
61+
4362
```rust
4463
// ✅ GOOD - Appropriate level and context
4564
tracing::debug!("Loading schema: {}", schema_name);
@@ -51,3 +70,46 @@ tracing::info!("Calling function xyz"); // Too verbose for info
5170
// ❌ BAD - Missing context
5271
tracing::error!("Parse failed"); // What failed? Where? Why?
5372
```
73+
74+
### Test Code Logging
75+
76+
```rust
77+
use crate::test_utils::logging::{test_debug, test_info, test_error, test_warn};
78+
79+
#[test]
80+
fn test_version_generation() {
81+
test_debug!("Starting test: version generation");
82+
83+
let fixture = GitRepoFixture::tagged("v1.0.0")
84+
.expect("Failed to create test fixture");
85+
test_info!("Created test fixture at: {}", fixture.path().display());
86+
87+
let result = run_pipeline(&fixture);
88+
if let Err(e) = result {
89+
test_error!("Pipeline failed: {}", e);
90+
return;
91+
}
92+
93+
test_debug!("Test completed successfully");
94+
}
95+
96+
// ✅ GOOD - Test-specific logging with context
97+
test_debug!("Flow pipeline output ({}): {}", format_name, output);
98+
test_info!("Test setup completed for branch: {}", branch_name);
99+
100+
// ❌ BAD - Using source code logging in tests
101+
tracing::debug!("Test message"); // Mixes with source code logs
102+
```
103+
104+
### Test Log Filtering Examples
105+
106+
```bash
107+
# Show only test logs (no source code noise)
108+
RUST_LOG=zerv_test=debug cargo test
109+
110+
# Show test info + source debug
111+
RUST_LOG=zerv_test=info,zerv::vcs=debug cargo test
112+
113+
# Show all logs when debugging
114+
RUST_LOG=debug cargo test
115+
```

Cargo.lock

Lines changed: 58 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ path = "src/main.rs"
3131

3232
[dependencies]
3333
chrono = "^0.4.41"
34-
clap = { version = "^4.4", features = ["derive"] }
34+
clap = { version = "^4.5", features = ["derive"] }
3535
handlebars = "^6.3"
3636
indexmap = { version = "^2.12", features = ["serde"] }
3737
libc = "^0.2"
@@ -46,6 +46,8 @@ tracing = "^0.1"
4646
tracing-subscriber = { version = "^0.3", features = ["env-filter"] }
4747

4848
[dev-dependencies]
49+
ctor = "^0.6"
50+
dotenvy = "^0.15"
4951
rstest = "^0.26.0"
5052
serial_test = "^3.0"
5153
shlex = "^1.3"

src/cli/flow/pipeline.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub fn run_flow_pipeline(args: FlowArgs) -> Result<String, ZervError> {
5656
#[cfg(test)]
5757
mod tests {
5858
use super::*;
59+
use crate::test_info;
5960
use crate::test_utils::{
6061
GitRepoFixture,
6162
assert_version_expectation,
@@ -93,12 +94,14 @@ mod tests {
9394

9495
assert_version_expectation(expectation, &output);
9596

96-
println!("Flow pipeline output ({}): {}", format_name, output);
97+
test_info!("Flow pipeline output ({}): {}", format_name, output);
9798
}
9899
}
99100

100101
#[test]
101102
fn test_trunk_based_development_flow() {
103+
test_info!("Starting trunk-based development flow test");
104+
102105
if !should_run_docker_tests() {
103106
return; // Skip when `ZERV_TEST_DOCKER` are disabled
104107
}

src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,23 @@ pub mod test_utils;
99
pub mod utils;
1010
pub mod vcs;
1111
pub mod version;
12+
13+
#[cfg(test)]
14+
mod test_setup {
15+
use tracing_subscriber::{
16+
EnvFilter,
17+
fmt,
18+
};
19+
20+
#[ctor::ctor]
21+
fn init_test_logging() {
22+
let _ = dotenvy::dotenv().ok();
23+
24+
let _ = fmt()
25+
.with_writer(std::io::stderr)
26+
.with_env_filter(EnvFilter::from_default_env())
27+
.with_target(true)
28+
.compact()
29+
.try_init();
30+
}
31+
}

0 commit comments

Comments
 (0)