From 557d50ad31772617846667fcdadf2c17e06cfa9f Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Fri, 27 Feb 2026 20:48:05 +0000 Subject: [PATCH] Add mutation test outcome tracking and persistence --- Cargo.lock | 1 + crates/bough-cli/Cargo.toml | 3 +- crates/bough-cli/src/main.rs | 6 +- crates/bough-cli/src/steps/test_workspace.rs | 95 ++++++++++++++------ crates/bough-core/src/lib.rs | 2 +- 5 files changed, 78 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d4682f07..52d24072 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -119,6 +119,7 @@ dependencies = [ "assert_cmd", "bough-core", "bough-typed-hash", + "chrono", "clap", "clap_complete", "glob", diff --git a/crates/bough-cli/Cargo.toml b/crates/bough-cli/Cargo.toml index 2938a087..ca1636eb 100644 --- a/crates/bough-cli/Cargo.toml +++ b/crates/bough-cli/Cargo.toml @@ -9,10 +9,11 @@ name = "bough" path = "src/main.rs" [dependencies] +chrono = { version = "0.4", features = ["serde"] } clap = { version = "4", features = ["derive", "env"] } clap_complete = "4" bough-core = { path = "../bough-core" } -bough-typed-hash = { path = "../bough-typed-hash" } +bough-typed-hash = { path = "../bough-typed-hash", features = ["disk"] } serde = { version = "1", features = ["derive"] } serde_json = "1" toml = "1.0.3" diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index 00b377d7..b0dc212f 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -65,6 +65,8 @@ enum WorkspaceAction { Test { #[arg()] name: String, + #[arg()] + mutation_hash: String, }, Drop { #[arg()] @@ -147,7 +149,7 @@ fn main() { let no_color = !std::io::IsTerminal::is_terminal(&std::io::stdout()); result.render(&cli.output_style, no_color, 0); } - WorkspaceAction::Test { name } => { + WorkspaceAction::Test { name, mutation_hash } => { let cfg = config::load(&cli).unwrap_or_else(|e| { eprintln!("{e}"); std::process::exit(1); @@ -157,7 +159,7 @@ fn main() { std::process::exit(1); }); let path = PathBuf::from(cfg.working_dir()).join(&*ws); - let result = steps::test_workspace::run(&cfg, &path).unwrap_or_else(|e| { + let result = steps::test_workspace::run(&cfg, &path, mutation_hash).unwrap_or_else(|e| { eprintln!("{e}"); std::process::exit(1); }); diff --git a/crates/bough-cli/src/steps/test_workspace.rs b/crates/bough-cli/src/steps/test_workspace.rs index a19af1b0..44bfbadb 100644 --- a/crates/bough-cli/src/steps/test_workspace.rs +++ b/crates/bough-cli/src/steps/test_workspace.rs @@ -1,16 +1,23 @@ use bough_core::config::Config; +use bough_core::{Mutation, MutationResult, Outcome}; +use bough_typed_hash::{DiskHashStore, HashStore, MemoryHashStore, TypedHashable}; use serde::Serialize; use std::path::{Path, PathBuf}; -use crate::phase_runner::PhaseRunner; +use crate::phase_runner::{self, PhaseRunner}; use crate::render::{Render, color}; +use super::{get_mutations, get_src_files}; + #[derive(Debug)] pub enum Error { NoActiveRunner, NoTestPhase, WorkspaceNotFound(PathBuf), - Phase(crate::phase_runner::Error), + SrcFiles(get_src_files::Error), + Mutations(get_mutations::Error), + MutationNotFound(String), + Phase(phase_runner::Error), } impl std::fmt::Display for Error { @@ -19,6 +26,9 @@ impl std::fmt::Display for Error { Error::NoActiveRunner => write!(f, "no active runner configured"), Error::NoTestPhase => write!(f, "no test phase configured for runner"), Error::WorkspaceNotFound(p) => write!(f, "workspace not found: {}", p.display()), + Error::SrcFiles(e) => write!(f, "{e}"), + Error::Mutations(e) => write!(f, "{e}"), + Error::MutationNotFound(h) => write!(f, "no mutation found with hash {h}"), Error::Phase(e) => write!(f, "test phase failed: {e}"), } } @@ -29,24 +39,60 @@ impl std::error::Error for Error {} #[derive(Debug, Serialize)] pub struct TestWorkspace { pub workspace: PathBuf, - pub stdout: String, + pub mutation_hash: String, + pub outcome: Outcome, +} + +fn find_mutation(config: &Config, hash: &str) -> Result { + let src_files = get_src_files::run(config).map_err(Error::SrcFiles)?; + let mutations = get_mutations::run(&src_files, config).map_err(Error::Mutations)?; + + for (_lang, muts) in &mutations.mutations { + for m in muts { + let m_hash = m.hash(&mut MemoryHashStore::new()).expect("hash failed").to_string(); + if m_hash == hash { + return Ok(m.clone()); + } + } + } + + Err(Error::MutationNotFound(hash.to_string())) } -pub fn run(config: &Config, workspace: &Path) -> Result { +pub fn run(config: &Config, workspace: &Path, mutation_hash: &str) -> Result { let runner_name = config.resolved_runner_name().ok_or(Error::NoActiveRunner)?; - let test = config.runner_test_phase(runner_name).ok_or(Error::NoTestPhase)?; + let test_phase = config.runner_test_phase(runner_name).ok_or(Error::NoTestPhase)?; if !workspace.exists() { return Err(Error::WorkspaceNotFound(workspace.to_path_buf())); } - let output = PhaseRunner::new(test, workspace) - .run() - .map_err(Error::Phase)?; + let mutation = find_mutation(config, mutation_hash)?; + + let outcome = match PhaseRunner::new(test_phase, workspace).run() { + Ok(_) => Outcome::Missed, + Err(phase_runner::Error::CommandFailed { .. }) => Outcome::Caught, + Err(phase_runner::Error::Timeout { .. }) => { + config.runner_treat_timeouts_as(runner_name).unwrap_or_default() + } + Err(e) => return Err(Error::Phase(e)), + }; + + let result = MutationResult { + outcome, + mutation, + at: chrono::Utc::now(), + }; + + let mut store = DiskHashStore::::new( + PathBuf::from(config.state_dir()), + ); + store.insert(result); Ok(TestWorkspace { workspace: workspace.to_path_buf(), - stdout: output.stdout, + mutation_hash: mutation_hash.to_string(), + outcome, }) } @@ -57,31 +103,30 @@ impl Render for TestWorkspace { fn render_terse(&self) -> String { format!( - "tested workspace {}\n", + "{} mutation {} in workspace {}\n", + match self.outcome { + Outcome::Caught => color("\x1b[32m", "caught"), + Outcome::Missed => color("\x1b[31m", "missed"), + }, + color("\x1b[2m", &self.mutation_hash), color("\x1b[1m", &self.workspace.display().to_string()), ) } fn render_verbose(&self) -> String { - let mut out = format!( - "tested workspace {}\n", - color("\x1b[1m", &self.workspace.display().to_string()), - ); - if !self.stdout.is_empty() { - out.push_str(&color("\x1b[2m", &self.stdout)); - } - out + self.render_terse() } fn render_markdown(&self, depth: u8) -> String { let heading = "#".repeat((depth + 1).min(6) as usize); - let mut out = format!( - "{heading} Test Workspace\n\n- **Path:** `{}`\n", + format!( + "{heading} Test Workspace\n\n\ + - **Workspace:** `{}`\n\ + - **Mutation:** `{}`\n\ + - **Outcome:** {:?}\n", self.workspace.display(), - ); - if !self.stdout.is_empty() { - out.push_str(&format!("\n```\n{}\n```\n", self.stdout.trim())); - } - out + self.mutation_hash, + self.outcome, + ) } } diff --git a/crates/bough-core/src/lib.rs b/crates/bough-core/src/lib.rs index 19549665..31ba2c3c 100644 --- a/crates/bough-core/src/lib.rs +++ b/crates/bough-core/src/lib.rs @@ -77,7 +77,7 @@ pub enum Outcome { Caught, } -#[derive(Clone)] +#[derive(Clone, Serialize, Deserialize)] pub struct MutationResult { pub outcome: Outcome, pub mutation: Mutation, -- knotmirror 0.0.1