Something went wrong. Try again.
A polyglot incremental mutation tester
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424use std::{ collections::{HashMap, HashSet}, path::PathBuf, sync::Arc,};
use bough_typed_hash::TypedHashable;use chrono::Duration;use tracing::info;
use crate::{ Factor, LanguageId, Status, base::Base, facet_disk_store::FacetDiskStore, mutation::{Mutation, MutationHash}, state::State, twig::TwigsIterBuilder, workspace::{Workspace, WorkspaceId},};
pub trait Config { fn get_workers_count(&self) -> u64;
fn get_bough_state_dir(&self) -> PathBuf; fn get_base_root_path(&self) -> PathBuf; fn get_base_include_globs(&self) -> impl Iterator<Item = String>; fn get_base_exclude_globs(&self) -> impl Iterator<Item = String>;
fn get_langs(&self) -> impl Iterator<Item = LanguageId>; fn get_lang_include_globs(&self, language_id: LanguageId) -> impl Iterator<Item = String>; fn get_lang_exclude_globs(&self, language_id: LanguageId) -> impl Iterator<Item = String>; fn get_lang_skip_queries(&self, language_id: LanguageId) -> impl Iterator<Item = String>;
fn get_test_cmd(&self) -> String; fn get_test_pwd(&self) -> PathBuf; fn get_test_env(&self) -> HashMap<String, String>; fn get_test_timeout(&self, reference: Option<Duration>) -> Duration;
fn get_init_cmd(&self) -> Option<String>; fn get_init_pwd(&self) -> PathBuf; fn get_init_env(&self) -> HashMap<String, String>; fn get_init_timeout(&self, reference: Option<Duration>) -> Duration;
fn get_reset_cmd(&self) -> Option<String>; fn get_reset_pwd(&self) -> PathBuf; fn get_reset_env(&self) -> HashMap<String, String>; fn get_reset_timeout(&self, reference: Option<Duration>) -> Duration;
fn get_find_number(&self) -> usize; fn get_find_number_per_file(&self) -> usize; fn get_find_factors(&self) -> Vec<Factor>;}
#[derive(Debug)]pub enum Error { File(crate::file::Error), Io(std::io::Error), Workspace(crate::workspace::Error), Phase(crate::phase::Error), NoCmdConfigured, AbsolutePwd(PathBuf), InvalidTimeout,}
pub struct TestConfig {}
pub struct Session<C: Config> { config: C, base: Arc<Base>, workspaces: Vec<WorkspaceId>, mutations_state: FacetDiskStore<MutationHash, State>, mutations_needing_test: Vec<MutationHash>,}
impl<C: Config> Session<C> { // bough[impl session.init+2] pub fn new(config: C) -> Result<Self, Error> { info!("initializing session"); let mut base_twigs_iter_builder = TwigsIterBuilder::new(); for include in config.get_base_include_globs() { base_twigs_iter_builder = base_twigs_iter_builder.with_include_glob(&include); } for exclude in config.get_base_exclude_globs() { base_twigs_iter_builder = base_twigs_iter_builder.with_exclude_glob(&exclude); }
let mut base = Base::new(config.get_base_root_path(), base_twigs_iter_builder)?;
for lang in config.get_langs() { let mut lang_twigs_iter_builder = TwigsIterBuilder::new(); for include in config.get_lang_include_globs(lang) { lang_twigs_iter_builder = lang_twigs_iter_builder.with_include_glob(&include); } for exclude in config.get_lang_exclude_globs(lang) { lang_twigs_iter_builder = lang_twigs_iter_builder.with_exclude_glob(&exclude); } let skip_queries: Vec<String> = config.get_lang_skip_queries(lang).collect(); base.add_mutator(lang, lang_twigs_iter_builder, skip_queries); }
// bough[impl session.init.state.attach] let mutations_state = FacetDiskStore::<<Mutation as TypedHashable>::Hash, State>::new( config.get_bough_state_dir().join("state"), );
let mutations_needing_test = Self::derive_mutations_needing_testing(&mutations_state);
Ok(Self { config, base: Arc::new(base), workspaces: Vec::new(), mutations_state, mutations_needing_test, }) }
pub fn base(&self) -> &Base { &self.base }
pub fn resolve_mutation( &self, unvalidated: bough_typed_hash::UnvalidatedHash, ) -> Result<Mutation, Error> { let mutations: Vec<Mutation> = self.base.mutations().collect::<Result<_, _>>()?; let hashes: Vec<MutationHash> = mutations.iter().map(|m| m.hash().expect("hash")).collect(); let matched = unvalidated .validate(&hashes) .expect("hash resolution failed"); Ok(mutations .into_iter() .find(|m| m.hash().unwrap() == matched) .unwrap()) }
fn derive_mutations_needing_testing( mutations_state: &FacetDiskStore<MutationHash, State>, ) -> Vec<MutationHash> { mutations_state .keys() .filter(|key| { if let Some(val) = mutations_state.get(key) { if let Some(status) = val.status() { if *status == Status::Caught { return false; } } }; return true; }) .collect() }
pub fn get_next_mutation_needing_test(&mut self) -> Option<MutationHash> { self.mutations_needing_test.pop() }
pub fn get_count_mutation_needing_test(&mut self) -> usize { self.mutations_needing_test.len() }
// bough[impl session.tend.state.add-missing] pub fn tend_add_missing_states(&mut self) -> Result<Vec<MutationHash>, Error> { let mutations_in_base: HashSet<Mutation> = self.base.mutations().collect::<Result<_, _>>()?; let mut added = Vec::new();
for mutation in &mutations_in_base { let hash = mutation.hash().expect("hashing should not fail"); if self.mutations_state.get(&hash).is_none() { let state = State::new(mutation.clone()); self.mutations_state .set(hash.clone(), state) .expect("writing state should not fail"); added.push(hash); } }
self.mutations_needing_test = Self::derive_mutations_needing_testing(&self.mutations_state);
Ok(added) }
// bough[impl session.tend.state.remove-stale] pub fn tend_remove_stale_states(&mut self) -> Result<Vec<MutationHash>, Error> { let mutations_in_base: HashSet<Mutation> = self.base.mutations().collect::<Result<_, _>>()?; let hashes_in_base: HashSet<MutationHash> = mutations_in_base .iter() .map(|m| m.hash().expect("hashing should not fail")) .collect();
let stale_keys: Vec<MutationHash> = self .mutations_state .keys() .filter(|k| !hashes_in_base.contains(k)) .collect();
for key in &stale_keys { self.mutations_state.remove(key); }
self.mutations_needing_test = Self::derive_mutations_needing_testing(&self.mutations_state);
Ok(stale_keys) }
// bough[impl session.tend.workspaces] // bough[impl session.tend.workspaces.bind] // bough[impl session.tend.workspaces.bind.validate-unchanged.rm] // bough[impl session.tend.workspaces.bind.validate-unchanged.forget] // bough[impl session.tend.workspaces.new] // bough[impl session.tend.workspaces.surplus] pub fn tend_workspaces(&mut self, desired_count: usize) -> Result<Vec<WorkspaceId>, Error> { let workspaces_dir = self.config.get_bough_state_dir().join("workspaces");
let existing_ids: Vec<WorkspaceId> = if workspaces_dir.join("work").exists() { std::fs::read_dir(workspaces_dir.join("work"))? .flatten() .filter_map(|entry| { let name = entry.file_name().to_string_lossy().into_owned(); WorkspaceId::parse(&name).ok() }) .collect() } else { vec![] };
let mut valid: Vec<WorkspaceId> = Vec::new(); for id in &existing_ids { match Workspace::bind(workspaces_dir.clone(), id, self.base.clone()) { Ok(_ws) => valid.push(id.clone()), Err(_) => { let ws_path = workspaces_dir.join("work").join(id.as_str()); if ws_path.exists() { std::fs::remove_dir_all(&ws_path)?; } } } }
if valid.len() > desired_count { for id in valid.drain(desired_count..) { let ws_path = workspaces_dir.join("work").join(id.as_str()); if ws_path.exists() { std::fs::remove_dir_all(&ws_path)?; } } }
while valid.len() < desired_count { let ws = Workspace::new(workspaces_dir.clone(), self.base.clone())?; valid.push(ws.id().clone()); }
self.workspaces = valid.clone(); Ok(valid) }
// bough[impl session.init.state.get] pub fn get_state(&self) -> &FacetDiskStore<MutationHash, State> { &self.mutations_state }
// bough[impl session.init.workspaces.get-ids] pub fn workspace_ids(&self) -> &[WorkspaceId] { &self.workspaces }
pub fn set_state( &mut self, mutation: &Mutation, status: crate::state::Status, ) -> Result<(), Error> { let hash = mutation.hash().expect("hashing should not fail"); if let Some(existing) = self.mutations_state.get(&hash) { if existing.status() == Some(&status) { return Ok(()); } } let mut state = State::new(mutation.clone()); state.set_outcome(status); self.mutations_state.set(hash, state).map_err(Error::Io)?; Ok(()) }
pub fn find_best_mutations(&self) -> Result<Vec<(MutationHash, State, f64)>, Error> { if self.mutations_needing_test.is_empty() { return Ok(Vec::new()); }
let factors = self.config.get_find_factors(); let number = self.config.get_find_number(); let number_per_file = self.config.get_find_number_per_file();
let needing_test: Vec<(MutationHash, State)> = self .mutations_needing_test .iter() .filter_map(|hash| { let state = self.mutations_state.get(hash)?; Some((hash.clone(), state)) }) .collect();
let all_states: Vec<State> = self .mutations_state .keys() .filter_map(|k| self.mutations_state.get(&k)) .collect();
let mut scorers: Vec<crate::mutation_score::MutationScorer> = factors .iter() .map(|f| crate::mutation_score::MutationScorer::new(self.base.clone(), *f)) .collect();
let mut scores_per_mutation: Vec<Vec<crate::mutation_score::OpaqueScore>> = Vec::with_capacity(needing_test.len());
for (_, state) in &needing_test { let mut mutation_scores = Vec::with_capacity(factors.len()); for scorer in &mut scorers { let score = scorer.score(state.mutation().clone(), &all_states); mutation_scores.push(score); } scores_per_mutation.push(mutation_scores); }
let viewers: Vec<crate::mutation_score::MutationScoreViewer> = scorers.into_iter().map(|s| s.into_viewer()).collect();
let mut scored: Vec<(MutationHash, State, f64)> = needing_test .into_iter() .zip(scores_per_mutation) .map(|((hash, state), scores)| { let composite = if viewers.is_empty() { 0.0 } else { let sum: f64 = scores .into_iter() .zip(&viewers) .map(|(s, v)| v.normalize(s)) .sum(); sum / viewers.len() as f64 }; (hash, state, composite) }) .collect();
scored.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
let mut per_file_counts: HashMap<crate::file::Twig, usize> = HashMap::new(); scored.retain(|(_, state, _)| { let twig = state.mutation().mutant().twig().clone(); let count = per_file_counts.entry(twig).or_insert(0); *count += 1; *count <= number_per_file });
scored.truncate(number);
Ok(scored) }
pub fn bind_workspace(&self, workspace_id: &WorkspaceId) -> Result<Workspace, Error> { let workspaces_dir = self.config.get_bough_state_dir().join("workspaces"); Ok(Workspace::bind( workspaces_dir, workspace_id, self.base.clone(), )?) }
pub fn bind_dirty_workspace(&self, workspace_id: &WorkspaceId) -> Workspace { let workspaces_dir = self.config.get_bough_state_dir().join("workspaces"); Workspace::bind_dirty(workspaces_dir, workspace_id, self.base.clone()) }}
impl From<crate::file::Error> for Error { fn from(e: crate::file::Error) -> Self { Error::File(e) }}
impl From<std::io::Error> for Error { fn from(e: std::io::Error) -> Self { Error::Io(e) }}
impl From<crate::workspace::Error> for Error { fn from(e: crate::workspace::Error) -> Self { Error::Workspace(e) }}
impl From<crate::phase::Error> for Error { fn from(e: crate::phase::Error) -> Self { Error::Phase(e) }}
#[cfg(test)]mod tests { use super::*;
#[derive(Clone)] struct MinimalConfig { root: PathBuf, state_dir: PathBuf, langs: Vec<LanguageId>, lang_includes: Vec<String>, workers_count: u64, test_cmd: String, test_pwd: PathBuf, init_cmd: Option<String>, reset_cmd: Option<String>, find_number: usize, find_number_per_file: usize, }
impl Config for MinimalConfig { fn get_base_root_path(&self) -> PathBuf { self.root.clone() }
fn get_base_include_globs(&self) -> impl Iterator<Item = String> { self.lang_includes.clone().into_iter() }
fn get_base_exclude_globs(&self) -> impl Iterator<Item = String> { Vec::<String>::new().into_iter() }
fn get_lang_include_globs(&self, _language_id: LanguageId) -> impl Iterator<Item = String> { self.lang_includes.clone().into_iter() }
fn get_lang_exclude_globs(&self, _language_id: LanguageId) -> impl Iterator<Item = String> { Vec::<String>::new().into_iter() }
fn get_lang_skip_queries(&self, _language_id: LanguageId) -> impl Iterator<Item = String> { Vec::<String>::new().into_iter() }
fn get_langs(&self) -> impl Iterator<Item = LanguageId> { self.langs.clone().into_iter() }
fn get_bough_state_dir(&self) -> PathBuf { self.state_dir.clone() }
fn get_workers_count(&self) -> u64 { self.workers_count }
fn get_test_cmd(&self) -> String { self.test_cmd.clone() }
fn get_test_pwd(&self) -> PathBuf { self.test_pwd.clone() }
fn get_test_env(&self) -> HashMap<String, String> { HashMap::new() }
fn get_test_timeout(&self, _reference: Option<Duration>) -> Duration { Duration::seconds(30) }
fn get_init_cmd(&self) -> Option<String> { self.init_cmd.clone() }
fn get_init_pwd(&self) -> PathBuf { PathBuf::from(".") }
fn get_init_env(&self) -> HashMap<String, String> { HashMap::new() }
fn get_init_timeout(&self, _reference: Option<Duration>) -> Duration { Duration::seconds(30) }
fn get_reset_cmd(&self) -> Option<String> { self.reset_cmd.clone() }
fn get_reset_pwd(&self) -> PathBuf { PathBuf::from(".") }
fn get_reset_env(&self) -> HashMap<String, String> { HashMap::new() }
fn get_reset_timeout(&self, _reference: Option<Duration>) -> Duration { Duration::seconds(30) }
fn get_find_number(&self) -> usize { self.find_number }
fn get_find_number_per_file(&self) -> usize { self.find_number_per_file }
fn get_find_factors(&self) -> Vec<Factor> { vec![Factor::EncompasingMissedMutationsCount, Factor::TSNodeDepth] } }
fn make_js_config(dir: &std::path::Path) -> MinimalConfig { std::fs::create_dir_all(dir.join("src")).unwrap(); MinimalConfig { root: dir.to_path_buf(), state_dir: dir.join("bough"), langs: vec![LanguageId::Javascript], lang_includes: vec!["src/**/*.js".to_string()], workers_count: 0, test_cmd: "echo test".to_string(), test_pwd: PathBuf::from("."), init_cmd: None, reset_cmd: None, find_number: 1, find_number_per_file: 1, } }
// bough[verify session.init+2] #[test] fn session_new_creates_session_from_config() { let dir = tempfile::tempdir().unwrap(); let config = MinimalConfig { root: dir.path().to_path_buf(), state_dir: dir.path().join("state"), langs: vec![], lang_includes: vec![], workers_count: 0, test_cmd: "echo test".to_string(), test_pwd: PathBuf::from("."), init_cmd: None, reset_cmd: None, find_number: 1, find_number_per_file: 1, }; let session = Session::new(config); assert!(session.is_ok()); }
// bough[verify session.init+2] #[test] fn session_new_fails_with_invalid_root() { let dir = tempfile::tempdir().unwrap(); let config = MinimalConfig { root: PathBuf::from("not/absolute"), state_dir: dir.path().join("state"), langs: vec![], lang_includes: vec![], workers_count: 0, test_cmd: "echo test".to_string(), test_pwd: PathBuf::from("."), init_cmd: None, reset_cmd: None, find_number: 1, find_number_per_file: 1, }; let session = Session::new(config); assert!(session.is_err()); }
// bough[verify session.init.state.attach] #[test] fn session_new_creates_state_dir() { let dir = tempfile::tempdir().unwrap(); let config = MinimalConfig { root: dir.path().to_path_buf(), state_dir: dir.path().join("my-state"), langs: vec![], lang_includes: vec![], workers_count: 0, test_cmd: "echo test".to_string(), test_pwd: PathBuf::from("."), init_cmd: None, reset_cmd: None, find_number: 1, find_number_per_file: 1, }; let _session = Session::new(config).unwrap(); assert!( dir.path().join("my-state/state").exists(), "mutations_state dir should be created at <state_dir>/state" ); }
fn state_dir(dir: &std::path::Path) -> PathBuf { dir.join("bough/state") }
fn state_files(dir: &std::path::Path) -> Vec<PathBuf> { let sd = state_dir(dir); if !sd.exists() { return vec![]; } let mut files: Vec<PathBuf> = std::fs::read_dir(&sd) .unwrap() .flatten() .map(|e| e.path()) .filter(|p| p.extension().is_some_and(|e| e == "json")) .collect(); files.sort(); files }
// bough[verify session.init.state.get] #[test] fn session_get_state_reads_back_disk_files() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); let config = make_js_config(dir.path()); let session = Session::new(config).unwrap(); let state = session.get_state(); let disk_count = state_files(dir.path()).len(); let api_count = state.keys().count(); assert_eq!(api_count, disk_count); for key in state.keys() { assert!(state.get(&key).is_some()); } }
fn workspace_dirs(dir: &std::path::Path) -> Vec<PathBuf> { let ws_dir = dir.join("bough/workspaces/work"); if !ws_dir.exists() { return vec![]; } let mut dirs: Vec<PathBuf> = std::fs::read_dir(&ws_dir) .unwrap() .flatten() .map(|e| e.path()) .filter(|p| p.is_dir()) .collect(); dirs.sort(); dirs }
// bough[verify session.init.workspaces] #[test] fn session_creates_workspaces_in_state_dir() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_workspaces(3).unwrap(); let dirs = workspace_dirs(dir.path()); assert_eq!(dirs.len(), 3, "expected 3 workspaces, got: {dirs:?}"); for d in &dirs { assert!( d.join("src/a.js").exists(), "workspace should contain source files" ); } }
// bough[verify session.init.workspaces.bind] #[test] fn session_binds_existing_workspaces() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_workspaces(2).unwrap(); let dirs_before = workspace_dirs(dir.path()); assert_eq!(dirs_before.len(), 2);
let config2 = make_js_config(dir.path()); let _session2 = Session::new(config2).unwrap(); let dirs_after = workspace_dirs(dir.path()); assert_eq!( dirs_after.len(), 2, "should reuse existing workspaces, not create new ones" ); assert_eq!(dirs_before, dirs_after, "workspace dirs should be the same"); }
// bough[verify session.init.workspaces.get-ids] #[test] fn session_workspace_ids_returns_created_ids() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_workspaces(3).unwrap(); let ids = session.workspace_ids(); assert_eq!(ids.len(), 3); for id in ids { assert_eq!(id.as_str().len(), 8); } }
// bough[verify session.tend.state.add-missing] #[test] fn tend_add_missing_states_adds_new_mutations() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap(); let initial_count = session.get_state().keys().count(); assert!(initial_count > 0);
std::fs::write(dir.path().join("src/b.js"), "function bar() { return 2; }").unwrap();
let mut config2 = make_js_config(dir.path()); config2.lang_includes = vec!["src/**/*.js".to_string()]; let mut session2 = Session::new(config2).unwrap(); session2.tend_add_missing_states().unwrap(); let count_after = session2.get_state().keys().count(); assert!(count_after > initial_count);
let added = session2.tend_add_missing_states().unwrap(); assert!( added.is_empty(), "no new mutations since session2 was created fresh" ); }
// bough[verify session.tend.state.add-missing] #[test] fn tend_add_missing_states_returns_newly_added_hashes() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); let initial_count = session.get_state().keys().count();
std::fs::write(dir.path().join("src/b.js"), "function bar() { return 2; }").unwrap(); Arc::get_mut(&mut session.base).unwrap().add_mutator( LanguageId::Javascript, crate::twig::TwigsIterBuilder::new().with_include_glob("src/**/*.js"), Vec::new(), );
let added = session.tend_add_missing_states().unwrap(); assert!(!added.is_empty()); let final_count = session.get_state().keys().count(); assert_eq!(final_count, initial_count + added.len()); }
// bough[verify session.tend.workspaces] #[test] fn tend_workspaces_returns_workspace_ids() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let mut config = make_js_config(dir.path()); config.workers_count = 0; let mut session = Session::new(config).unwrap(); let ids = session.tend_workspaces(3).unwrap(); assert_eq!(ids.len(), 3); }
// bough[verify session.tend.workspaces.bind] #[test] fn tend_workspaces_binds_existing() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_workspaces(2).unwrap(); let dirs_before = workspace_dirs(dir.path()); assert_eq!(dirs_before.len(), 2);
let ids = session.tend_workspaces(2).unwrap(); assert_eq!(ids.len(), 2); let dirs_after = workspace_dirs(dir.path()); assert_eq!(dirs_before, dirs_after); }
// bough[verify session.tend.workspaces.bind.validate-unchanged.rm] // bough[verify session.tend.workspaces.bind.validate-unchanged.forget] #[test] fn tend_workspaces_removes_modified_workspace_from_disk() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_workspaces(2).unwrap(); let dirs_before = workspace_dirs(dir.path()); assert_eq!(dirs_before.len(), 2);
std::fs::write(dirs_before[0].join("src/a.js"), "MUTATED").unwrap();
let ids = session.tend_workspaces(2).unwrap(); assert_eq!(ids.len(), 2); assert!( !dirs_before[0].exists(), "modified workspace dir should be removed from disk" ); }
// bough[verify session.tend.workspaces.new] #[test] fn tend_workspaces_creates_new_to_reach_desired_count() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_workspaces(1).unwrap(); assert_eq!(workspace_dirs(dir.path()).len(), 1);
let ids = session.tend_workspaces(3).unwrap(); assert_eq!(ids.len(), 3); assert_eq!(workspace_dirs(dir.path()).len(), 3); }
// bough[verify session.tend.workspaces.surplus] #[test] fn tend_workspaces_removes_surplus() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_workspaces(4).unwrap(); assert_eq!(workspace_dirs(dir.path()).len(), 4);
let ids = session.tend_workspaces(2).unwrap(); assert_eq!(ids.len(), 2); assert_eq!(workspace_dirs(dir.path()).len(), 2); }
// bough[verify session.tend.state.remove-stale] #[test] fn tend_remove_stale_states_removes_orphaned_entries() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); std::fs::write(dir.path().join("src/b.js"), "function bar() { return 2; }").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap(); let initial_count = session.get_state().keys().count(); assert!(initial_count > 0);
std::fs::remove_file(dir.path().join("src/b.js")).unwrap(); session.base = Arc::new( crate::base::Base::new( dir.path().to_path_buf(), crate::twig::TwigsIterBuilder::new().with_include_glob("src/**/*.js"), ) .unwrap(), ); Arc::get_mut(&mut session.base).unwrap().add_mutator( LanguageId::Javascript, crate::twig::TwigsIterBuilder::new().with_include_glob("src/**/*.js"), Vec::new(), );
let removed = session.tend_remove_stale_states().unwrap(); assert!(!removed.is_empty()); let final_count = session.get_state().keys().count(); assert!(final_count < initial_count); assert_eq!(final_count + removed.len(), initial_count); }
// bough[verify session.tend.state.remove-stale] #[test] fn tend_remove_stale_states_returns_empty_when_nothing_stale() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap();
let removed = session.tend_remove_stale_states().unwrap(); assert!(removed.is_empty()); }
#[test] fn run_test_in_base_executes_test_cmd() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.test_cmd = "echo hello".to_string(); let session = Session::new(config.clone()).unwrap(); let outcome = session.base().run_test(&config, None).unwrap(); assert!(matches!( outcome, crate::phase::PhaseOutcome::Completed { exit_code: 0, .. } )); assert_eq!(String::from_utf8_lossy(outcome.stdout()).trim(), "hello"); }
#[test] fn run_test_in_base_uses_pwd() { let dir = tempfile::tempdir().unwrap(); let sub = dir.path().join("src"); std::fs::create_dir_all(&sub).unwrap(); let mut config = make_js_config(dir.path()); config.test_cmd = "pwd".to_string(); config.test_pwd = PathBuf::from("src"); let session = Session::new(config.clone()).unwrap(); let outcome = session.base().run_test(&config, None).unwrap(); let out = String::from_utf8_lossy(outcome.stdout()); assert!( out.trim().ends_with("src"), "pwd should end with src, got: {out}" ); }
#[test] fn run_test_in_base_errors_on_absolute_pwd() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.test_pwd = PathBuf::from("/absolute/path"); let session = Session::new(config.clone()).unwrap(); let result = session.base().run_test(&config, None); assert!(matches!(result, Err(crate::phase::Error::AbsolutePwd(_)))); }
#[test] fn run_test_in_base_nonzero_exit_is_ok() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.test_cmd = "false".to_string(); let session = Session::new(config.clone()).unwrap(); let outcome = session.base().run_test(&config, None).unwrap(); assert!(matches!( outcome, crate::phase::PhaseOutcome::Completed { exit_code: 1, .. } )); }
#[test] fn run_init_in_base_executes_init_cmd() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.init_cmd = Some("echo init".to_string()); let session = Session::new(config.clone()).unwrap(); let outcome = session.base().run_init(&config, None).unwrap(); assert!(matches!( outcome, crate::phase::PhaseOutcome::Completed { exit_code: 0, .. } )); assert_eq!(String::from_utf8_lossy(outcome.stdout()).trim(), "init"); }
#[test] fn run_init_in_base_errors_when_no_cmd() { let dir = tempfile::tempdir().unwrap(); let config = make_js_config(dir.path()); let session = Session::new(config.clone()).unwrap(); let result = session.base().run_init(&config, None); assert!(matches!(result, Err(crate::phase::Error::NoCmdConfigured))); }
#[test] fn run_reset_in_base_executes_reset_cmd() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.reset_cmd = Some("echo reset".to_string()); let session = Session::new(config.clone()).unwrap(); let outcome = session.base().run_reset(&config, None).unwrap(); assert!(matches!( outcome, crate::phase::PhaseOutcome::Completed { exit_code: 0, .. } )); assert_eq!(String::from_utf8_lossy(outcome.stdout()).trim(), "reset"); }
#[test] fn run_reset_in_base_errors_when_no_cmd() { let dir = tempfile::tempdir().unwrap(); let config = make_js_config(dir.path()); let session = Session::new(config.clone()).unwrap(); let result = session.base().run_reset(&config, None); assert!(matches!(result, Err(crate::phase::Error::NoCmdConfigured))); }
#[test] fn run_test_in_workspace_executes_in_workspace_root() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.test_cmd = "pwd".to_string(); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let mut session = Session::new(config.clone()).unwrap(); let ids = session.tend_workspaces(1).unwrap(); let workspace = session.bind_dirty_workspace(&ids[0]); let outcome = workspace.run_test(&config, None).unwrap(); let out = String::from_utf8_lossy(outcome.stdout()); assert!( out.trim().contains(ids[0].as_str()), "should run in workspace dir containing workspace id, got: {out}" ); }
#[test] fn run_test_in_workspace_uses_pwd_relative_to_workspace() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.test_cmd = "pwd".to_string(); config.test_pwd = PathBuf::from("src"); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let mut session = Session::new(config.clone()).unwrap(); let ids = session.tend_workspaces(1).unwrap(); let workspace = session.bind_dirty_workspace(&ids[0]); let outcome = workspace.run_test(&config, None).unwrap(); let out = String::from_utf8_lossy(outcome.stdout()); assert!( out.trim().ends_with("src"), "pwd should end with src, got: {out}" ); assert!( out.trim().contains(ids[0].as_str()), "should be inside workspace dir, got: {out}" ); }
#[test] fn run_init_in_workspace_executes_init_cmd() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.init_cmd = Some("echo workspace_init".to_string()); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let mut session = Session::new(config.clone()).unwrap(); let ids = session.tend_workspaces(1).unwrap(); let workspace = session.bind_workspace(&ids[0]).unwrap(); let outcome = workspace.run_init(&config, None).unwrap(); assert!(matches!( outcome, crate::phase::PhaseOutcome::Completed { exit_code: 0, .. } )); assert_eq!( String::from_utf8_lossy(outcome.stdout()).trim(), "workspace_init" ); }
#[test] fn run_init_in_workspace_errors_when_no_cmd() { let dir = tempfile::tempdir().unwrap(); let config = make_js_config(dir.path()); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let mut session = Session::new(config.clone()).unwrap(); let ids = session.tend_workspaces(1).unwrap(); let workspace = session.bind_workspace(&ids[0]).unwrap(); let result = workspace.run_init(&config, None); assert!(matches!(result, Err(crate::phase::Error::NoCmdConfigured))); }
#[test] fn run_reset_in_workspace_executes_reset_cmd() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.reset_cmd = Some("echo workspace_reset".to_string()); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let mut session = Session::new(config.clone()).unwrap(); let ids = session.tend_workspaces(1).unwrap(); let workspace = session.bind_workspace(&ids[0]).unwrap(); let outcome = workspace.run_reset(&config, None).unwrap(); assert!(matches!( outcome, crate::phase::PhaseOutcome::Completed { exit_code: 0, .. } )); assert_eq!( String::from_utf8_lossy(outcome.stdout()).trim(), "workspace_reset" ); }
#[test] fn run_reset_in_workspace_errors_when_no_cmd() { let dir = tempfile::tempdir().unwrap(); let config = make_js_config(dir.path()); std::fs::write(dir.path().join("src/a.js"), "const x = 1;").unwrap(); let mut session = Session::new(config.clone()).unwrap(); let ids = session.tend_workspaces(1).unwrap(); let workspace = session.bind_workspace(&ids[0]).unwrap(); let result = workspace.run_reset(&config, None); assert!(matches!(result, Err(crate::phase::Error::NoCmdConfigured))); }
#[test] fn run_test_in_base_splits_cmd_at_whitespace() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.test_cmd = "echo one two three".to_string(); let session = Session::new(config.clone()).unwrap(); let outcome = session.base().run_test(&config, None).unwrap(); assert_eq!( String::from_utf8_lossy(outcome.stdout()).trim(), "one two three" ); }
#[test] fn run_test_in_base_default_pwd_is_root() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); config.test_cmd = "pwd".to_string(); let session = Session::new(config.clone()).unwrap(); let outcome = session.base().run_test(&config, None).unwrap(); let out = String::from_utf8_lossy(outcome.stdout()); let actual = PathBuf::from(out.trim()).canonicalize().unwrap(); let expected = dir.path().canonicalize().unwrap(); assert_eq!(actual, expected); }
#[test] fn set_state_records_outcome_on_mutation() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap();
let mutation: Mutation = session.base().mutations().next().unwrap().unwrap(); let hash = mutation.hash().unwrap(); session .set_state(&mutation, crate::state::Status::Missed) .unwrap();
let state = session.get_state().get(&hash).unwrap(); assert!(state.has_outcome()); assert!(state.outcome_at().is_some()); }
#[test] fn set_state_persists_to_disk() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap();
let mutation: Mutation = session.base().mutations().next().unwrap().unwrap(); let hash = mutation.hash().unwrap(); session .set_state(&mutation, crate::state::Status::Caught) .unwrap();
let config2 = make_js_config(dir.path()); let session2 = Session::new(config2).unwrap(); let persisted = session2.get_state().get(&hash); assert!(persisted.is_some()); assert!(persisted.unwrap().has_outcome()); }
#[test] fn set_state_sets_at_automatically() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap();
let before = chrono::Utc::now() - chrono::Duration::seconds(1); let mutation: Mutation = session.base().mutations().next().unwrap().unwrap(); let hash = mutation.hash().unwrap(); session .set_state(&mutation, crate::state::Status::Missed) .unwrap(); let after = chrono::Utc::now() + chrono::Duration::seconds(1);
let state = session.get_state().get(&hash).unwrap(); let at = state.outcome_at().unwrap(); assert!( at >= before && at <= after, "expected {before} <= {at} <= {after}" ); }
#[test] fn set_state_overwrites_previous_outcome() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap();
let mutation: Mutation = session.base().mutations().next().unwrap().unwrap(); let hash = mutation.hash().unwrap(); session .set_state(&mutation, crate::state::Status::Missed) .unwrap(); let first_at = session .get_state() .get(&hash) .unwrap() .outcome_at() .unwrap();
std::thread::sleep(std::time::Duration::from_secs(1)); session .set_state(&mutation, crate::state::Status::Caught) .unwrap(); let second_at = session .get_state() .get(&hash) .unwrap() .outcome_at() .unwrap();
assert!(second_at > first_at); }
#[test] fn set_state_skips_write_when_status_unchanged() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap();
let mutation: Mutation = session.base().mutations().next().unwrap().unwrap(); let hash = mutation.hash().unwrap(); session .set_state(&mutation, crate::state::Status::Missed) .unwrap(); let first_at = session .get_state() .get(&hash) .unwrap() .outcome_at() .unwrap();
std::thread::sleep(std::time::Duration::from_secs(1)); session .set_state(&mutation, crate::state::Status::Missed) .unwrap(); let second_at = session .get_state() .get(&hash) .unwrap() .outcome_at() .unwrap();
assert_eq!(first_at, second_at); }
#[test] fn set_state_preserves_mutation() { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir_all(dir.path().join("src")).unwrap(); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); let config = make_js_config(dir.path()); let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap();
let mutation: Mutation = session.base().mutations().next().unwrap().unwrap(); let hash = mutation.hash().unwrap(); session .set_state(&mutation, crate::state::Status::Caught) .unwrap();
let state = session.get_state().get(&hash).unwrap(); assert!(state.has_outcome()); assert_eq!(state.mutation(), &mutation); }
#[test] fn find_best_mutations_empty_when_no_mutations() { let dir = tempfile::tempdir().unwrap(); let config = make_js_config(dir.path()); let session = Session::new(config).unwrap(); let results = session.find_best_mutations().unwrap(); assert!(results.is_empty()); }
#[test] fn find_best_mutations_returns_mutations_needing_test() { let dir = tempfile::tempdir().unwrap(); let config = make_js_config(dir.path()); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap(); let total = session.get_count_mutation_needing_test(); assert!(total > 0);
let results = session.find_best_mutations().unwrap(); assert_eq!(results.len(), 1); let (hash, state, score) = &results[0]; assert!(state.mutation().mutant().twig().path().ends_with("a.js")); assert!(*score >= 0.0 && *score <= 1.0); assert!(session.get_state().get(hash).is_some()); }
#[test] fn find_best_mutations_respects_number_limit() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); std::fs::write( dir.path().join("src/a.js"), "function foo() { return a + b; }", ) .unwrap(); config.find_number = 2; config.find_number_per_file = 10; let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap(); let total = session.get_count_mutation_needing_test(); assert!( total > 2, "need more than 2 mutations to test limit, got {total}" );
let results = session.find_best_mutations().unwrap(); assert_eq!(results.len(), 2); }
#[test] fn find_best_mutations_respects_number_per_file_limit() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); std::fs::write( dir.path().join("src/a.js"), "function foo() { return a + b; }", ) .unwrap(); std::fs::write( dir.path().join("src/b.js"), "function bar() { return c - d; }", ) .unwrap(); config.find_number = 100; config.find_number_per_file = 1; let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap();
let results = session.find_best_mutations().unwrap(); let a_count = results .iter() .filter(|(_, s, _)| s.mutation().mutant().twig().path().ends_with("a.js")) .count(); let b_count = results .iter() .filter(|(_, s, _)| s.mutation().mutant().twig().path().ends_with("b.js")) .count(); assert_eq!(a_count, 1); assert_eq!(b_count, 1); }
#[test] fn find_best_mutations_excludes_caught() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); std::fs::write(dir.path().join("src/a.js"), "function foo() { return 1; }").unwrap(); config.find_number = 100; config.find_number_per_file = 100; let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap(); let total_before = session.get_count_mutation_needing_test();
let mutation: Mutation = session.base().mutations().next().unwrap().unwrap(); session .set_state(&mutation, crate::state::Status::Caught) .unwrap(); session.mutations_needing_test = Session::<MinimalConfig>::derive_mutations_needing_testing(&session.mutations_state);
let results = session.find_best_mutations().unwrap(); assert_eq!(results.len(), total_before - 1); }
#[test] fn find_best_mutations_sorted_by_score_descending() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); std::fs::write(dir.path().join("src/a.js"), "if (x) { return a + b; }").unwrap(); config.find_number = 100; config.find_number_per_file = 100; let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap();
let results = session.find_best_mutations().unwrap(); assert!(results.len() > 1); for w in results.windows(2) { assert!( w[0].2 >= w[1].2, "results should be sorted descending by score: {} >= {}", w[0].2, w[1].2 ); } }
#[test] fn find_best_mutations_scores_between_zero_and_one() { let dir = tempfile::tempdir().unwrap(); let mut config = make_js_config(dir.path()); std::fs::write(dir.path().join("src/a.js"), "if (x) { return a + b; }").unwrap(); config.find_number = 100; config.find_number_per_file = 100; let mut session = Session::new(config).unwrap(); session.tend_add_missing_states().unwrap();
let results = session.find_best_mutations().unwrap(); for (_, _, score) in &results { assert!(*score >= 0.0 && *score <= 1.0, "score {score} out of range"); } }}