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
// Tests for lint hotspot handlers
// Extracted for file health compliance (CB-040)
mod tests {
use super::*;
fn create_test_hotspot_result() -> LintHotspotResult {
LintHotspotResult {
hotspot: LintHotspot {
file: PathBuf::from("src/main.rs"),
defect_density: 0.05,
total_violations: 5,
sloc: 100,
severity_distribution: SeverityDistribution {
error: 2,
warning: 3,
suggestion: 0,
note: 0,
},
top_lints: vec![
("clippy::too_many_arguments".to_string(), 2),
("unused_variable".to_string(), 3),
],
detailed_violations: vec![],
},
all_violations: vec![],
summary_by_file: std::collections::HashMap::new(),
total_project_violations: 5,
enforcement: None,
refactor_chain: None,
quality_gate: QualityGateStatus {
passed: true,
violations: vec![],
blocking: false,
},
}
}
/// Test that enforce flag exits with non-zero status when there are violations
///
/// # Example
///
/// ```
/// use pmat::cli::handlers::lint_hotspot_handlers::should_exit_with_error;
///
/// // With enforce flag and violations - should exit with error
/// let should_exit = should_exit_with_error(true, true, 5);
/// assert!(should_exit);
///
/// // Without enforce flag but quality gate failed - should exit with error
/// let should_exit = should_exit_with_error(false, false, 5);
/// assert!(should_exit);
///
/// // Without enforce flag and no violations - should not exit with error
/// let should_exit = should_exit_with_error(true, false, 0);
/// assert!(!should_exit);
/// ```
fn should_exit_with_error(
quality_gate_passed: bool,
enforce: bool,
total_violations: usize,
) -> bool {
!quality_gate_passed || (enforce && total_violations > 0)
}
#[test]
fn test_enforce_flag_behavior() {
// Test 1: Enforce flag with violations should trigger exit
assert!(should_exit_with_error(true, true, 5));
// Test 2: Enforce flag without violations should not trigger exit
assert!(!should_exit_with_error(true, true, 0));
// Test 3: No enforce flag with violations should not trigger exit
assert!(!should_exit_with_error(true, false, 5));
// Test 4: Quality gate failed should always trigger exit
assert!(should_exit_with_error(false, false, 0));
assert!(should_exit_with_error(false, true, 5));
}
#[test]
fn test_format_summary_with_violations() {
let result = create_test_hotspot_result();
let output = format_summary(&result, false, std::time::Duration::from_secs(1), 10).unwrap();
assert!(output.contains("# Lint Hotspot Analysis"));
assert!(output.contains("**Total Project Violations**: 5"));
assert!(output.contains("## Top Files with Lint Issues"));
assert!(output.contains("## Hottest File Details"));
assert!(output.contains("**File**: src/main.rs"));
}
#[test]
fn test_quality_gate_enforcement_scenario() {
let mut result = create_test_hotspot_result();
// Test case 1: Quality gate passes but enforce flag is set with violations
result.quality_gate.passed = true;
result.total_project_violations = 10;
let should_exit = should_exit_with_error(
result.quality_gate.passed,
true, // enforce flag
result.total_project_violations,
);
assert!(
should_exit,
"Should exit with error when enforce flag is set and violations exist"
);
// Test case 2: Quality gate passes, enforce flag set, no violations
result.total_project_violations = 0;
let should_exit = should_exit_with_error(
result.quality_gate.passed,
true, // enforce flag
result.total_project_violations,
);
assert!(
!should_exit,
"Should not exit with error when enforce flag is set but no violations"
);
}
#[test]
fn test_multiple_enforcement_scenarios() {
// Scenario 1: Quality gate failed, no enforce flag
assert!(should_exit_with_error(false, false, 0));
// Scenario 2: Quality gate passed, enforce flag, violations present
assert!(should_exit_with_error(true, true, 1));
// Scenario 3: Quality gate passed, enforce flag, no violations
assert!(!should_exit_with_error(true, true, 0));
// Scenario 4: Quality gate passed, no enforce flag, violations present
assert!(!should_exit_with_error(true, false, 10));
// Scenario 5: Quality gate failed, enforce flag, violations present
assert!(should_exit_with_error(false, true, 5));
}
}
mod property_tests {
use proptest::prelude::*;
proptest! {
#[test]
fn basic_property_stability(_input in ".*") {
// Basic property test for coverage
prop_assert!(true);
}
#[test]
fn module_consistency_check(_x in 0u32..1000) {
// Module consistency verification
prop_assert!(_x < 1001);
}
}
}