fallow_api/mutation/
last_report.rs1use std::path::PathBuf;
10
11use crate::analysis_context::resolve_programmatic_analysis_context;
12use crate::{AnalysisOptions, ProgrammaticError};
13
14const LAST_REPORT_FILE: &str = "last-report.json";
16
17fn cache_error(message: String) -> ProgrammaticError {
18 ProgrammaticError::new(message, 2).with_code("FALLOW_MUTATION_CACHE_WRITE_FAILED")
19}
20
21fn cache_dir(analysis: &AnalysisOptions) -> Result<PathBuf, ProgrammaticError> {
23 resolve_programmatic_analysis_context(analysis)?;
24 let run =
25 crate::run::resolve_programmatic_run(analysis, &crate::run::ProgrammaticEntry::Mutation)?;
26 Ok(run.settings().cache_dir.value().clone())
27}
28
29pub fn last_mutation_report_path(analysis: &AnalysisOptions) -> Result<PathBuf, ProgrammaticError> {
35 Ok(cache_dir(analysis)?.join("mutation").join(LAST_REPORT_FILE))
36}
37
38pub fn remove_finished_mutation_journal(
52 analysis: &AnalysisOptions,
53 report: &fallow_output::MutationReport,
54) -> Result<bool, ProgrammaticError> {
55 if report.completion.status != fallow_output::MutationCompletionStatus::Complete
56 || report.runner.is_none()
57 {
58 return Ok(false);
59 }
60 let Some(run_id) = report.journal_run_id.as_deref() else {
61 return Ok(false);
62 };
63 let mutation_cache = cache_dir(analysis)?.join("mutation");
64 super::journal::Journal::remove_owned(&mutation_cache, run_id).map_err(|error| {
65 cache_error(format!(
66 "failed to remove {}: {error}",
67 super::journal::Journal::path(&mutation_cache).display()
68 ))
69 })
70}
71
72pub fn save_last_mutation_report(
81 analysis: &AnalysisOptions,
82 report: &serde_json::Value,
83) -> Result<PathBuf, ProgrammaticError> {
84 let cache_dir = cache_dir(analysis)?;
85 super::ignore_cache_dir(&cache_dir).map_err(|error| cache_error(error.to_string()))?;
86 let dir = cache_dir.join("mutation");
87 std::fs::create_dir_all(&dir).map_err(|error| cache_error(error.to_string()))?;
88 let bytes = serde_json::to_vec(report)
89 .map_err(|error| cache_error(format!("failed to serialize the report: {error}")))?;
90 let path = dir.join(LAST_REPORT_FILE);
91 let temporary = dir.join(format!("{LAST_REPORT_FILE}.{}.tmp", std::process::id()));
92 std::fs::write(&temporary, bytes)
93 .and_then(|()| std::fs::rename(&temporary, &path))
94 .map_err(|error| {
95 let _ = std::fs::remove_file(&temporary);
96 cache_error(format!("failed to save {}: {error}", path.display()))
97 })?;
98 Ok(path)
99}
100
101pub struct MutationWitnessStore {
103 witnesses: super::recorded_args::witnesses::WitnessMap,
104 now: u64,
105}
106
107impl MutationWitnessStore {
108 pub fn values(
112 &self,
113 mutant_id: &str,
114 input_id: &str,
115 ) -> Option<(
116 Vec<serde_json::Value>,
117 fallow_output::MutationWitnessSide,
118 fallow_output::MutationWitnessSide,
119 )> {
120 let stored = super::recorded_args::witnesses::fresh_witness(
121 &self.witnesses,
122 mutant_id,
123 input_id,
124 self.now,
125 )?;
126 let side = |side: &super::recorded_args::witnesses::StoredSide| {
127 Some(fallow_output::MutationWitnessSide {
128 outcome: serde_json::from_value(serde_json::Value::String(side.outcome.clone()))
129 .ok()?,
130 value: side.value.clone(),
131 text: side.text.clone(),
132 })
133 };
134 Some((
135 stored.args.clone(),
136 side(&stored.original)?,
137 side(&stored.mutant)?,
138 ))
139 }
140}
141
142pub fn mutation_witness_store(analysis: &AnalysisOptions) -> Option<MutationWitnessStore> {
145 let cache_dir = cache_dir(analysis).ok()?;
146 Some(MutationWitnessStore {
147 witnesses: super::recorded_args::witnesses::read_witnesses(&cache_dir),
148 now: super::recorded_args::now_secs(),
149 })
150}