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 each operator.
120    pub operators: Option<Vec<Operator>>,
121    /// Keep mutants near a file system, process, network or database sink.
122    /// Only an explicit caller option can set this, never project config,
123    /// because the run has no sandbox copy.
124    pub allow_side_effects: bool,
125    /// Execute all covering tests and retain complete killing test sets.
126    pub kill_matrix: bool,
127    /// The `--kill-matrix-for` targets: test files or runner test ids. Each
128    /// mutant that a target test covers runs its target tests without bail
129    /// and its other covering tests with bail. The other mutants are
130    /// `pruned`. Exclusive with `kill_matrix`.
131    pub kill_matrix_targets: Vec<String>,
132    /// The maximum number of covered mutants to run, in rank order. A
133    /// mutant that no dry-run test covers is `no_coverage` and does not
134    /// count. The others become `pending`.
135    pub max_mutants: Option<usize>,
136    /// Run in a dirty git worktree, or outside a git worktree. The run still
137    /// compares the worktree after the run.
138    pub allow_dirty: bool,
139    /// The JavaScript runtime. `None` uses the runtime of the runner from
140    /// `PATH`: `node` for `node:test`, `bun` for `bun test`, and `node`,
141    /// then `bun`, for Vitest.
142    pub runtime: Option<PathBuf>,
143    /// The number of companion processes. `None` uses half the logical
144    /// cores.
145    pub concurrency: Option<usize>,
146    /// The time budget of the run. Mutants that did not start before it
147    /// ends become `pending`.
148    pub time_budget: Option<Duration>,
149    /// The timeout of the companion `init` and the dry run. `None` uses
150    /// 120 seconds.
151    pub runner_timeout: Option<Duration>,
152    /// The config file of the test runner, for example a separate Vitest
153    /// config of the integration tests. A relative path is relative to the
154    /// project root. The file must exist inside the project root. `None`
155    /// uses the `mutation.runnerConfig` config key, else the default config
156    /// discovery of the runner. Only the Vitest runner reads a config file.
157    pub runner_config: Option<PathBuf>,
158    /// The files that the caller writes after the run, for example the
159    /// `--output-file` report. The worktree check leaves them out, so an old
160    /// report does not make the worktree dirty. A relative path is relative
161    /// to the process directory, as the caller writes it.
162    pub output_paths: Vec<PathBuf>,
163    /// Type-check each survived mutant through the TypeScript-Go companion
164    /// of `--type-aware`. A survivor whose mutated file does not type-check
165    /// becomes `compile_error` and leaves the score. The run fails before
166    /// the tests start when no trusted companion exists.
167    pub typecheck: bool,
168    /// Reuse the final results in the outcome journal of an interrupted
169    /// earlier run, and run only the other mutants. A journal of other
170    /// inputs stops the run with `FALLOW_MUTATION_RESUME_MISMATCH`. With no
171    /// journal, the run starts from the beginning and the report says so.
172    /// Without this option, the run starts a new journal.
173    pub resume: bool,
174    /// Run only the mutants with these ids, for example the survivors of an
175    /// earlier report after new tests. Each id must be in the plan of the
176    /// scope, or the run fails with `FALLOW_MUTATION_UNKNOWN_MUTANT`. Empty
177    /// runs each mutant of the scope.
178    pub mutant_ids: Vec<String>,
179    /// Allow a dirty worktree when each dirty path is a test file: a path
180    /// that the test path rules match, or a test file of the plan. Another
181    /// dirty path fails the run with `FALLOW_MUTATION_NON_TEST_CHANGE`, so
182    /// the run proves a change of the tests only. `allow_dirty` wins.
183    pub test_changes_only: bool,
184}