|
| 1 | +//! 质量门禁实际执行验证。 |
| 2 | +//! |
| 3 | +//! 验证 delivery_decision=deny 拦截违规、allow 放行干净代码。 |
| 4 | +
|
| 5 | +use std::process::Command; |
| 6 | + |
| 7 | +fn ensure_clean_dir(name: &str) -> std::path::PathBuf { |
| 8 | + let tmp = std::env::temp_dir().join(name); |
| 9 | + let _ = std::fs::remove_dir_all(&tmp); |
| 10 | + std::fs::create_dir_all(&tmp).unwrap(); |
| 11 | + tmp |
| 12 | +} |
| 13 | + |
| 14 | +/// 违规代码应触发 deny。 |
| 15 | +#[test] |
| 16 | +fn violation_triggers_deny() { |
| 17 | + let root = ensure_clean_dir("codeguard-gate-deny"); |
| 18 | + // 有语法错误的 Java 代码 |
| 19 | + std::fs::write(root.join("Bad.java"), "class Bad { int f( { return 1; } }\n").unwrap(); |
| 20 | + |
| 21 | + let output = Command::new(env!("CARGO_BIN_EXE_codeguard")) |
| 22 | + .args(["check", "all", root.to_str().unwrap(), "--format=json"]) |
| 23 | + .output() |
| 24 | + .expect("无法运行 codeguard check all"); |
| 25 | + |
| 26 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 27 | + let report: serde_json::Value = serde_json::from_str(&stdout).expect("JSON 解析失败"); |
| 28 | + |
| 29 | + let delivery = report["delivery_decision"].as_str().unwrap_or(""); |
| 30 | + // 有违规时应为 deny 或 incomplete(不为 allow) |
| 31 | + assert_ne!(delivery, "allow", "有违规时不应签发 allow"); |
| 32 | + println!("违规 delivery_decision: {delivery}"); |
| 33 | + |
| 34 | + let _ = std::fs::remove_dir_all(&root); |
| 35 | +} |
| 36 | + |
| 37 | +/// 干净代码应触发 allow(当所有检查完成时)。 |
| 38 | +#[test] |
| 39 | +fn clean_code_triggers_allow_when_complete() { |
| 40 | + let root = ensure_clean_dir("codeguard-gate-allow"); |
| 41 | + std::fs::write(root.join("Clean.java"), "class Clean { int x = 1; }\n").unwrap(); |
| 42 | + |
| 43 | + let output = Command::new(env!("CARGO_BIN_EXE_codeguard")) |
| 44 | + .args(["check", "all", root.to_str().unwrap(), "--format=json"]) |
| 45 | + .output() |
| 46 | + .expect("无法运行 codeguard check all"); |
| 47 | + |
| 48 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 49 | + let report: serde_json::Value = serde_json::from_str(&stdout).expect("JSON 解析失败"); |
| 50 | + |
| 51 | + let delivery = report["delivery_decision"].as_str().unwrap_or(""); |
| 52 | + // 干净代码不应为 deny |
| 53 | + assert_ne!(delivery, "deny", "干净代码不应签发 deny"); |
| 54 | + println!("干净 delivery_decision: {delivery}"); |
| 55 | + |
| 56 | + let _ = std::fs::remove_dir_all(&root); |
| 57 | +} |
| 58 | + |
| 59 | +/// 门禁决策应与 unresolved_conditions 一致。 |
| 60 | +#[test] |
| 61 | +fn gate_decision_consistent_with_unresolved() { |
| 62 | + let root = ensure_clean_dir("codeguard-gate-consistent"); |
| 63 | + std::fs::write(root.join("Test.java"), "class Test { int x = 1; }\n").unwrap(); |
| 64 | + |
| 65 | + let output = Command::new(env!("CARGO_BIN_EXE_codeguard")) |
| 66 | + .args(["check", "all", root.to_str().unwrap(), "--format=json"]) |
| 67 | + .output() |
| 68 | + .expect("无法运行 codeguard check all"); |
| 69 | + |
| 70 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 71 | + let report: serde_json::Value = serde_json::from_str(&stdout).expect("JSON 解析失败"); |
| 72 | + |
| 73 | + let delivery = report["delivery_decision"].as_str().unwrap_or(""); |
| 74 | + let unresolved = report["unresolved_conditions"] |
| 75 | + .as_array() |
| 76 | + .cloned() |
| 77 | + .unwrap_or_default(); |
| 78 | + |
| 79 | + // 有 unresolved 时不应为 allow |
| 80 | + if !unresolved.is_empty() { |
| 81 | + assert_ne!(delivery, "allow", "有 unresolved 时不应签发 allow"); |
| 82 | + } |
| 83 | + |
| 84 | + // 无 unresolved 时不应为 deny(除非有 confirmed findings) |
| 85 | + if unresolved.is_empty() { |
| 86 | + let confirmed: usize = report["syntax_candidates"]["observations"] |
| 87 | + .as_array() |
| 88 | + .map(|arr| { |
| 89 | + arr.iter() |
| 90 | + .filter(|obs| { |
| 91 | + let status = obs["status"].as_str().unwrap_or(""); |
| 92 | + status == "confirmed" || status == "violation" |
| 93 | + }) |
| 94 | + .count() |
| 95 | + }) |
| 96 | + .unwrap_or(0); |
| 97 | + if confirmed == 0 { |
| 98 | + assert_ne!(delivery, "deny", "无发现且无 unresolved 时不应签发 deny"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + println!("一致 delivery_decision: {delivery}, unresolved: {}", unresolved.len()); |
| 103 | + |
| 104 | + let _ = std::fs::remove_dir_all(&root); |
| 105 | +} |
| 106 | + |
| 107 | +/// 门禁决策应与 exit_code 一致。 |
| 108 | +#[test] |
| 109 | +fn gate_decision_consistent_with_exit_code() { |
| 110 | + let root = ensure_clean_dir("codeguard-gate-exit-code"); |
| 111 | + std::fs::write(root.join("Test.java"), "class Test { int x = 1; }\n").unwrap(); |
| 112 | + |
| 113 | + let output = Command::new(env!("CARGO_BIN_EXE_codeguard")) |
| 114 | + .args(["check", "all", root.to_str().unwrap(), "--format=json"]) |
| 115 | + .output() |
| 116 | + .expect("无法运行 codeguard check all"); |
| 117 | + |
| 118 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 119 | + let report: serde_json::Value = serde_json::from_str(&stdout).expect("JSON 解析失败"); |
| 120 | + |
| 121 | + let delivery = report["delivery_decision"].as_str().unwrap_or(""); |
| 122 | + let exit_code = output.status.code().unwrap_or(0); |
| 123 | + |
| 124 | + // allow 应对应 exit_code 0 |
| 125 | + if delivery == "allow" { |
| 126 | + assert_eq!(exit_code, 0, "allow 应对应 exit_code 0"); |
| 127 | + } |
| 128 | + |
| 129 | + // deny 应对应非 0 exit_code |
| 130 | + if delivery == "deny" { |
| 131 | + assert_ne!(exit_code, 0, "deny 应对应非 0 exit_code"); |
| 132 | + } |
| 133 | + |
| 134 | + println!("exit_code: {exit_code}, delivery_decision: {delivery}"); |
| 135 | + |
| 136 | + let _ = std::fs::remove_dir_all(&root); |
| 137 | +} |
0 commit comments