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