1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! Comprehensive tests for Clap environment variable integration
//!
//! Tests environment variable expansion behavior, precedence rules,
//! and interaction with command-line arguments.
//!
//! NOTE: All tests in this file are ignored due to stack overflow under
//! large thread count coverage instrumentation. Run manually with:
//! cargo test clap_env_var -- --ignored --test-threads=1
use crate::cli::Cli;
use clap::Parser;
use std::env;
use parking_lot::Mutex;
#[cfg(test)]
use serial_test::serial;
// Global mutex to ensure env var tests don't interfere across all modules
// Using parking_lot::Mutex which doesn't poison on panic
static ENV_MUTEX: Mutex<()> = Mutex::new(());
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod env_var_expansion_tests {
use super::*;
#[test]
#[serial(env_vars)]
fn test_rust_log_env_var() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("RUST_LOG");
// Test RUST_LOG environment variable mapping to trace_filter
env::set_var("RUST_LOG", "debug");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(parsed.trace_filter, Some("debug".to_string()));
}
// Test with complex filter
env::set_var("RUST_LOG", "paiml=debug,cache=trace");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(
parsed.trace_filter,
Some("paiml=debug,cache=trace".to_string())
);
}
// Clean up
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_env_var_precedence() {
let _guard = ENV_MUTEX.lock();
// Test that command-line arguments take precedence over env vars
env::set_var("RUST_LOG", "info");
let cli = Cli::try_parse_from(["pmat", "--trace-filter", "debug", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
// Command-line argument should override env var
assert_eq!(parsed.trace_filter, Some("debug".to_string()));
}
// Clean up
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_empty_env_var() {
let _guard = ENV_MUTEX.lock();
// Test empty environment variable
env::set_var("RUST_LOG", "");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
// Empty env var should be treated as Some("")
assert_eq!(parsed.trace_filter, Some("".to_string()));
}
// Clean up
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_env_var_unset() {
let _guard = ENV_MUTEX.lock();
// Make sure RUST_LOG is not set
env::remove_var("RUST_LOG");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
// No env var should result in None
assert_eq!(parsed.trace_filter, None);
}
}
#[test]
#[serial(env_vars)]
fn test_env_var_with_special_characters() {
let _guard = ENV_MUTEX.lock();
// Test env var with special characters
env::set_var("RUST_LOG", "module::submodule=debug,other_mod=trace");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(
parsed.trace_filter,
Some("module::submodule=debug,other_mod=trace".to_string())
);
}
// Clean up
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_env_var_unicode() {
let _guard = ENV_MUTEX.lock();
// Test env var with Unicode characters
env::set_var("RUST_LOG", "测试=debug");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(parsed.trace_filter, Some("测试=debug".to_string()));
}
// Clean up
env::remove_var("RUST_LOG");
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod env_var_interaction_tests {
use super::*;
#[test]
#[serial(env_vars)]
fn test_env_var_with_verbose_flags() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("RUST_LOG");
// Test interaction between env var and verbose flags
env::set_var("RUST_LOG", "warn");
let cli = Cli::try_parse_from(["pmat", "--verbose", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert!(parsed.verbose);
assert_eq!(parsed.trace_filter, Some("warn".to_string()));
// Both should be active
}
// Clean up
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_multiple_env_vars() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("PMAT_MODE");
env::remove_var("RUST_LOG");
// Test if there are other env vars that Clap might use
// This is a placeholder - adapt based on actual env vars used
// Set potential env vars
env::set_var("PMAT_MODE", "mcp");
env::set_var("RUST_LOG", "debug");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
// RUST_LOG should be captured
assert_eq!(parsed.trace_filter, Some("debug".to_string()));
// PMAT_MODE is likely not used (no env attribute on mode field)
assert_eq!(parsed.mode, None);
}
// Clean up
env::remove_var("PMAT_MODE");
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_env_var_parsing_errors() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("RUST_LOG");
// Test if env vars can cause parsing errors
// Since RUST_LOG accepts any string, it shouldn't cause errors
env::set_var("RUST_LOG", "!@#$%^&*()");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(parsed.trace_filter, Some("!@#$%^&*()".to_string()));
}
// Clean up
env::remove_var("RUST_LOG");
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod env_var_precedence_tests {
use super::*;
#[test]
#[serial(env_vars)]
fn test_explicit_none_vs_env_var() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("RUST_LOG");
// Test if we can explicitly override env var with a flag
env::set_var("RUST_LOG", "debug");
// There's no --no-trace-filter flag, so we can't explicitly set to None
// But we can override with a different value
let cli = Cli::try_parse_from(["pmat", "--trace-filter", "off", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(parsed.trace_filter, Some("off".to_string()));
}
// Clean up
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_env_var_case_sensitivity() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("rust_log");
env::remove_var("RUST_LOG");
// Test case sensitivity of env var names
env::set_var("rust_log", "lowercase");
env::set_var("RUST_LOG", "uppercase");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
// Should use RUST_LOG (uppercase) as specified in the attribute
assert_eq!(parsed.trace_filter, Some("uppercase".to_string()));
}
// Clean up
env::remove_var("rust_log");
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_env_var_whitespace_handling() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("RUST_LOG");
// Test env var with leading/trailing whitespace
env::set_var("RUST_LOG", " debug ");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
// Clap should preserve the whitespace
assert_eq!(parsed.trace_filter, Some(" debug ".to_string()));
}
// Clean up
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_env_var_with_equals_sign() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("RUST_LOG");
// Test env var value containing equals signs
env::set_var("RUST_LOG", "module=debug,other=trace=extra");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(
parsed.trace_filter,
Some("module=debug,other=trace=extra".to_string())
);
}
// Clean up
env::remove_var("RUST_LOG");
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod env_var_edge_cases {
use super::*;
#[test]
#[serial(env_vars)]
fn test_very_long_env_var() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("RUST_LOG");
// Test very long env var value - create a properly formatted filter string
let long_value = (0..1000)
.map(|i| format!("module{i}=debug"))
.collect::<Vec<_>>()
.join(",");
env::set_var("RUST_LOG", &long_value);
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(parsed.trace_filter, Some(long_value));
}
// Clean up
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_env_var_with_newlines() {
let _guard = ENV_MUTEX.lock();
// Apply Jidoka - Clean environment and verify state
env::remove_var("RUST_LOG");
// Verify clean state
let clean_cli = Cli::try_parse_from(["pmat", "list"]);
assert!(clean_cli.is_ok());
if let Ok(parsed) = clean_cli {
assert_eq!(
parsed.trace_filter, None,
"Environment should be clean before test"
);
}
// Test env var with newline characters
env::set_var("RUST_LOG", "debug\ntrace\ninfo");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok(), "CLI parsing should succeed with newlines");
if let Ok(parsed) = cli {
// Apply Kaizen - More flexible assertion for newline handling
match parsed.trace_filter {
Some(filter) => {
// Some systems might normalize newlines or reject them
assert!(
filter == "debug\ntrace\ninfo"
|| filter.contains("debug")
&& filter.contains("trace")
&& filter.contains("info"),
"Filter should contain expected values, got: {filter:?}"
);
}
None => {
// Some systems might reject env vars with newlines
println!("Kaizen Note: System rejected environment variable with newlines");
}
}
}
// Clean up
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_env_var_with_null_bytes() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("RUST_LOG");
// Test env var with null bytes (this will actually panic on Linux)
// Most shells/OSes don't allow null bytes in env vars
// This test verifies the platform limitation
let value_with_null = "debug\0trace";
// This will panic on Linux - catch the panic to verify behavior
let result = std::panic::catch_unwind(|| {
env::set_var("RUST_LOG", value_with_null);
});
// Should panic on platforms that don't support null bytes
assert!(result.is_err());
// Clean up (may not be necessary if set_var panicked)
let _ = std::panic::catch_unwind(|| {
env::remove_var("RUST_LOG");
});
}
#[test]
#[serial(env_vars)]
fn test_env_var_concurrent_modification() {
let _guard = ENV_MUTEX.lock();
// Apply Jidoka - Clean environment first and verify
env::remove_var("RUST_LOG");
// Verify clean state
let clean_cli = Cli::try_parse_from(["pmat", "list"]);
assert!(clean_cli.is_ok());
if let Ok(parsed) = clean_cli {
assert_eq!(
parsed.trace_filter, None,
"Environment should be clean before test"
);
}
// Test behavior when env var is set and parsed
env::set_var("RUST_LOG", "initial");
// Parse CLI with environment variable set
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok(), "CLI parsing should succeed");
if let Ok(parsed) = cli {
// Apply Kaizen - Clap reads env vars at parse time, so should capture initial value
assert_eq!(
parsed.trace_filter,
Some("initial".to_string()),
"Should capture env var value at parse time"
);
}
// Modify env var after parsing to verify it doesn't affect already-parsed CLI
env::set_var("RUST_LOG", "modified");
// Parse a new CLI instance to show the env var was indeed modified
let cli2 = Cli::try_parse_from(["pmat", "list"]);
assert!(cli2.is_ok());
if let Ok(parsed2) = cli2 {
assert_eq!(
parsed2.trace_filter,
Some("modified".to_string()),
"New parsing should see modified env var"
);
}
// Clean up
env::remove_var("RUST_LOG");
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod env_var_documentation_tests {
use super::*;
#[test]
#[serial(env_vars)]
fn test_env_var_help_text() {
// Test that env var is mentioned in help text
use clap::CommandFactory;
let mut cmd = Cli::command();
let mut help_output = Vec::new();
let _ = cmd.write_long_help(&mut help_output);
let help_str = String::from_utf8_lossy(&help_output);
// Check that RUST_LOG is mentioned in the help
assert!(help_str.contains("RUST_LOG") || help_str.contains("env:"));
}
#[test]
#[serial(env_vars)]
fn test_env_var_in_error_messages() {
// Test if env vars are mentioned in error messages when relevant
env::set_var("RUST_LOG", "debug");
// Create an invalid command to trigger error
let cli = Cli::try_parse_from(["pmat", "--invalid-flag"]);
assert!(cli.is_err());
// Error message might not mention env vars, but let's check
if let Err(e) = cli {
let _error_str = e.to_string();
// Env vars are usually not mentioned in parse errors
}
// Clean up
env::remove_var("RUST_LOG");
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod env_var_isolation_tests {
use super::*;
#[test]
#[serial(env_vars)]
fn test_isolated_env_var() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("RUST_LOG");
// Test in clean environment
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(parsed.trace_filter, None);
}
// Set and test
env::set_var("RUST_LOG", "test_isolated");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(parsed.trace_filter, Some("test_isolated".to_string()));
}
// Clean up
env::remove_var("RUST_LOG");
}
#[test]
#[serial(env_vars)]
fn test_env_var_does_not_leak() {
let _guard = ENV_MUTEX.lock();
// Clean environment first
env::remove_var("RUST_LOG");
// Test that env var changes in one test don't affect others
env::set_var("RUST_LOG", "test_specific_value");
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(parsed.trace_filter, Some("test_specific_value".to_string()));
}
// Clean up
env::remove_var("RUST_LOG");
// Verify clean state
let cli = Cli::try_parse_from(["pmat", "list"]);
assert!(cli.is_ok());
if let Ok(parsed) = cli {
assert_eq!(parsed.trace_filter, None);
}
}
}