|
| 1 | +//! Codebase evaluation metrics runner. |
| 2 | +//! |
| 3 | +//! Implements the smallest actionable slice of |
| 4 | +//! `docs/specifications/terraphim-codebase-eval-check.md` (§3 Metrics Runner): |
| 5 | +//! executes `cargo clippy` and `cargo test`, parses their machine-readable |
| 6 | +//! JSON output, and emits normalised [`MetricRecord`]s. |
| 7 | +//! |
| 8 | +//! The Verdict Engine (§4), Artifacts Store (§5), and CI Integration (§6) |
| 9 | +//! described in the spec are intentionally out of scope for this slice. |
| 10 | +
|
| 11 | +mod error; |
| 12 | +mod metrics_runner; |
| 13 | +mod types; |
| 14 | + |
| 15 | +pub use error::{EvalError, Result}; |
| 16 | +pub use metrics_runner::{MetricsRunner, parse_clippy_json, parse_test_json}; |
| 17 | +pub use types::{MetricCounts, MetricRecord, PassFail}; |
| 18 | + |
| 19 | +use std::path::{Path, PathBuf}; |
| 20 | +use std::process::Command; |
| 21 | + |
| 22 | +/// Run `cargo clippy --message-format=json --quiet` in `manifest_dir` and |
| 23 | +/// normalise the result into a [`MetricRecord`]. |
| 24 | +/// |
| 25 | +/// # Errors |
| 26 | +/// - [`EvalError::NotADirectory`] if `manifest_dir` is not a directory. |
| 27 | +/// - [`EvalError::CargoFailed`] if cargo itself fails to run (non-zero exit |
| 28 | +/// **and** no JSON was produced). Note: clippy exits non-zero when it finds |
| 29 | +/// lints, but the JSON output is still parsed and returned as a record. |
| 30 | +pub fn run_clippy(manifest_dir: impl AsRef<Path>) -> Result<MetricRecord> { |
| 31 | + run_cargo( |
| 32 | + manifest_dir.as_ref(), |
| 33 | + "clippy", |
| 34 | + "cargo-clippy", |
| 35 | + &["clippy", "--message-format=json", "--quiet"], |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +/// Run `cargo test --message-format=json --quiet` in `manifest_dir` and |
| 40 | +/// normalise the result into a [`MetricRecord`]. |
| 41 | +/// |
| 42 | +/// # Errors |
| 43 | +/// - [`EvalError::NotADirectory`] if `manifest_dir` is not a directory. |
| 44 | +/// - [`EvalError::CargoFailed`] if cargo fails to spawn. |
| 45 | +pub fn run_test(manifest_dir: impl AsRef<Path>) -> Result<MetricRecord> { |
| 46 | + run_cargo( |
| 47 | + manifest_dir.as_ref(), |
| 48 | + "test", |
| 49 | + "cargo-test", |
| 50 | + &["test", "--message-format=json", "--quiet"], |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +fn run_cargo( |
| 55 | + manifest_dir: &Path, |
| 56 | + metric_id: &str, |
| 57 | + tool: &str, |
| 58 | + args: &[&str], |
| 59 | +) -> Result<MetricRecord> { |
| 60 | + let manifest_dir = canonicalize_dir(manifest_dir)?; |
| 61 | + let output = Command::new("cargo") |
| 62 | + .args(args) |
| 63 | + .current_dir(&manifest_dir) |
| 64 | + .output()?; |
| 65 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 66 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 67 | + let code = output.status.code(); |
| 68 | + |
| 69 | + let counts = match metric_id { |
| 70 | + "clippy" => parse_clippy_json(&stdout)?, |
| 71 | + "test" => parse_test_json(&stdout)?, |
| 72 | + other => unreachable!("unsupported metric_id: {other}"), |
| 73 | + }; |
| 74 | + |
| 75 | + // If cargo failed to even produce parseable output, surface the error. |
| 76 | + // Clippy exits 101 when it finds errors, but stdout is still valid JSON |
| 77 | + // and counts.errors > 0 — that is a normal "fail" record, not an error. |
| 78 | + if counts == MetricCounts::default() && !output.status.success() && code.is_some() { |
| 79 | + return Err(EvalError::CargoFailed { |
| 80 | + code: code.unwrap_or(-1), |
| 81 | + stderr: stderr.to_string(), |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + Ok(MetricRecord::new( |
| 86 | + metric_id, |
| 87 | + tool, |
| 88 | + counts, |
| 89 | + code, |
| 90 | + Some(manifest_dir), |
| 91 | + )) |
| 92 | +} |
| 93 | + |
| 94 | +fn canonicalize_dir(path: &Path) -> Result<PathBuf> { |
| 95 | + if !path.is_dir() { |
| 96 | + return Err(EvalError::NotADirectory(path.to_path_buf())); |
| 97 | + } |
| 98 | + Ok(path.canonicalize().unwrap_or_else(|_| path.to_path_buf())) |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod tests { |
| 103 | + use super::*; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn run_clippy_on_missing_dir_returns_not_a_directory() { |
| 107 | + let result = run_clippy("/nonexistent/path/does/not/exist"); |
| 108 | + assert!(matches!(result, Err(EvalError::NotADirectory(_)))); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn run_test_on_missing_dir_returns_not_a_directory() { |
| 113 | + let result = run_test("/nonexistent/path/does/not/exist"); |
| 114 | + assert!(matches!(result, Err(EvalError::NotADirectory(_)))); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn run_clippy_on_existing_dir_does_not_return_not_a_directory() { |
| 119 | + // The crate's own manifest dir exists; this sanity-checks that a real |
| 120 | + // directory is not wrongly rejected. (Cargo may still fail if the dir |
| 121 | + // lacks a Cargo.toml, but that is a different error, not NotADirectory.) |
| 122 | + let result = run_clippy(env!("CARGO_MANIFEST_DIR")); |
| 123 | + assert!(!matches!(result, Err(EvalError::NotADirectory(_)))); |
| 124 | + } |
| 125 | +} |
0 commit comments