Skip to main content

fallow_api/mutation/
mod.rs

1//! The dynamic half of mutation testing: the runner protocol, the embedded
2//! companion, the transport, the scheduler and the result model.
3//!
4//! The static half (selection, the test map, pruning, ranking and the
5//! instrumented code) is `fallow_engine::mutation`. The entry points are
6//! [`crate::run_mutation`] and [`crate::plan_mutation`].
7
8pub(crate) mod companion;
9pub(crate) mod discovery;
10pub(crate) mod journal;
11pub(crate) mod kill_matrix;
12pub(crate) mod last_report;
13pub(crate) mod manifest;
14pub(crate) mod node_args;
15pub(crate) mod protocol;
16pub(crate) mod report;
17pub(crate) mod result_cache;
18pub(crate) mod run_dir;
19pub(crate) mod runner_select;
20pub(crate) mod scheduler;
21pub(crate) mod transport;
22pub(crate) mod typecheck;
23pub(crate) mod verdicts;
24pub(crate) mod worktree;
25
26#[cfg(test)]
27#[expect(clippy::unwrap_used, reason = "tests unwrap to keep assertions short")]
28mod e2e_tests;
29
30use std::path::PathBuf;
31use std::time::Duration;
32
33pub use fallow_engine::mutation::Operator;
34pub use last_report::{
35    last_mutation_report_path, remove_finished_mutation_journal, save_last_mutation_report,
36};
37pub use report::baseline_blocked_run_error;
38pub use scheduler::control::split_control_run_failure;
39pub use verdicts::{MAX_VERDICT_INPUT_BYTES, apply_mutation_verdicts, validate_mutation_verdicts};
40
41use crate::AnalysisOptions;
42
43/// The code that a mutation run mutates.
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub enum MutationRunScope {
46    /// The added lines against a base ref. `None` detects the base ref the
47    /// same way as `audit`. An explicit `analysis.diff_file` wins over the
48    /// base ref.
49    Changed {
50        /// The base ref.
51        base: Option<String>,
52    },
53    /// Each source file of the project.
54    All,
55    /// The files at these paths, or below these directories. A relative
56    /// path is relative to the project root.
57    Paths(Vec<PathBuf>),
58}
59
60impl Default for MutationRunScope {
61    fn default() -> Self {
62        Self::Changed { base: None }
63    }
64}
65
66/// A phase event of a mutation run, for a caller that shows progress.
67///
68/// [`crate::run_mutation_with_progress`] sends the events in run order. A
69/// run with no covered mutant and no test file skips the runner phases.
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71#[non_exhaustive]
72pub enum MutationProgress {
73    /// The plan is ready. The runner setup starts: the runtime, the
74    /// companion install and the runner start.
75    Setup {
76        /// The planned mutants.
77        mutants: usize,
78    },
79    /// The baseline run starts: the test files run with no active mutant.
80    Baseline {
81        /// The test files of the baseline run.
82        test_files: usize,
83    },
84    /// The mutant runs. The first event has `done` 0, and each finished
85    /// mutant sends one event.
86    Mutants {
87        /// The mutants that finished.
88        done: usize,
89        /// The mutants that this phase runs. A cached result is not in it.
90        total: usize,
91    },
92    /// The retries of survivors and timeouts. The first event has `done` 0.
93    Retries {
94        /// The retries that finished.
95        done: usize,
96        /// The retries that this phase runs.
97        total: usize,
98    },
99    /// The tests stopped. The worktree check, the survivor type check and
100    /// the report start.
101    Finalize,
102}
103
104/// Receives the [`MutationProgress`] events of a run. The scheduler sends
105/// the mutant events from its worker threads.
106pub type MutationProgressSink<'a> = &'a (dyn Fn(MutationProgress) + Sync);
107
108/// Options of [`crate::run_mutation`] and [`crate::plan_mutation`].
109#[derive(Debug, Clone, Default)]
110pub struct MutationRunOptions {
111    /// Shared project, config and cache options.
112    pub analysis: AnalysisOptions,
113    /// The code to mutate.
114    pub scope: MutationRunScope,
115    /// The operators to use. `None` uses each operator.
116    pub operators: Option<Vec<Operator>>,
117    /// Keep mutants near a file system, process, network or database sink.
118    /// Only an explicit caller option can set this, never project config,
119    /// because the run has no sandbox copy.
120    pub allow_side_effects: bool,
121    /// Execute all covering tests and retain complete killing test sets.
122    pub kill_matrix: bool,
123    /// The maximum number of covered mutants to run, in rank order. A
124    /// mutant that no dry-run test covers is `no_coverage` and does not
125    /// count. The others become `pending`.
126    pub max_mutants: Option<usize>,
127    /// Run in a dirty git worktree, or outside a git worktree. The run still
128    /// compares the worktree after the run.
129    pub allow_dirty: bool,
130    /// The JavaScript runtime. `None` uses the runtime of the runner from
131    /// `PATH`: `node` for `node:test`, `bun` for `bun test`, and `node`,
132    /// then `bun`, for Vitest.
133    pub runtime: Option<PathBuf>,
134    /// The number of companion processes. `None` uses half the logical
135    /// cores.
136    pub concurrency: Option<usize>,
137    /// The time budget of the run. Mutants that did not start before it
138    /// ends become `pending`.
139    pub time_budget: Option<Duration>,
140    /// The timeout of the companion `init` and the dry run. `None` uses
141    /// 120 seconds.
142    pub runner_timeout: Option<Duration>,
143    /// The config file of the test runner, for example a separate Vitest
144    /// config of the integration tests. A relative path is relative to the
145    /// project root. The file must exist inside the project root. `None`
146    /// uses the `mutation.runnerConfig` config key, else the default config
147    /// discovery of the runner. Only the Vitest runner reads a config file.
148    pub runner_config: Option<PathBuf>,
149    /// The files that the caller writes after the run, for example the
150    /// `--output-file` report. The worktree check leaves them out, so an old
151    /// report does not make the worktree dirty. A relative path is relative
152    /// to the process directory, as the caller writes it.
153    pub output_paths: Vec<PathBuf>,
154    /// Type-check each survived mutant through the TypeScript-Go companion
155    /// of `--type-aware`. A survivor whose mutated file does not type-check
156    /// becomes `compile_error` and leaves the score. The run fails before
157    /// the tests start when no trusted companion exists.
158    pub typecheck: bool,
159    /// Reuse the final results in the outcome journal of an interrupted
160    /// earlier run, and run only the other mutants. A journal of other
161    /// inputs stops the run with `FALLOW_MUTATION_RESUME_MISMATCH`. With no
162    /// journal, the run starts from the beginning and the report says so.
163    /// Without this option, the run starts a new journal.
164    pub resume: bool,
165}