fallow_api/mutation/
last_report.rs1use std::path::{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
17pub fn ignore_cache_dir(cache_dir: &Path) -> std::io::Result<()> {
20 std::fs::create_dir_all(cache_dir)?;
21 let ignore = cache_dir.join(".gitignore");
22 if !ignore.exists() {
23 std::fs::write(ignore, "*\n")?;
24 }
25 Ok(())
26}
27
28fn cache_error(message: String) -> ProgrammaticError {
29 ProgrammaticError::new(message, 2).with_code("FALLOW_MUTATION_CACHE_WRITE_FAILED")
30}
31
32fn cache_dir(analysis: &AnalysisOptions) -> Result<PathBuf, ProgrammaticError> {
34 resolve_programmatic_analysis_context(analysis)?;
35 let run =
36 crate::run::resolve_programmatic_run(analysis, &crate::run::ProgrammaticEntry::Mutation)?;
37 Ok(run.settings().cache_dir.value().clone())
38}
39
40pub fn last_mutation_report_path(analysis: &AnalysisOptions) -> Result<PathBuf, ProgrammaticError> {
46 Ok(cache_dir(analysis)?.join("mutation").join(LAST_REPORT_FILE))
47}
48
49pub fn remove_finished_mutation_journal(
62 analysis: &AnalysisOptions,
63 report: &fallow_output::MutationReport,
64) -> Result<bool, ProgrammaticError> {
65 if report.completion.status != fallow_output::MutationCompletionStatus::Complete
66 || report.runner.is_none()
67 {
68 return Ok(false);
69 }
70 let path = super::journal::Journal::path(&cache_dir(analysis)?.join("mutation"));
71 match std::fs::remove_file(&path) {
72 Ok(()) => Ok(true),
73 Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
74 Err(error) => Err(cache_error(format!(
75 "failed to remove {}: {error}",
76 path.display()
77 ))),
78 }
79}
80
81pub fn save_last_mutation_report(
90 analysis: &AnalysisOptions,
91 report: &serde_json::Value,
92) -> Result<PathBuf, ProgrammaticError> {
93 let cache_dir = cache_dir(analysis)?;
94 ignore_cache_dir(&cache_dir).map_err(|error| cache_error(error.to_string()))?;
95 let dir = cache_dir.join("mutation");
96 std::fs::create_dir_all(&dir).map_err(|error| cache_error(error.to_string()))?;
97 let bytes = serde_json::to_vec(report)
98 .map_err(|error| cache_error(format!("failed to serialize the report: {error}")))?;
99 let path = dir.join(LAST_REPORT_FILE);
100 let temporary = dir.join(format!("{LAST_REPORT_FILE}.{}.tmp", std::process::id()));
101 std::fs::write(&temporary, bytes)
102 .and_then(|()| std::fs::rename(&temporary, &path))
103 .map_err(|error| {
104 let _ = std::fs::remove_file(&temporary);
105 cache_error(format!("failed to save {}: {error}", path.display()))
106 })?;
107 Ok(path)
108}
109
110pub struct MutationWitnessStore {
112 witnesses: super::recorded_args::witnesses::WitnessMap,
113 now: u64,
114}
115
116impl MutationWitnessStore {
117 pub fn values(
121 &self,
122 mutant_id: &str,
123 input_id: &str,
124 ) -> Option<(
125 Vec<serde_json::Value>,
126 fallow_output::MutationWitnessSide,
127 fallow_output::MutationWitnessSide,
128 )> {
129 let stored = super::recorded_args::witnesses::fresh_witness(
130 &self.witnesses,
131 mutant_id,
132 input_id,
133 self.now,
134 )?;
135 let side = |side: &super::recorded_args::witnesses::StoredSide| {
136 Some(fallow_output::MutationWitnessSide {
137 outcome: serde_json::from_value(serde_json::Value::String(side.outcome.clone()))
138 .ok()?,
139 value: side.value.clone(),
140 text: side.text.clone(),
141 })
142 };
143 Some((
144 stored.args.clone(),
145 side(&stored.original)?,
146 side(&stored.mutant)?,
147 ))
148 }
149}
150
151pub fn mutation_witness_store(analysis: &AnalysisOptions) -> Option<MutationWitnessStore> {
154 let cache_dir = cache_dir(analysis).ok()?;
155 Some(MutationWitnessStore {
156 witnesses: super::recorded_args::witnesses::read_witnesses(&cache_dir),
157 now: super::recorded_args::now_secs(),
158 })
159}