From 555fb9a0d4ba958df649465c3911745cb1b06e33 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Tue, 3 Mar 2026 01:16:15 +0000 Subject: [PATCH] Replace serde-value with toml, rename commands to command --- Cargo.lock | 1 - bough.config.toml | 4 +- crates/bough-core/Cargo.toml | 3 +- crates/bough-core/src/config.rs | 96 ++++++++++++++++----------------- crates/bough-core/src/phase.rs | 30 +++++------ 5 files changed, 65 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d85d3a1e..d8444815 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -162,7 +162,6 @@ dependencies = [ "clap", "glob", "serde", - "serde-value", "tempfile", "toml 0.8.23", "tree-sitter", diff --git a/bough.config.toml b/bough.config.toml index d83c60ef..75c34c26 100644 --- a/bough.config.toml +++ b/bough.config.toml @@ -3,10 +3,10 @@ pwd = "examples/vitest-js" ordering = "random" [init] -commands = "npm install" +command = "npm install" [test] -commands = "npm test" +command = "npm test" [test_ids] get_all = "npm run --silent get-test-ids" diff --git a/crates/bough-core/Cargo.toml b/crates/bough-core/Cargo.toml index c441c224..5c977364 100644 --- a/crates/bough-core/Cargo.toml +++ b/crates/bough-core/Cargo.toml @@ -10,7 +10,7 @@ chrono = { version = "0.4", features = ["serde"] } clap = { version = "4.5.60", features = ["derive"] } glob = "0.3" serde = { version = "1", features = ["derive"] } -serde-value = "0.7.0" +toml = "0.8" tree-sitter = "0.26" tree-sitter-javascript = "0.25" tree-sitter-typescript = "0.23" @@ -18,4 +18,3 @@ wait-timeout = "0.2" [dev-dependencies] tempfile = "3.26" -toml = "0.8" diff --git a/crates/bough-core/src/config.rs b/crates/bough-core/src/config.rs index 71c8b116..3036169c 100644 --- a/crates/bough-core/src/config.rs +++ b/crates/bough-core/src/config.rs @@ -2,7 +2,6 @@ use crate::Outcome; use crate::languages::LanguageId; use crate::phase::{Phase, PhaseRunner, RunIn}; use serde::{Deserialize, Serialize}; -use serde_value::Value; use std::collections::HashMap; use std::path::{Path, PathBuf}; @@ -26,12 +25,12 @@ pub struct PhaseMetaConfig { pub struct PhaseConfig { #[serde(flatten)] pub(crate) meta: PhaseMetaConfig, - pub(crate) commands: String, + pub(crate) command: String, } impl PhaseConfig { - pub fn commands(&self) -> &str { - &self.commands + pub fn command(&self) -> &str { + &self.command } } @@ -90,31 +89,31 @@ pub enum MutantSkipConfig { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(default)] pub struct Config { - pub(crate) source_dir: PathBuf, - pub(crate) ordering: OrderingConfig, - pub(crate) treat_timeouts_as: Outcome, - pub(crate) test_ids: Option, - #[serde(default = "default_threads")] pub(crate) threads: u32, - #[serde(default = "default_bough_dir")] pub(crate) bough_dir: PathBuf, - #[serde(skip)] + pub(crate) source_dir: PathBuf, + pub(crate) ordering: OrderingConfig, + pub(crate) treat_timeouts_as: Outcome, + pub(crate) test_ids: Option, #[serde(flatten)] pub(crate) meta_defaults: PhaseMetaConfig, - - #[serde(flatten)] - pub(crate) phases: HashMap, - + pub(crate) init: Option, + pub(crate) reset: Option, + pub(crate) test: Option, #[serde(default)] pub(crate) mutate: HashMap, } impl Config { pub fn get_phase_config(&self, phase: Phase) -> Option<&PhaseConfig> { - self.phases.get(&phase) + match phase { + Phase::Init => self.init.as_ref(), + Phase::Reset => self.reset.as_ref(), + Phase::Test => self.test.as_ref(), + } } pub fn new_phase_runner( @@ -123,8 +122,7 @@ impl Config { run_in: RunIn, ) -> Result { let phase_config = self - .phases - .get(&phase) + .get_phase_config(phase) .ok_or(ConfigError::PhaseNotConfigured { phase })?; let base = match &run_in { @@ -163,7 +161,7 @@ impl Config { pwd, env, timeout, - commands: phase_config.commands.clone(), + command: phase_config.command.clone(), }) } @@ -186,37 +184,39 @@ impl Default for Config { treat_timeouts_as: Outcome::Caught, test_ids: None, meta_defaults: PhaseMetaConfig::default(), - phases: HashMap::new(), + init: None, + reset: None, + test: None, mutate: HashMap::new(), } } } pub struct ConfigBuilder { - value: Value, + value: toml::Value, source_dir: PathBuf, } impl ConfigBuilder { pub fn new(source_dir: PathBuf) -> Self { Self { - value: Value::Map(Default::default()), + value: toml::Value::Table(Default::default()), source_dir, } } - pub fn from_value(self, value: Value) -> Self { + pub fn from_value(self, value: toml::Value) -> Self { Self { value, ..self } } - pub fn override_with(mut self, patch: Value) -> Self { + pub fn override_with(mut self, patch: toml::Value) -> Self { self.value = Self::deep_merge(self.value, patch); self } - fn deep_merge(base: Value, patch: Value) -> Value { + fn deep_merge(base: toml::Value, patch: toml::Value) -> toml::Value { match (base, patch) { - (Value::Map(mut base_map), Value::Map(patch_map)) => { + (toml::Value::Table(mut base_map), toml::Value::Table(patch_map)) => { for (k, v) in patch_map { let merged = match base_map.remove(&k) { Some(existing) => Self::deep_merge(existing, v), @@ -224,7 +224,7 @@ impl ConfigBuilder { }; base_map.insert(k, merged); } - Value::Map(base_map) + toml::Value::Table(base_map) } (_, patch) => patch, } @@ -240,7 +240,7 @@ impl ConfigBuilder { #[derive(Debug)] pub enum ConfigError { PhaseNotConfigured { phase: Phase }, - Deserialize(serde_value::DeserializerError), + Deserialize(toml::de::Error), } impl std::fmt::Display for ConfigError { @@ -261,7 +261,6 @@ mod tests { use super::*; use crate::Outcome; use crate::languages::LanguageId; - use crate::phase::Phase; const IDEAL_CONFIG: &str = r#" threads = 1 @@ -271,18 +270,18 @@ treat_timeouts_as = "Missed" [init] pwd = "./examples/vitest" -commands = "npm install" +command = "npm install" [reset] pwd = "./examples/vitest" -commands = "npm clean" +command = "npm clean" [test] pwd = "./examples/vitest" timeout.absolute = 30 timeout.relative = 3 env = { NODE_ENV = "production" } -commands = "npx run test" +command = "npx run test" [mutate.js] files.include = ["**/*.js", "**/*.jsx"] @@ -302,15 +301,14 @@ mutants.skip = [ const MINIMAL_CONFIG: &str = r#" [test] -commands = "npx vitest run" +command = "npx vitest run" [mutate.ts] files.include = ["src/**/*.ts"] "#; - fn toml_to_value(s: &str) -> Value { - let tv: toml::Value = toml::from_str(s).unwrap(); - serde_value::to_value(tv).unwrap() + fn toml_to_value(s: &str) -> toml::Value { + toml::from_str(s).unwrap() } #[test] @@ -322,15 +320,15 @@ files.include = ["src/**/*.ts"] assert_eq!(config.ordering, OrderingConfig::Alphabetical); assert_eq!(config.treat_timeouts_as, Outcome::Missed); - let init = &config.phases[&Phase::Init]; + let init = config.init.as_ref().unwrap(); assert_eq!(init.meta.pwd, Some(PathBuf::from("./examples/vitest"))); - assert_eq!(init.commands, "npm install"); + assert_eq!(init.command, "npm install"); - let test = &config.phases[&Phase::Test]; + let test = config.test.as_ref().unwrap(); assert_eq!(test.meta.timeout.absolute, Some(30)); assert_eq!(test.meta.timeout.relative, Some(3)); assert_eq!(test.meta.env["NODE_ENV"], "production"); - assert_eq!(test.commands, "npx run test"); + assert_eq!(test.command, "npx run test"); let js_mutate = &config.mutate[&LanguageId::Javascript]; assert_eq!(js_mutate.files.include, vec!["**/*.js", "**/*.jsx"]); @@ -347,11 +345,11 @@ files.include = ["src/**/*.ts"] assert_eq!(config.bough_dir, PathBuf::from("./.bough")); assert_eq!(config.ordering, OrderingConfig::Random); assert_eq!(config.treat_timeouts_as, Outcome::Caught); - assert!(!config.phases.contains_key(&Phase::Init)); - assert!(!config.phases.contains_key(&Phase::Reset)); + assert!(config.init.is_none()); + assert!(config.reset.is_none()); - let test = &config.phases[&Phase::Test]; - assert_eq!(test.commands, "npx vitest run"); + let test = config.test.as_ref().unwrap(); + assert_eq!(test.command, "npx vitest run"); assert_eq!(test.meta.pwd, None); assert!(test.meta.env.is_empty()); assert_eq!(test.meta.timeout, TimeoutConfig::default()); @@ -388,10 +386,10 @@ files.include = ["src/**/*.ts"] MINIMAL_CONFIG, r#" [test] - commands = "npm test" + command = "npm test" "#, ); - assert_eq!(config.phases[&Phase::Test].commands, "npm test"); + assert_eq!(config.test.as_ref().unwrap().command, "npm test"); } #[test] @@ -403,14 +401,14 @@ files.include = ["src/**/*.ts"] FOO = "bar" "#, ); - assert_eq!(config.phases[&Phase::Test].meta.env["FOO"], "bar"); + assert_eq!(config.test.as_ref().unwrap().meta.env["FOO"], "bar"); } #[test] fn override_deep_merge_phase() { let config = build_with_override(IDEAL_CONFIG, r#"treat_timeouts_as = "Caught""#); assert_eq!(config.treat_timeouts_as, Outcome::Caught); - assert!(config.phases.contains_key(&Phase::Init)); + assert!(config.init.is_some()); } #[test] @@ -418,7 +416,7 @@ files.include = ["src/**/*.ts"] let config: Config = toml::from_str("").expect("empty config should parse"); assert_eq!(config.threads, 1); assert_eq!(config.bough_dir, PathBuf::from("./.bough")); - assert!(config.phases.is_empty()); + assert!(config.init.is_none() && config.reset.is_none() && config.test.is_none()); } #[test] diff --git a/crates/bough-core/src/phase.rs b/crates/bough-core/src/phase.rs index 26dc2f1f..bb814838 100644 --- a/crates/bough-core/src/phase.rs +++ b/crates/bough-core/src/phase.rs @@ -62,13 +62,13 @@ pub struct PhaseRunner { pub pwd: PathBuf, pub env: HashMap, pub timeout: TimeoutConfig, - pub commands: String, + pub command: String, } impl PhaseRunner { pub fn run(&self) -> Result { let timeout = self.timeout.absolute.map(Duration::from_secs); - let (stdout, code) = self.run_one(0, &self.commands, &self.pwd, &timeout)?; + let (stdout, code) = self.run_one(0, &self.command, &self.pwd, &timeout)?; Ok(PhaseOutput { stdout, error_code: if code != 0 { Some(code) } else { None } }) } @@ -133,7 +133,7 @@ mod tests { fn config(source_dir: &str, toml: &str) -> crate::config::Config { let tv: toml::Value = toml::from_str(toml).unwrap(); ConfigBuilder::new(PathBuf::from(source_dir)) - .from_value(serde_value::to_value(tv).unwrap()) + .from_value(tv) .build() .unwrap() } @@ -142,7 +142,7 @@ mod tests { fn source_dir_is_pwd_when_no_pwd_set() { let c = config("/src", r#" [test] - commands = "run" + command = "run" "#); let r = c.new_phase_runner(Phase::Test, RunIn::SourceDir).unwrap(); assert_eq!(r.pwd, PathBuf::from("/src")); @@ -153,7 +153,7 @@ mod tests { let c = config("/src", r#" [test] pwd = "subdir" - commands = "run" + command = "run" "#); let r = c.new_phase_runner(Phase::Test, RunIn::SourceDir).unwrap(); assert_eq!(r.pwd, PathBuf::from("/src/subdir")); @@ -164,7 +164,7 @@ mod tests { let c = config("/src", r#" pwd = "default-dir" [test] - commands = "run" + command = "run" "#); let r = c.new_phase_runner(Phase::Test, RunIn::SourceDir).unwrap(); assert_eq!(r.pwd, PathBuf::from("/src/default-dir")); @@ -176,7 +176,7 @@ mod tests { pwd = "default-dir" [test] pwd = "phase-dir" - commands = "run" + command = "run" "#); let r = c.new_phase_runner(Phase::Test, RunIn::SourceDir).unwrap(); assert_eq!(r.pwd, PathBuf::from("/src/phase-dir")); @@ -187,7 +187,7 @@ mod tests { let c = config("/src", r#" bough_dir = "/bough" [test] - commands = "run" + command = "run" "#); let r = c.new_phase_runner(Phase::Test, RunIn::Workspace(WorkspaceId::from_trusted("ws"))).unwrap(); assert_eq!(r.pwd, PathBuf::from("/bough/workspaces/ws")); @@ -198,7 +198,7 @@ mod tests { let c = config("/src", r#" bough_dir = ".bough" [test] - commands = "run" + command = "run" "#); let r = c.new_phase_runner(Phase::Test, RunIn::Workspace(WorkspaceId::from_trusted("ws"))).unwrap(); assert_eq!(r.pwd, PathBuf::from("/src/.bough/workspaces/ws")); @@ -211,7 +211,7 @@ mod tests { SHARED = "default" DEFAULT_ONLY = "d" [test] - commands = "run" + command = "run" [test.env] SHARED = "phase" PHASE_ONLY = "p" @@ -228,7 +228,7 @@ mod tests { timeout.absolute = 60 timeout.relative = 5 [test] - commands = "run" + command = "run" [test.timeout] absolute = 30 "#); @@ -238,20 +238,20 @@ mod tests { } #[test] - fn commands_taken_from_phase() { + fn command_taken_from_phase() { let c = config("/src", r#" [test] - commands = "step1" + command = "step1" "#); let r = c.new_phase_runner(Phase::Test, RunIn::SourceDir).unwrap(); - assert_eq!(r.commands, "step1"); + assert_eq!(r.command, "step1"); } #[test] fn phase_not_configured_returns_error() { let c = config("/src", r#" [test] - commands = "run" + command = "run" "#); let err = c.new_phase_runner(Phase::Init, RunIn::SourceDir).unwrap_err(); assert!(matches!(err, ConfigError::PhaseNotConfigured { .. })); -- knotmirror 0.0.1