use std::path::{Path, PathBuf};
use crate::analysis_context::resolve_programmatic_analysis_context;
use crate::{AnalysisOptions, ProgrammaticError};
const LAST_REPORT_FILE: &str = "last-report.json";
pub fn ignore_cache_dir(cache_dir: &Path) -> std::io::Result<()> {
std::fs::create_dir_all(cache_dir)?;
let ignore = cache_dir.join(".gitignore");
if !ignore.exists() {
std::fs::write(ignore, "*\n")?;
}
Ok(())
}
fn cache_error(message: String) -> ProgrammaticError {
ProgrammaticError::new(message, 2).with_code("FALLOW_MUTATION_CACHE_WRITE_FAILED")
}
fn cache_dir(analysis: &AnalysisOptions) -> Result<PathBuf, ProgrammaticError> {
resolve_programmatic_analysis_context(analysis)?;
let run =
crate::run::resolve_programmatic_run(analysis, &crate::run::ProgrammaticEntry::Mutation)?;
Ok(run.settings().cache_dir.value().clone())
}
pub fn last_mutation_report_path(analysis: &AnalysisOptions) -> Result<PathBuf, ProgrammaticError> {
Ok(cache_dir(analysis)?.join("mutation").join(LAST_REPORT_FILE))
}
pub fn remove_finished_mutation_journal(
analysis: &AnalysisOptions,
report: &fallow_output::MutationReport,
) -> Result<bool, ProgrammaticError> {
if report.completion.status != fallow_output::MutationCompletionStatus::Complete
|| report.runner.is_none()
{
return Ok(false);
}
let Some(run_id) = report.journal_run_id.as_deref() else {
return Ok(false);
};
let mutation_cache = cache_dir(analysis)?.join("mutation");
super::journal::Journal::remove_owned(&mutation_cache, run_id).map_err(|error| {
cache_error(format!(
"failed to remove {}: {error}",
super::journal::Journal::path(&mutation_cache).display()
))
})
}
pub fn save_last_mutation_report(
analysis: &AnalysisOptions,
report: &serde_json::Value,
) -> Result<PathBuf, ProgrammaticError> {
let cache_dir = cache_dir(analysis)?;
ignore_cache_dir(&cache_dir).map_err(|error| cache_error(error.to_string()))?;
let dir = cache_dir.join("mutation");
std::fs::create_dir_all(&dir).map_err(|error| cache_error(error.to_string()))?;
let bytes = serde_json::to_vec(report)
.map_err(|error| cache_error(format!("failed to serialize the report: {error}")))?;
let path = dir.join(LAST_REPORT_FILE);
let temporary = dir.join(format!("{LAST_REPORT_FILE}.{}.tmp", std::process::id()));
std::fs::write(&temporary, bytes)
.and_then(|()| std::fs::rename(&temporary, &path))
.map_err(|error| {
let _ = std::fs::remove_file(&temporary);
cache_error(format!("failed to save {}: {error}", path.display()))
})?;
Ok(path)
}
pub struct MutationWitnessStore {
witnesses: super::recorded_args::witnesses::WitnessMap,
now: u64,
}
impl MutationWitnessStore {
pub fn values(
&self,
mutant_id: &str,
input_id: &str,
) -> Option<(
Vec<serde_json::Value>,
fallow_output::MutationWitnessSide,
fallow_output::MutationWitnessSide,
)> {
let stored = super::recorded_args::witnesses::fresh_witness(
&self.witnesses,
mutant_id,
input_id,
self.now,
)?;
let side = |side: &super::recorded_args::witnesses::StoredSide| {
Some(fallow_output::MutationWitnessSide {
outcome: serde_json::from_value(serde_json::Value::String(side.outcome.clone()))
.ok()?,
value: side.value.clone(),
text: side.text.clone(),
})
};
Some((
stored.args.clone(),
side(&stored.original)?,
side(&stored.mutant)?,
))
}
}
pub fn mutation_witness_store(analysis: &AnalysisOptions) -> Option<MutationWitnessStore> {
let cache_dir = cache_dir(analysis).ok()?;
Some(MutationWitnessStore {
witnesses: super::recorded_args::witnesses::read_witnesses(&cache_dir),
now: super::recorded_args::now_secs(),
})
}