fallow-api 3.47.0

Programmatic API contract types for fallow
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! The two-phase runs of `--kill-matrix-for` target tests.

use super::*;

/// One `--kill-matrix-for` target and the dry-run tests that it selects.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedTarget {
    /// The target as the caller gave it, after path normalization.
    pub target: String,
    /// The dry-run test ids that the target selects, sorted.
    pub test_ids: Vec<String>,
    /// Why the target selects no test. `None` when `test_ids` is nonempty,
    /// and also before the scheduler checks the target against the files of
    /// the dry run.
    pub unmatched: Option<Unmatched>,
}

/// Why a `--kill-matrix-for` target selects no test of the dry run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Unmatched {
    /// The runner does not list the file of the target as a test file.
    NotATestFile,
    /// The runner lists the file, but the file reaches no planned code, so
    /// the dry run does not run it. A test that runs the code only in a
    /// child process is an example.
    NoPlannedCode,
    /// The dry run runs the file, but no test of the file has the target
    /// id, or each test of the file has a duplicate id.
    NoTest,
}

impl Unmatched {
    /// The reason code of the report. `NoTest` has no code of its own:
    /// `unmatched-target` describes it fully.
    pub const fn reason(self) -> Option<&'static str> {
        match self {
            Self::NotATestFile => Some("not-a-test-file"),
            Self::NoPlannedCode => Some("no-planned-code"),
            Self::NoTest => None,
        }
    }

    const fn describe(self) -> &'static str {
        match self {
            Self::NotATestFile => "the runner does not list the file as a test file",
            Self::NoPlannedCode => {
                "the file reaches no planned code, so the dry run does not run it"
            }
            Self::NoTest => "no test of the dry run has this id",
        }
    }
}

/// The test files that decide if a target can match: `runner` lists each
/// test file of the runner, and `dry` the files that the dry run runs.
#[derive(Clone, Copy)]
pub(super) struct TargetFiles<'a> {
    runner: &'a [String],
    dry: &'a [String],
}

impl<'a> TargetFiles<'a> {
    pub(super) const fn new(runner: &'a [String], dry: &'a [String]) -> Self {
        Self { runner, dry }
    }

    /// Why `target` cannot select a test before the dry run runs. `None`
    /// when the dry run runs the file of the target.
    fn unmatched(self, target: &str) -> Option<Unmatched> {
        let names = |files: &[String]| files.iter().any(|file| names_file(target, file));
        if names(self.dry) {
            None
        } else if names(self.runner) {
            Some(Unmatched::NoPlannedCode)
        } else {
            Some(Unmatched::NotATestFile)
        }
    }
}

/// True when `target` is `file` or a test id (`file::name`) of `file`.
fn names_file(target: &str, file: &str) -> bool {
    target
        .strip_prefix(file)
        .is_some_and(|rest| rest.is_empty() || rest.starts_with("::"))
}

/// Checks the targets of `config` before the dry run. The dry run can take
/// a long time, so when the dry run runs the file of no target, the run
/// stops before it. A run with at least one possible target goes on.
pub(super) fn check_targets(
    config: &ScheduleConfig,
    caps: Capabilities,
    files: TargetFiles<'_>,
) -> Result<(), ScheduleError> {
    if config.targets.is_empty() {
        return Ok(());
    }
    if !caps.test_filter {
        return Err(ScheduleError::TargetsNeedTestFilter);
    }
    let unmatched: Option<Vec<ResolvedTarget>> = config
        .targets
        .iter()
        .map(|target| {
            files.unmatched(target).map(|reason| ResolvedTarget {
                target: target.clone(),
                test_ids: Vec::new(),
                unmatched: Some(reason),
            })
        })
        .collect();
    match unmatched {
        Some(unmatched) => Err(ScheduleError::UnmatchedTargets(unmatched)),
        None => Ok(()),
    }
}

/// The message of the error for targets that select no test.
pub(super) fn unmatched_message(targets: &[ResolvedTarget]) -> String {
    let named: Vec<String> = targets
        .iter()
        .map(|target| {
            let reason = target.unmatched.unwrap_or(Unmatched::NoTest).describe();
            format!("{} ({reason})", target.target)
        })
        .collect();
    format!(
        "no --kill-matrix-for target selects a test of the dry run: {}; the dry run runs the test files that reach the planned code",
        named.join(", ")
    )
}

impl Worker<'_> {
    /// Runs the target tests of `job` without bail, so each target test that
    /// kills the mutant is known. After a terminal result, runs the other
    /// covering tests with bail: the first other killer shows that the kills
    /// of the targets are not unique. In the target-first mode, a survivor of
    /// the targets is `not_killed_by_target` and runs no other test: it
    /// cannot add a kill to a target. With `target_full`, a survivor of the
    /// targets gets the same status as in normal mode.
    pub(super) fn attempt_targets(
        &mut self,
        job: &Job,
        caps: Capabilities,
    ) -> Result<Attempt, ScheduleError> {
        let targets = Job {
            tests: job.targets.clone(),
            ..job.clone()
        };
        let first = self.attempt_tests(&targets, caps, false)?;
        if first.status == MutantStatus::Survived && !self.config.target_full {
            return Ok(Attempt {
                status: MutantStatus::NotKilledByTarget,
                ..first
            });
        }
        let terminal = match first.status {
            MutantStatus::Killed => first.kill_matrix_complete,
            MutantStatus::Survived => true,
            _ => false,
        };
        let others: Vec<String> = job
            .tests
            .iter()
            .filter(|test| !job.targets.contains(test))
            .cloned()
            .collect();
        if !terminal || others.is_empty() {
            return Ok(first);
        }
        let others = Job {
            tests: others,
            ..job.clone()
        };
        let second = self.attempt_tests(&others, caps, true)?;
        let target_kill = first.status == MutantStatus::Killed;
        let mut merged = merge_target_phases(first, second);
        let other_killer = merged
            .killing_tests
            .iter()
            .any(|test| !job.targets.contains(test));
        if target_kill && merged.kill_matrix_complete {
            merged.kill_matrix_complete = self.passes_without_mutant(&targets, caps)?
                && (!other_killer || self.passes_without_mutant(&others, caps)?);
        }
        Ok(merged)
    }

    /// True when the tests of `job` pass in a control run with no active
    /// mutant. Each phase runs a part of the covering tests, so a test that
    /// depends on the order of the tests can fail there without the mutant.
    /// A target that fails without the other tests must not count as a
    /// killer, and an other test that fails without the targets must not
    /// remove the uniqueness of a target kill. The memo gives each test set
    /// at most one control run.
    fn passes_without_mutant(
        &mut self,
        job: &Job,
        caps: Capabilities,
    ) -> Result<bool, ScheduleError> {
        let tests: Vec<String> = job
            .tests
            .iter()
            .filter(|test| !self.memo.is_failed(test))
            .cloned()
            .collect();
        if tests.len() != job.tests.len() {
            return Ok(false);
        }
        Ok(matches!(
            self.check_control(&tests, job, caps)?,
            ControlCheck::Passed
        ))
    }
}

/// The attempt of a target-mode mutant from its two phases: `targets`, the
/// run of the target tests without bail, and `others`, the run of the other
/// covering tests with bail. `killing_tests` holds each target killer and the
/// first other killer. The evidence is complete when the target run is exact
/// and, after a target kill, the other run gave a terminal result.
fn merge_target_phases(targets: Attempt, others: Attempt) -> Attempt {
    let duration_ms = match (targets.duration_ms, others.duration_ms) {
        (Some(first), Some(second)) => Some(first.saturating_add(second)),
        (first, second) => first.or(second),
    };
    let mut dropped_tests = targets.dropped_tests;
    dropped_tests.extend(others.dropped_tests.iter().cloned());
    dropped_tests.sort();
    dropped_tests.dedup();
    let other_killer = (others.status == MutantStatus::Killed)
        .then(|| others.killed_by.clone())
        .flatten();
    // Each other test failed with no active mutant: the target run decides.
    if others.status == MutantStatus::NoCoverage {
        return Attempt {
            kill_matrix_complete: targets.kill_matrix_complete && dropped_tests.is_empty(),
            dropped_tests,
            ..targets
        };
    }
    if targets.status != MutantStatus::Killed {
        return Attempt {
            killing_tests: other_killer.into_iter().collect(),
            kill_matrix_complete: targets.kill_matrix_complete && dropped_tests.is_empty(),
            duration_ms,
            dropped_tests,
            ..others
        };
    }
    let others_terminal = match others.status {
        MutantStatus::Killed => other_killer.is_some(),
        MutantStatus::Survived => true,
        _ => false,
    };
    let mut killing_tests = targets.killing_tests;
    killing_tests.extend(other_killer);
    killing_tests.sort();
    killing_tests.dedup();
    Attempt {
        killing_tests,
        kill_matrix_complete: targets.kill_matrix_complete
            && others_terminal
            && dropped_tests.is_empty(),
        duration_ms,
        dropped_tests,
        issue: targets.issue.or(others.issue),
        ..targets
    }
}

/// True when the target-first mode retries the first run of `job`: exactly
/// one target test killed the mutant and no other test did. That kill is a
/// unique kill of the target, and one flaky run must not decide that a test
/// is not redundant. A shared kill, a kill of two or more target tests and
/// each result of the full mode get no such retry.
pub(super) fn retries_unique_kill(config: &ScheduleConfig, job: &Job, attempt: &Attempt) -> bool {
    !config.target_full
        && attempt.status == MutantStatus::Killed
        && attempt.kill_matrix_complete
        && matches!(attempt.killing_tests.as_slice(), [test] if job.targets.contains(test))
}

/// The target tests of `tests`, in the same order. Empty outside target mode.
pub(super) fn job_targets(context: &JobContext<'_>, tests: &[String]) -> Vec<String> {
    context.targets.map_or_else(Vec::new, |targets| {
        tests
            .iter()
            .filter(|test| targets.contains(*test))
            .cloned()
            .collect()
    })
}

/// The test ids that the targets of `config` select, and the resolved
/// targets in `outcome`. `None` outside target mode. A target that selects
/// no test stays in `outcome` with the reason. The run stops only when no
/// target selects a test.
pub(super) fn config_targets(
    config: &ScheduleConfig,
    dry: &DryRunResult,
    caps: Capabilities,
    files: TargetFiles<'_>,
    outcome: &mut ScheduleOutcome,
) -> Result<Option<FxHashSet<String>>, ScheduleError> {
    if config.targets.is_empty() {
        return Ok(None);
    }
    let (targets, _) = resolve_targets(&config.targets, &dry.tests, caps)?;
    let (mut targets, ids) = without_duplicate_ids(targets, &reported_duplicates(dry))?;
    for target in targets
        .iter_mut()
        .filter(|target| target.test_ids.is_empty())
    {
        target.unmatched = Some(files.unmatched(&target.target).unwrap_or(Unmatched::NoTest));
    }
    if ids.is_empty() {
        return Err(ScheduleError::UnmatchedTargets(targets));
    }
    outcome.targets = targets;
    Ok(Some(ids))
}

/// The message of the error for targets that select a test with a duplicate id.
pub(super) fn duplicate_message(ids: &[String]) -> String {
    format!(
        "these --kill-matrix-for targets name a duplicate test id: {}; the runner numbers tests that share a title in report order, so the kill evidence of these tests is not reliable; give each test a unique title",
        ids.join(", ")
    )
}

/// Applies the duplicate test ids to the resolved targets. The companion
/// numbers tests that share a title in report order, so the kills of such a
/// test can belong to its twin. A target that names a duplicate id is an
/// error. A file target drops the duplicate ids and keeps its other tests;
/// a file target with no test left selects no test.
fn without_duplicate_ids(
    targets: Vec<ResolvedTarget>,
    duplicate_ids: &[String],
) -> Result<(Vec<ResolvedTarget>, FxHashSet<String>), ScheduleError> {
    let duplicates: FxHashSet<&String> = duplicate_ids.iter().collect();
    let named: Vec<String> = targets
        .iter()
        .filter(|target| duplicates.contains(&target.target))
        .map(|target| target.target.clone())
        .collect();
    if !named.is_empty() {
        return Err(ScheduleError::DuplicateTargetTestIds(named));
    }
    let mut ids = FxHashSet::default();
    let mut kept = Vec::with_capacity(targets.len());
    for mut target in targets {
        target.test_ids.retain(|id| !duplicates.contains(id));
        ids.extend(target.test_ids.iter().cloned());
        kept.push(target);
    }
    Ok((kept, ids))
}

/// Matches each `--kill-matrix-for` target against the tests of the dry run.
/// A target selects each test whose id or file equals it. Gives the targets
/// with their tests, and the union of the selected test ids. A target that
/// selects no test has an empty `test_ids`.
pub(super) fn resolve_targets(
    targets: &[String],
    tests: &[DryTest],
    caps: Capabilities,
) -> Result<(Vec<ResolvedTarget>, FxHashSet<String>), ScheduleError> {
    if !caps.test_filter {
        return Err(ScheduleError::TargetsNeedTestFilter);
    }
    let mut resolved = Vec::with_capacity(targets.len());
    let mut ids = FxHashSet::default();
    for target in targets {
        let mut test_ids: Vec<String> = tests
            .iter()
            .filter(|test| test.test_id == *target || test.file == *target)
            .map(|test| test.test_id.clone())
            .collect();
        test_ids.sort();
        test_ids.dedup();
        ids.extend(test_ids.iter().cloned());
        resolved.push(ResolvedTarget {
            target: target.clone(),
            test_ids,
            unmatched: None,
        });
    }
    Ok((resolved, ids))
}

/// The status and the evidence of a retried target kill (see
/// [`retries_unique_kill`]). The kill is confirmed only when the retry kills
/// the mutant with the same tests. A retry with other killers, for example
/// an other test that passed in the first run and fails now, makes the
/// mutant `flaky`: one run would make the kill shared and the other unique.
/// Each result other than a confirmed kill leaves the evidence incomplete.
pub(super) fn confirmed_kill(
    first: &MutantOutcome,
    status: MutantStatus,
    retry: Attempt,
) -> (MutantStatus, Attempt) {
    let killers = |tests: &[String]| -> BTreeSet<String> { tests.iter().cloned().collect() };
    let same_killers = killers(&first.killing_tests) == killers(&retry.killing_tests);
    if status != MutantStatus::Killed || !same_killers {
        let status = if status == MutantStatus::Killed {
            MutantStatus::Flaky
        } else {
            status
        };
        return (
            status,
            Attempt {
                kill_matrix_complete: false,
                ..retry
            },
        );
    }
    (
        status,
        Attempt {
            kill_matrix_complete: first.kill_matrix_complete && retry.kill_matrix_complete,
            ..retry
        },
    )
}