aprender-test-lib 0.34.0

Probar: Rust-native testing framework with pixel coverage, TUI snapshots, and visual regression
Documentation
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
//! PMAT Bridge for Quality Gate Integration (PROBAR-PMAT-001)
//!
//! Integrates pmat static analysis results into probar compliance checking.
//!
//! ## Rationale
//!
//! Rather than duplicating pmat's capabilities (SATD, complexity, dead code),
//! probar delegates static analysis to pmat and integrates results into its
//! compliance framework.
//!
//! ## Usage
//!
//! ```rust,ignore
//! use jugar_probar::comply::PmatBridge;
//!
//! let bridge = PmatBridge::new();
//! let result = bridge.run_quality_gate(Path::new("src/"))?;
//!
//! println!("SATD violations: {}", result.satd_count);
//! println!("Complexity violations: {}", result.complexity_count);
//! ```

use std::path::Path;
use std::process::Command;

use super::{ComplianceCheck, ComplianceResult};

/// Result of pmat quality gate analysis
#[derive(Debug, Default, Clone)]
pub struct PmatResult {
    /// SATD (Self-Admitted Technical Debt) violations
    pub satd_count: usize,
    /// SATD critical count
    pub satd_critical: usize,
    /// Complexity violations
    pub complexity_count: usize,
    /// Dead code violations
    pub dead_code_count: usize,
    /// Code duplication violations
    pub duplicate_count: usize,
    /// Security violations
    pub security_count: usize,
    /// Test coverage violations
    pub coverage_count: usize,
    /// Documentation violations
    pub documentation_count: usize,
    /// Total violations
    pub total_violations: usize,
    /// Whether quality gate passed
    pub passed: bool,
    /// Raw output from pmat
    pub raw_output: String,
}

impl PmatResult {
    /// Check if there are critical issues
    #[must_use]
    pub fn has_critical(&self) -> bool {
        self.satd_critical > 0 || self.security_count > 0
    }

    /// Get error-level count (critical + security)
    #[must_use]
    pub fn error_count(&self) -> usize {
        self.satd_critical + self.security_count
    }

    /// Get warning-level count
    #[must_use]
    pub fn warning_count(&self) -> usize {
        self.total_violations.saturating_sub(self.error_count())
    }
}

/// Bridge to pmat for quality gate integration
#[derive(Debug, Default)]
pub struct PmatBridge {
    /// Path to pmat binary (defaults to "pmat")
    pmat_path: String,
    /// Additional flags for pmat
    extra_flags: Vec<String>,
}

impl PmatBridge {
    /// Create a new pmat bridge with default settings
    #[must_use]
    pub fn new() -> Self {
        Self {
            pmat_path: "pmat".to_string(),
            extra_flags: Vec::new(),
        }
    }

    /// Set custom pmat binary path
    #[must_use]
    pub fn with_pmat_path(mut self, path: impl Into<String>) -> Self {
        self.pmat_path = path.into();
        self
    }

    /// Add extra flags for pmat
    #[must_use]
    pub fn with_flag(mut self, flag: impl Into<String>) -> Self {
        self.extra_flags.push(flag.into());
        self
    }

    /// Check if pmat is available
    #[must_use]
    pub fn is_available(&self) -> bool {
        Command::new(&self.pmat_path)
            .arg("--version")
            .output()
            .is_ok_and(|o| o.status.success())
    }

    /// Run pmat quality-gate and parse results
    ///
    /// # Arguments
    /// * `path` - Path to analyze
    ///
    /// # Errors
    /// Returns error if pmat is not available or fails to run
    pub fn run_quality_gate(&self, path: &Path) -> Result<PmatResult, String> {
        if !self.is_available() {
            return Err("pmat not found. Install with: cargo install pmat".to_string());
        }

        let mut cmd = Command::new(&self.pmat_path);
        cmd.arg("quality-gate").arg(path);

        for flag in &self.extra_flags {
            cmd.arg(flag);
        }

        let output = cmd
            .output()
            .map_err(|e| format!("Failed to run pmat: {e}"))?;

        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        let combined = format!("{stdout}\n{stderr}");

        Ok(self.parse_output(&combined, output.status.success()))
    }

    /// Parse pmat output into structured result
    fn parse_output(&self, output: &str, success: bool) -> PmatResult {
        let mut result = PmatResult {
            passed: success,
            raw_output: output.to_string(),
            ..Default::default()
        };

        // Parse violation counts from output
        // Format: "  🔍 Checking complexity... 64 violations found"
        for line in output.lines() {
            let line_lower = line.to_lowercase();

            if line_lower.contains("complexity") && line_lower.contains("violations") {
                result.complexity_count = Self::extract_count(line);
            } else if line_lower.contains("dead code") && line_lower.contains("violations") {
                result.dead_code_count = Self::extract_count(line);
            } else if line_lower.contains("technical debt") && line_lower.contains("violations") {
                result.satd_count = Self::extract_count(line);
                // Check for critical count in format "355 violations (17 critical)"
                if let Some(critical_start) = line.find('(') {
                    if let Some(critical_end) = line.find(" critical") {
                        if critical_start < critical_end {
                            let critical_str = &line[critical_start + 1..critical_end];
                            result.satd_critical = critical_str.trim().parse().unwrap_or(0);
                        }
                    }
                }
            } else if line_lower.contains("duplicat") && line_lower.contains("violations") {
                result.duplicate_count = Self::extract_count(line);
            } else if line_lower.contains("security") && line_lower.contains("violations") {
                result.security_count = Self::extract_count(line);
            } else if line_lower.contains("coverage") && line_lower.contains("violations") {
                result.coverage_count = Self::extract_count(line);
            } else if line_lower.contains("documentation") && line_lower.contains("violations") {
                result.documentation_count = Self::extract_count(line);
            } else if line_lower.contains("total violations:") {
                result.total_violations = Self::extract_count(line);
            }
        }

        // If total wasn't explicitly found, sum up
        if result.total_violations == 0 {
            result.total_violations = result.satd_count
                + result.complexity_count
                + result.dead_code_count
                + result.duplicate_count
                + result.security_count
                + result.coverage_count
                + result.documentation_count;
        }

        result
    }

    /// Extract numeric count from a line
    fn extract_count(line: &str) -> usize {
        // Find first number in the line
        let mut num_str = String::new();
        let mut found_digit = false;

        for c in line.chars() {
            if c.is_ascii_digit() {
                num_str.push(c);
                found_digit = true;
            } else if found_digit {
                break;
            }
        }

        num_str.parse().unwrap_or(0)
    }

    /// Convert pmat results to compliance checks
    #[must_use]
    pub fn to_compliance_checks(&self, result: &PmatResult) -> Vec<ComplianceCheck> {
        let mut checks = Vec::new();

        // SATD check
        if result.satd_count == 0 {
            checks.push(ComplianceCheck::pass("PMAT-SATD-001", "SATD Detection"));
        } else if result.satd_critical > 0 {
            checks.push(ComplianceCheck::fail(
                "PMAT-SATD-001",
                "SATD Detection",
                &format!(
                    "{} SATD violations ({} critical)",
                    result.satd_count, result.satd_critical
                ),
                result.satd_count,
            ));
        } else {
            checks.push(ComplianceCheck::warn(
                "PMAT-SATD-001",
                "SATD Detection",
                &format!("{} SATD violations", result.satd_count),
                result.satd_count,
            ));
        }

        // Complexity check
        if result.complexity_count == 0 {
            checks.push(ComplianceCheck::pass(
                "PMAT-COMPLEXITY-001",
                "Complexity Analysis",
            ));
        } else {
            checks.push(ComplianceCheck::warn(
                "PMAT-COMPLEXITY-001",
                "Complexity Analysis",
                &format!("{} complexity violations", result.complexity_count),
                result.complexity_count,
            ));
        }

        // Dead code check
        if result.dead_code_count == 0 {
            checks.push(ComplianceCheck::pass(
                "PMAT-DEADCODE-001",
                "Dead Code Detection",
            ));
        } else {
            checks.push(ComplianceCheck::warn(
                "PMAT-DEADCODE-001",
                "Dead Code Detection",
                &format!("{} dead code violations", result.dead_code_count),
                result.dead_code_count,
            ));
        }

        // Security check
        if result.security_count == 0 {
            checks.push(ComplianceCheck::pass(
                "PMAT-SECURITY-001",
                "Security Analysis",
            ));
        } else {
            checks.push(ComplianceCheck::fail(
                "PMAT-SECURITY-001",
                "Security Analysis",
                &format!("{} security violations", result.security_count),
                result.security_count,
            ));
        }

        // Duplicate check
        if result.duplicate_count == 0 {
            checks.push(ComplianceCheck::pass(
                "PMAT-DUPLICATE-001",
                "Code Duplication",
            ));
        } else {
            checks.push(ComplianceCheck::warn(
                "PMAT-DUPLICATE-001",
                "Code Duplication",
                &format!("{} duplicate code violations", result.duplicate_count),
                result.duplicate_count,
            ));
        }

        checks
    }

    /// Run quality gate and return compliance result
    ///
    /// # Arguments
    /// * `path` - Path to analyze
    ///
    /// # Errors
    /// Returns error if pmat fails
    pub fn check_compliance(&self, path: &Path) -> Result<ComplianceResult, String> {
        let pmat_result = self.run_quality_gate(path)?;
        let checks = self.to_compliance_checks(&pmat_result);

        let mut result = ComplianceResult::new();
        result.files_analyzed = 1; // pmat analyzes directory

        for check in checks {
            result.add_check(check);
        }

        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::comply::ComplianceStatus;

    #[test]
    fn test_parse_output_full() {
        let bridge = PmatBridge::new();
        let output = r#"
🔍 Running quality gate checks...

  🔍 Checking complexity... 64 violations found
  🔍 Checking dead code... 6 violations found
  🔍 Checking technical debt... 355 violations found
  🔍 Checking security... 0 violations found
  🔍 Checking duplicates... 88 violations found
Quality Gate: FAILED
Total violations: 513
"#;

        let result = bridge.parse_output(output, false);

        assert_eq!(result.complexity_count, 64);
        assert_eq!(result.dead_code_count, 6);
        assert_eq!(result.satd_count, 355);
        assert_eq!(result.security_count, 0);
        assert_eq!(result.duplicate_count, 88);
        assert_eq!(result.total_violations, 513);
        assert!(!result.passed);
    }

    #[test]
    fn test_parse_output_passing() {
        let bridge = PmatBridge::new();
        let output = r#"
🔍 Running quality gate checks...

  🔍 Checking complexity... 0 violations found
  🔍 Checking dead code... 0 violations found
  🔍 Checking technical debt... 0 violations found
  🔍 Checking security... 0 violations found
Quality Gate: PASSED
Total violations: 0
"#;

        let result = bridge.parse_output(output, true);

        assert_eq!(result.total_violations, 0);
        assert!(result.passed);
    }

    #[test]
    fn test_extract_count() {
        assert_eq!(PmatBridge::extract_count("64 violations found"), 64);
        assert_eq!(PmatBridge::extract_count("found 123 issues"), 123);
        assert_eq!(PmatBridge::extract_count("no numbers here"), 0);
        assert_eq!(
            PmatBridge::extract_count("355 violations (17 critical)"),
            355
        );
    }

    #[test]
    fn test_to_compliance_checks() {
        let bridge = PmatBridge::new();

        let result = PmatResult {
            satd_count: 10,
            satd_critical: 2,
            complexity_count: 5,
            security_count: 1,
            ..Default::default()
        };

        let checks = bridge.to_compliance_checks(&result);

        // Should have checks for SATD, complexity, dead code, security, duplicates
        assert!(checks.len() >= 4);

        // SATD with critical should be a failure
        let satd_check = checks.iter().find(|c| c.id == "PMAT-SATD-001").unwrap();
        assert_eq!(satd_check.status, ComplianceStatus::Fail);

        // Security violation should be a failure
        let security_check = checks.iter().find(|c| c.id == "PMAT-SECURITY-001").unwrap();
        assert_eq!(security_check.status, ComplianceStatus::Fail);

        // Complexity should be a warning
        let complexity_check = checks
            .iter()
            .find(|c| c.id == "PMAT-COMPLEXITY-001")
            .unwrap();
        assert_eq!(complexity_check.status, ComplianceStatus::Warn);
    }

    #[test]
    fn test_pmat_result_helpers() {
        let result = PmatResult {
            satd_count: 100,
            satd_critical: 5,
            security_count: 2,
            complexity_count: 50,
            total_violations: 157,
            ..Default::default()
        };

        assert!(result.has_critical());
        assert_eq!(result.error_count(), 7); // 5 critical + 2 security
        assert_eq!(result.warning_count(), 150); // 157 - 7
    }

    #[test]
    fn test_builder_pattern() {
        let bridge = PmatBridge::new()
            .with_pmat_path("/custom/pmat")
            .with_flag("--strict");

        assert_eq!(bridge.pmat_path, "/custom/pmat");
        assert!(bridge.extra_flags.contains(&"--strict".to_string()));
    }

    #[test]
    fn test_to_compliance_checks_all_pass() {
        let bridge = PmatBridge::new();
        let result = PmatResult::default(); // all zeros

        let checks = bridge.to_compliance_checks(&result);
        assert_eq!(checks.len(), 5);
        for check in &checks {
            assert_eq!(check.status, ComplianceStatus::Pass);
        }
    }

    #[test]
    fn test_to_compliance_checks_satd_warn_no_critical() {
        let bridge = PmatBridge::new();
        let result = PmatResult {
            satd_count: 5,
            satd_critical: 0, // no critical => warn, not fail
            ..Default::default()
        };

        let checks = bridge.to_compliance_checks(&result);
        let satd = checks.iter().find(|c| c.id == "PMAT-SATD-001").unwrap();
        assert_eq!(satd.status, ComplianceStatus::Warn);
    }

    #[test]
    fn test_to_compliance_checks_dead_code_warn() {
        let bridge = PmatBridge::new();
        let result = PmatResult {
            dead_code_count: 12,
            ..Default::default()
        };

        let checks = bridge.to_compliance_checks(&result);
        let dead = checks.iter().find(|c| c.id == "PMAT-DEADCODE-001").unwrap();
        assert_eq!(dead.status, ComplianceStatus::Warn);
    }

    #[test]
    fn test_to_compliance_checks_duplicates_warn() {
        let bridge = PmatBridge::new();
        let result = PmatResult {
            duplicate_count: 8,
            ..Default::default()
        };

        let checks = bridge.to_compliance_checks(&result);
        let dup = checks
            .iter()
            .find(|c| c.id == "PMAT-DUPLICATE-001")
            .unwrap();
        assert_eq!(dup.status, ComplianceStatus::Warn);
    }
}