diff --git a/crates/bough-core/BASE_SPEC.md b/crates/bough-core/BASE_SPEC.md index 8d455860..53915b1e 100644 --- a/crates/bough-core/BASE_SPEC.md +++ b/crates/bough-core/BASE_SPEC.md @@ -6,11 +6,11 @@ Base impls Root core[base.files] Base::files -> TwigsIter -core[base.mutant_files] -Base::mutant_files(language_id: &LanguageId) -> TwigsIter + + -core[base.mutants] -Base::mutants(language_id: &LanguageId) -> TwigsMutantsIter + + -core[base.mutations] -Base::mutations(language_id: &LanguageId) -> TwigsMutationsIter + + diff --git a/crates/bough-core/FILE_SPEC.md b/crates/bough-core/FILE_SPEC.md index 63c37545..29c46cca 100644 --- a/crates/bough-core/FILE_SPEC.md +++ b/crates/bough-core/FILE_SPEC.md @@ -24,12 +24,6 @@ TwigsIter impls Iterator core[twig.iter.new] TwigsIter::new(root: &impl Root) -> Self, uses walkdir to recursively walk root -core[twig.iter.include] -TwigsIter::with_include_glob(self, pattern: &str) -> Self adds an include glob::Pattern - -core[twig.iter.exclude] -TwigsIter::with_exclude_glob(self, pattern: &str) -> Self adds an exclude glob::Pattern - core[twig.iter.include.match] A file should be yielded only if it matches any of the include patterns @@ -38,4 +32,3 @@ If no include patterns are configured, no files are yielded core[twig.iter.exclude.match] A file should be excluded if it matches any of the exclude patterns - diff --git a/crates/bough-core/MUTANTSITER_SPEC.md b/crates/bough-core/MUTANTSITER_SPEC.md index b66405d1..cf074d27 100644 --- a/crates/bough-core/MUTANTSITER_SPEC.md +++ b/crates/bough-core/MUTANTSITER_SPEC.md @@ -36,31 +36,31 @@ TwigMutantsIter finds javascript add binary ops core[mutant.twig-iter.find.js.binary.sub] TwigMutantsIter finds javascript subtract binary ops -## TwigsMutantsIter +core[mutant.twig-iter.skip.kind] +TwigMutantsIter::with_skip_kind(self, kind: MutantKind) -> Self skips all mutants of the specified kind -core[mutant.iter.twigs] -TwigsMutantsIter holds a TwigsIter +core[mutant.twig-iter.skip.kind.multiple] +withskip_kind can be called multiple times; a mutant is skipped if its kind matches ANY of the configured skip kinds -core[mutant.iter.base] -TwigsMutantsIter holds &Base +core[mutant.twig-iter.skip.query] +TwigMutantsIter::with_skip_query(self, query: &str) -> Self skips all mutants whose tree-sitter node matches the provided S-expression query -core[mutant.iter.lang] -TwigsMutantsIter owns LanguageId +core[mutant.twig-iter.skip.query.multiple] +withskip_query can be called multiple times; a mutant is skipped if its node matches ANY of the configured skip queries -core[mutant.iter.item] -TwigsMutantsIter impls Iterator + -core[mutant.iter.find] -TwigsMutantsIter lazily creates a TwigMutantsIter for each twig, yielding all Mutants across all files + + -core[mutant.iter.skip.kind] -TwigsMutantsIter::with_skip_kind(self, kind: MutantKind) -> Self skips all mutants of the specified kind + + -core[mutant.iter.skip.kind.multiple] -with_skip_kind can be called multiple times; a mutant is skipped if its kind matches ANY of the configured skip kinds + + -core[mutant.iter.skip.query] -TwigsMutantsIter::with_skip_query(self, query: &str) -> Self skips all mutants whose tree-sitter node matches the provided S-expression query + + -core[mutant.iter.skip.query.multiple] -with_skip_query can be called multiple times; a mutant is skipped if its node matches ANY of the configured skip queries + + diff --git a/crates/bough-core/src/mutant.rs b/crates/bough-core/src/mutant.rs index f0e451f7..34f9bc90 100644 --- a/crates/bough-core/src/mutant.rs +++ b/crates/bough-core/src/mutant.rs @@ -5,7 +5,7 @@ use bough_typed_hash::{HashInto, TypedHashable}; // core[impl mutant.iter.twig] // core[impl mutant.iter.base] // core[impl mutant.iter.lang] -pub struct MutantsIter<'a> { +pub struct TwigMutantsIter<'a> { lang: LanguageId, base: &'a Base, twig: &'a Twig, @@ -155,7 +155,7 @@ pub enum MutantKind { BinaryOp(BinaryOpMutationKind), } -impl<'a> MutantsIter<'a> { +impl<'a> TwigMutantsIter<'a> { pub fn new(lang: LanguageId, base: &'a Base, twig: &'a Twig) -> std::io::Result { // core[impl mutant.iter.file] let file_path = crate::file::File::new(base, twig).resolve(); @@ -196,7 +196,7 @@ impl<'a> MutantsIter<'a> { // core[impl mutant.iter.item] // core[impl mutant.iter.find] -impl<'a> Iterator for MutantsIter<'a> { +impl<'a> Iterator for TwigMutantsIter<'a> { type Item = Mutant<'a>; fn next(&mut self) -> Option { @@ -256,7 +256,7 @@ pub(crate) fn span_from_node(node: &tree_sitter::Node<'_>) -> Span { mod tests { use super::*; use crate::base::Base; - use crate::file::{TwigsIter, Root}; + use crate::file::{Root, TwigsIter}; use std::path::PathBuf; fn make_base() -> (tempfile::TempDir, Base) { @@ -377,7 +377,7 @@ mod tests { fn mutants_iter_holds_twig() { let (_dir, base) = make_base(); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let iter = MutantsIter::new(LanguageId::Javascript, &base, &twig).unwrap(); + let iter = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig).unwrap(); assert_eq!(iter.twig().path(), std::path::Path::new("src/a.js")); } @@ -386,7 +386,7 @@ mod tests { fn mutants_iter_holds_base() { let (_dir, base) = make_base(); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let iter = MutantsIter::new(LanguageId::Javascript, &base, &twig).unwrap(); + let iter = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig).unwrap(); assert_eq!(iter.base().path(), base.path()); } @@ -395,7 +395,7 @@ mod tests { fn mutants_iter_owns_lang() { let (_dir, base) = make_base(); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let iter = MutantsIter::new(LanguageId::Javascript, &base, &twig).unwrap(); + let iter = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig).unwrap(); assert_eq!(iter.lang(), LanguageId::Javascript); } @@ -404,7 +404,7 @@ mod tests { fn mutants_iter_resolves_file_from_base_and_twig() { let (_dir, base) = make_base(); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - assert!(MutantsIter::new(LanguageId::Javascript, &base, &twig).is_ok()); + assert!(TwigMutantsIter::new(LanguageId::Javascript, &base, &twig).is_ok()); } // core[verify mutant.iter.file] @@ -412,7 +412,7 @@ mod tests { fn mutants_iter_errors_on_missing_file() { let (_dir, base) = make_base(); let twig = Twig::new(PathBuf::from("src/nonexistent.js")).unwrap(); - assert!(MutantsIter::new(LanguageId::Javascript, &base, &twig).is_err()); + assert!(TwigMutantsIter::new(LanguageId::Javascript, &base, &twig).is_err()); } // core[verify mutant.iter.item] @@ -421,7 +421,7 @@ mod tests { fn mutants_iter_walks_tree_and_returns_mutants() { let (_dir, base) = make_js_base("const a = 1;"); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let iter = MutantsIter::new(LanguageId::Javascript, &base, &twig).unwrap(); + let iter = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig).unwrap(); let mutants: Vec<_> = iter.collect(); assert!(mutants.is_empty()); } @@ -432,7 +432,7 @@ mod tests { let js = "function foo() { return 1; }\nfunction bar() { return 2; }"; let (_dir, base) = make_js_base(js); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let mutants: Vec<_> = MutantsIter::new(LanguageId::Javascript, &base, &twig) + let mutants: Vec<_> = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig) .unwrap() .collect(); let blocks: Vec<_> = mutants @@ -450,7 +450,7 @@ mod tests { let js = "if (x > 0) { console.log(x); }"; let (_dir, base) = make_js_base(js); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let mutants: Vec<_> = MutantsIter::new(LanguageId::Javascript, &base, &twig) + let mutants: Vec<_> = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig) .unwrap() .collect(); let conditions: Vec<_> = mutants @@ -466,7 +466,7 @@ mod tests { let js = "while (i < 10) { i++; }"; let (_dir, base) = make_js_base(js); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let mutants: Vec<_> = MutantsIter::new(LanguageId::Javascript, &base, &twig) + let mutants: Vec<_> = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig) .unwrap() .collect(); let conditions: Vec<_> = mutants @@ -482,7 +482,7 @@ mod tests { let js = "for (let i = 0; i < 10; i++) { console.log(i); }"; let (_dir, base) = make_js_base(js); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let mutants: Vec<_> = MutantsIter::new(LanguageId::Javascript, &base, &twig) + let mutants: Vec<_> = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig) .unwrap() .collect(); let conditions: Vec<_> = mutants @@ -501,7 +501,7 @@ mod tests { let js = "const x = a + b;"; let (_dir, base) = make_js_base(js); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let mutants: Vec<_> = MutantsIter::new(LanguageId::Javascript, &base, &twig) + let mutants: Vec<_> = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig) .unwrap() .collect(); let adds: Vec<_> = mutants @@ -517,7 +517,7 @@ mod tests { let js = "const x = a - b;"; let (_dir, base) = make_js_base(js); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let mutants: Vec<_> = MutantsIter::new(LanguageId::Javascript, &base, &twig) + let mutants: Vec<_> = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig) .unwrap() .collect(); let subs: Vec<_> = mutants @@ -534,7 +534,7 @@ mod tests { let js = "const x = a * b;"; let (_dir, base) = make_js_base(js); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); - let mutants: Vec<_> = MutantsIter::new(LanguageId::Javascript, &base, &twig) + let mutants: Vec<_> = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig) .unwrap() .collect(); let binary_ops: Vec<_> = mutants diff --git a/crates/bough-core/src/phase.rs b/crates/bough-core/src/phase.rs index a5b92325..893afa1f 100644 --- a/crates/bough-core/src/phase.rs +++ b/crates/bough-core/src/phase.rs @@ -1,6 +1,6 @@ -use std::collections::HashMap; #[cfg(test)] use std::path::{Path, PathBuf}; +use std::{collections::HashMap, time::Duration}; use crate::file::{File, Root, Twig}; @@ -37,8 +37,8 @@ pub struct Phase<'a, R: Root> { pwd: Twig, env: HashMap, cmd: Vec, - timeout_absolute: Option, - timeout_relative: Option, + timeout_absolute: Option, + timeout_relative: Option, } impl<'a, R: Root> Phase<'a, R> { @@ -58,11 +58,11 @@ impl<'a, R: Root> Phase<'a, R> { &self.cmd } - pub fn timeout_absolute(&self) -> Option { + pub fn timeout_absolute(&self) -> Option { self.timeout_absolute } - pub fn timeout_relative(&self) -> Option { + pub fn timeout_relative(&self) -> Option { self.timeout_relative } @@ -72,7 +72,10 @@ impl<'a, R: Root> Phase<'a, R> { // core[impl phase.run.timeout] // core[impl phase.run.timeout.absolute] // core[impl phase.run.timeout.relative] - pub fn run(&self, reference_duration: Option) -> Result { + pub fn run( + &self, + reference_duration: Option, + ) -> Result { use wait_timeout::ChildExt; if self.cmd.is_empty() { @@ -132,8 +135,11 @@ impl<'a, R: Root> Phase<'a, R> { }) } - fn effective_timeout(&self, reference_duration: Option) -> Option { - let absolute = self.timeout_absolute.map(std::time::Duration::from_secs); + fn effective_timeout( + &self, + reference_duration: Option, + ) -> Option { + let absolute = self.timeout_absolute; let relative = match (self.timeout_relative, reference_duration) { (Some(multiplier), Some(ref_dur)) => Some(ref_dur * multiplier as u32), _ => None, @@ -218,7 +224,10 @@ mod tests { fn phase_holds_pwd_twig() { let root = TestRoot(PathBuf::from("/tmp/project")); let pwd = crate::file::Twig::new(PathBuf::from("src/test")).unwrap(); - let phase = Phase { pwd, ..make_phase(&root) }; + let phase = Phase { + pwd, + ..make_phase(&root) + }; assert_eq!(phase.pwd().path(), Path::new("src/test")); } @@ -227,7 +236,10 @@ mod tests { fn phase_holds_env_vars() { let root = TestRoot(PathBuf::from("/tmp/project")); let env = HashMap::from([("NODE_ENV".into(), "test".into())]); - let phase = Phase { env, ..make_phase(&root) }; + let phase = Phase { + env, + ..make_phase(&root) + }; assert_eq!(phase.env()["NODE_ENV"], "test"); } @@ -236,7 +248,10 @@ mod tests { fn phase_holds_cmd() { let root = TestRoot(PathBuf::from("/tmp/project")); let cmd = vec!["npx".into(), "vitest".into(), "run".into()]; - let phase = Phase { cmd, ..make_phase(&root) }; + let phase = Phase { + cmd, + ..make_phase(&root) + }; assert_eq!(phase.cmd(), &["npx", "vitest", "run"]); } @@ -244,9 +259,13 @@ mod tests { #[test] fn phase_holds_timeout() { let root = TestRoot(PathBuf::from("/tmp/project")); - let phase = Phase { timeout_absolute: Some(30), timeout_relative: Some(3), ..make_phase(&root) }; - assert_eq!(phase.timeout_absolute(), Some(30)); - assert_eq!(phase.timeout_relative(), Some(3)); + let phase = Phase { + timeout_absolute: Some(Duration::from_secs(30)), + timeout_relative: Some(3.0), + ..make_phase(&root) + }; + assert_eq!(phase.timeout_absolute(), Some(Duration::from_secs(30))); + assert_eq!(phase.timeout_relative(), Some(3.0)); } // core[verify phase.out] @@ -325,7 +344,10 @@ mod tests { }; let outcome = phase.run(None).unwrap(); let out = String::from_utf8_lossy(outcome.stdout()); - assert!(out.trim().ends_with("subdir"), "pwd should end with subdir, got: {out}"); + assert!( + out.trim().ends_with("subdir"), + "pwd should end with subdir, got: {out}" + ); } // core[verify phase.run.env] @@ -340,7 +362,10 @@ mod tests { ..make_phase(&root) }; let outcome = phase.run(None).unwrap(); - assert_eq!(String::from_utf8_lossy(outcome.stdout()).trim(), "hello_env"); + assert_eq!( + String::from_utf8_lossy(outcome.stdout()).trim(), + "hello_env" + ); } // core[verify phase.out.stdio] @@ -380,7 +405,8 @@ mod tests { let phase = Phase { cmd: vec!["sleep".into(), "10".into()], pwd: crate::file::Twig::new(PathBuf::from(".")).unwrap(), - timeout_absolute: Some(1), timeout_relative: None, + timeout_absolute: Some(Duration::from_millis(100)), + timeout_relative: None, ..make_phase(&root) }; let outcome = phase.run(None).unwrap(); @@ -396,7 +422,8 @@ mod tests { let phase = Phase { cmd: vec!["echo".into(), "fast".into()], pwd: crate::file::Twig::new(PathBuf::from(".")).unwrap(), - timeout_absolute: Some(10), timeout_relative: None, + timeout_absolute: Some(Duration::from_millis(100)), + timeout_relative: None, ..make_phase(&root) }; let outcome = phase.run(None).unwrap(); @@ -412,7 +439,8 @@ mod tests { let phase = Phase { cmd: vec!["sleep".into(), "10".into()], pwd: crate::file::Twig::new(PathBuf::from(".")).unwrap(), - timeout_absolute: None, timeout_relative: Some(2), + timeout_absolute: None, + timeout_relative: Some(2.0), ..make_phase(&root) }; let ref_dur = std::time::Duration::from_millis(500); @@ -429,7 +457,8 @@ mod tests { let phase = Phase { cmd: vec!["echo".into(), "ok".into()], pwd: crate::file::Twig::new(PathBuf::from(".")).unwrap(), - timeout_absolute: None, timeout_relative: Some(2), + timeout_absolute: None, + timeout_relative: Some(2.0), ..make_phase(&root) }; let outcome = phase.run(None).unwrap();