|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 3 | + |
| 4 | +//! CLI contract for `panic-attack adjudicate`: an adjudication that processed |
| 5 | +//! no report, or failed to load any report, must never read as `pass`. |
| 6 | +
|
| 7 | +use std::fs; |
| 8 | +use std::path::Path; |
| 9 | +use std::process::{Command, Output}; |
| 10 | +use tempfile::TempDir; |
| 11 | + |
| 12 | +fn adjudicate(dir: &Path, reports: &[&Path]) -> (Output, serde_json::Value) { |
| 13 | + let out = dir.join("out.json"); |
| 14 | + let output = Command::new(env!("CARGO_BIN_EXE_panic-attack")) |
| 15 | + .current_dir(dir) |
| 16 | + .arg("adjudicate") |
| 17 | + .args(reports) |
| 18 | + .arg("--output") |
| 19 | + .arg(&out) |
| 20 | + .output() |
| 21 | + .expect("binary should run"); |
| 22 | + let json = fs::read_to_string(&out) |
| 23 | + .ok() |
| 24 | + .and_then(|s| serde_json::from_str(&s).ok()) |
| 25 | + .unwrap_or(serde_json::Value::Null); |
| 26 | + (output, json) |
| 27 | +} |
| 28 | + |
| 29 | +fn good_abduct(dir: &Path) -> std::path::PathBuf { |
| 30 | + let p = dir.join("good-abduct.json"); |
| 31 | + let report = serde_json::json!({ |
| 32 | + "schema_version": "2.5", |
| 33 | + "created_at": "2026-01-01T00:00:00Z", |
| 34 | + "target": "src/main.rs", |
| 35 | + "source_root": ".", |
| 36 | + "workspace_dir": "runtime/abduct", |
| 37 | + "dependency_scope": "none", |
| 38 | + "selected_files": 0, |
| 39 | + "locked_files": 0, |
| 40 | + "mtime_shifted_files": 0, |
| 41 | + "mtime_offset_days": 0, |
| 42 | + "time_mode": "normal", |
| 43 | + "time_scale": null, |
| 44 | + "virtual_now": null, |
| 45 | + "files": [], |
| 46 | + "execution": null, |
| 47 | + }); |
| 48 | + fs::write(&p, report.to_string()).unwrap(); |
| 49 | + p |
| 50 | +} |
| 51 | + |
| 52 | +#[test] |
| 53 | +fn missing_file_is_not_pass() { |
| 54 | + let dir = TempDir::new().unwrap(); |
| 55 | + let missing = dir.path().join("nope.json"); |
| 56 | + let (o, j) = adjudicate(dir.path(), &[&missing]); |
| 57 | + assert_eq!(o.status.code(), Some(2), "{:?}", o); |
| 58 | + assert_eq!(j["verdict"], "error"); |
| 59 | + assert_eq!(j["processed_reports"], 0); |
| 60 | + assert_eq!(j["failed_reports"], 1); |
| 61 | + assert!(j["notes"].to_string().contains("nope.json")); |
| 62 | + assert!(!String::from_utf8_lossy(&o.stdout).contains("verdict: pass")); |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn unrecognised_file_is_not_pass() { |
| 67 | + let dir = TempDir::new().unwrap(); |
| 68 | + let junk = dir.path().join("junk.json"); |
| 69 | + fs::write(&junk, r#"{"hello": "world"}"#).unwrap(); |
| 70 | + let (o, j) = adjudicate(dir.path(), &[&junk]); |
| 71 | + assert_eq!(o.status.code(), Some(2), "{:?}", o); |
| 72 | + assert_eq!(j["verdict"], "error"); |
| 73 | + assert_eq!(j["failed_reports"], 1); |
| 74 | + assert!(String::from_utf8_lossy(&o.stderr).contains("junk.json")); |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn good_plus_bad_is_not_pass() { |
| 79 | + let dir = TempDir::new().unwrap(); |
| 80 | + let good = good_abduct(dir.path()); |
| 81 | + let missing = dir.path().join("gone.json"); |
| 82 | + |
| 83 | + // Control: the good report alone passes and exits 0. |
| 84 | + let (o, j) = adjudicate(dir.path(), &[&good]); |
| 85 | + assert_eq!(j["processed_reports"], 1, "fixture must parse: {:?}", o); |
| 86 | + assert_eq!(j["verdict"], "pass"); |
| 87 | + assert_eq!(o.status.code(), Some(0)); |
| 88 | + |
| 89 | + let (o, j) = adjudicate(dir.path(), &[&good, &missing]); |
| 90 | + assert_eq!(j["processed_reports"], 1); |
| 91 | + assert_eq!(j["failed_reports"], 1); |
| 92 | + assert_eq!(j["verdict"], "error"); |
| 93 | + assert_eq!(o.status.code(), Some(2)); |
| 94 | +} |
| 95 | + |
| 96 | +#[test] |
| 97 | +fn zero_arguments_is_rejected() { |
| 98 | + let dir = TempDir::new().unwrap(); |
| 99 | + let o = Command::new(env!("CARGO_BIN_EXE_panic-attack")) |
| 100 | + .current_dir(dir.path()) |
| 101 | + .arg("adjudicate") |
| 102 | + .output() |
| 103 | + .unwrap(); |
| 104 | + assert_eq!(o.status.code(), Some(2)); |
| 105 | + assert!(!String::from_utf8_lossy(&o.stdout).contains("verdict: pass")); |
| 106 | +} |
0 commit comments