eval-magic 0.3.2

One-stop CLI for running skill evals — measure whether an agent skill actually shifts behavior.
Documentation
//! Runtime helpers.
//!
//! The synchronous git invocation helper. No runtime asset locator lives here:
//! the schemas are bundled into the binary at compile time (`include_str!`),
//! `clap` owns argument parsing, and the `error: <msg>` + exit(1) contract
//! lives in `src/main.rs`.

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

/// Outcome of a git invocation.
///
/// `status` is `None` when git could not be spawned at all (e.g. ENOENT, a
/// nonexistent cwd, permission denied); the reason is surfaced into `stderr`,
/// so callers have one channel for both git's own errors and spawn failures.
/// `stdout` and
/// `stderr` are raw bytes — callers that read file contents out of git
/// (`git show`) need the undecoded buffer, not a lossy UTF-8 string.
#[derive(Debug)]
pub struct GitOutput {
    pub status: Option<i32>,
    pub stdout: Vec<u8>,
    pub stderr: Vec<u8>,
}

/// Synchronously invoke `git` with `args` in `cwd`, returning its status and raw
/// output. A failure to spawn git is not an error here: it yields `status: None`
/// with the spawn error surfaced into `stderr`, so callers can handle it
/// alongside git's own failures.
pub fn run_git(args: &[&str], cwd: &Path) -> GitOutput {
    match Command::new("git").args(args).current_dir(cwd).output() {
        Ok(out) => GitOutput {
            status: out.status.code(),
            stdout: out.stdout,
            stderr: out.stderr,
        },
        Err(err) => GitOutput {
            status: None,
            stdout: Vec::new(),
            stderr: format!("{err}").into_bytes(),
        },
    }
}

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

    /// A successful git command returns exit status 0 and writes to stdout.
    /// Run against this crate's own repo, which has commit history.
    #[test]
    fn run_git_success_status_and_stdout() {
        let res = run_git(
            &["rev-parse", "--short", "HEAD"],
            env!("CARGO_MANIFEST_DIR").as_ref(),
        );
        assert_eq!(res.status, Some(0));
        assert!(String::from_utf8_lossy(&res.stdout).trim().len() > 3);
    }

    /// A git command that fails (bad ref) returns a non-zero status.
    #[test]
    fn run_git_failing_command_nonzero() {
        let res = run_git(
            &["rev-parse", "not-a-real-ref-xyz"],
            env!("CARGO_MANIFEST_DIR").as_ref(),
        );
        assert_ne!(res.status, Some(0));
    }

    /// When git itself cannot be spawned (here, a nonexistent cwd), the status
    /// is `None` and the spawn error is surfaced into stderr — the contract is
    /// the null status plus a readable reason, not any particular error-code
    /// spelling.
    #[test]
    fn run_git_spawn_error_surfaced() {
        let res = run_git(
            &["rev-parse", "HEAD"],
            "/nonexistent-dir-for-rungit-test".as_ref(),
        );
        assert_eq!(res.status, None);
        assert!(String::from_utf8_lossy(&res.stderr).contains("No such file or directory"));
    }
}