fallow-api 3.43.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use super::*;
use fallow_output::DUPLICATE_TEST_IDS_CODE;
use targets::Unmatched;

/// The mutant runs: the tests and the bail flag of each run with an active
/// mutant. Control runs are left out.
fn mutant_runs(spawner: &FakeSpawner) -> Vec<(Option<String>, Vec<String>, bool)> {
    spawner
        .runs()
        .into_iter()
        .filter(|(_, run)| run.mutant_id.is_some())
        .map(|(_, run)| (run.mutant_id, run.test_ids, run.bail))
        .collect()
}

/// A run in which `killers` fail. A control run, with no active mutant,
/// passes.
fn killed_by_each(run: &RunRequest, killers: &[String]) -> Response {
    if run.mutant_id.is_none() {
        return killed_by_each_always(run, &[]);
    }
    killed_by_each_always(run, killers)
}

/// A run in which `killers` fail, also with no active mutant.
fn killed_by_each_always(run: &RunRequest, killers: &[String]) -> Response {
    let tests: Vec<RunTest> = run
        .test_ids
        .iter()
        .map(|id| RunTest {
            test_id: id.clone(),
            status: if killers.contains(id) {
                TestStatus::Fail
            } else {
                TestStatus::Pass
            },
            failure_kind: killers.contains(id).then(|| "assertion".to_owned()),
            message: None,
        })
        .collect();
    let first = run.test_ids.iter().find(|id| killers.contains(id)).cloned();
    Response::Run(RunResult {
        mutant_id: run.mutant_id.clone(),
        status: if first.is_some() {
            RunStatus::Killed
        } else {
            RunStatus::Survived
        },
        killed_by: first,
        duration_ms: Some(3.0),
        tests_executed: tests.len(),
        tests,
        unhandled_errors: Vec::new(),
        loop_timeout: false,
    })
}

fn target_config(targets: &[String]) -> ScheduleConfig {
    let mut config = config(1);
    config.targets = targets.to_vec();
    config
}

/// m1: only t1 covers it. m2: t1 and t2 cover it.
fn run_targets(
    targets: &[String],
    killers: &'static [&'static str],
) -> (ScheduleOutcome, FakeSpawner) {
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        move |_, run| {
            let killers: Vec<String> = killers.iter().map(|id| tid(id)).collect();
            Ok(killed_by_each(run, &killers))
        },
    ));
    let outcome =
        run_schedule(&spawner, &plan(&["m1", "m2"]), &target_config(targets)).expect("schedule");
    (outcome, spawner)
}

#[test]
fn targets_run_without_bail_then_the_other_tests_run_with_bail() {
    let (outcome, spawner) = run_targets(&[tid("t2")], &["t2"]);
    assert_eq!(
        mutant_runs(&spawner),
        [
            (Some("m2".to_owned()), vec![tid("t2")], false),
            (Some("m2".to_owned()), vec![tid("t1")], true),
        ],
        "only the mutant that a target covers runs"
    );
    let m2 = &outcome.results["m2"];
    assert_eq!(m2.status, MutantStatus::Killed);
    assert_eq!(m2.killed_by, Some(tid("t2")));
    assert_eq!(m2.killing_tests, [tid("t2")]);
    assert!(m2.kill_matrix_complete);
    assert_eq!(outcome.results["m1"].status, MutantStatus::Pruned);
    assert_eq!(
        outcome.targets,
        [targets::ResolvedTarget {
            target: tid("t2"),
            test_ids: vec![tid("t2")],
            unmatched: None,
        }]
    );
}

#[test]
fn the_first_other_killer_joins_the_target_killers() {
    let (outcome, _) = run_targets(&[tid("t2")], &["t1", "t2"]);
    let m2 = &outcome.results["m2"];
    assert_eq!(m2.status, MutantStatus::Killed);
    assert_eq!(m2.killed_by, Some(tid("t2")));
    assert_eq!(m2.killing_tests, [tid("t1"), tid("t2")]);
    assert!(
        m2.kill_matrix_complete,
        "the control run of the other tests passed"
    );
}

#[test]
fn an_other_killer_that_fails_without_the_mutant_leaves_incomplete_evidence() {
    // t1 passes with t2 in the dry run, but fails in a run without t2, also
    // with no active mutant: it depends on the order of the tests.
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        |_, run| Ok(killed_by_each(run, &[tid("t1"), tid("t2")])),
    ))
    .with_control(|_, run| {
        if run.test_ids == [tid("t1")] {
            Ok(killed_by_each_always(run, &[tid("t1")]))
        } else {
            Ok(passing_control(run))
        }
    });
    let outcome =
        run_schedule(&spawner, &plan(&["m2"]), &target_config(&[tid("t2")])).expect("schedule");
    let m2 = &outcome.results["m2"];
    assert_eq!(m2.status, MutantStatus::Killed);
    assert_eq!(m2.killed_by, Some(tid("t2")));
    assert!(
        !m2.kill_matrix_complete,
        "an unconfirmed other killer must not remove the uniqueness of a target kill"
    );
    assert!(
        spawner
            .controls()
            .iter()
            .any(|(_, run)| run.test_ids == [tid("t1")]),
        "the other tests get a control run"
    );
}

#[test]
fn a_target_survivor_gets_its_status_from_the_other_tests() {
    let (outcome, _) = run_targets(&[tid("t2")], &["t1"]);
    let m2 = &outcome.results["m2"];
    assert_eq!(m2.status, MutantStatus::Killed);
    assert_eq!(m2.killed_by, Some(tid("t1")));
    assert_eq!(m2.killing_tests, [tid("t1")]);
    assert!(m2.kill_matrix_complete);
}

#[test]
fn a_file_target_selects_each_test_of_the_file_and_needs_no_other_run() {
    let (outcome, spawner) = run_targets(&["test/a.test.ts".to_owned()], &["t1", "t2"]);
    assert_eq!(
        mutant_runs(&spawner),
        [
            (Some("m1".to_owned()), vec![tid("t1")], false),
            (Some("m2".to_owned()), vec![tid("t1"), tid("t2")], false),
        ]
    );
    assert_eq!(outcome.results["m2"].killing_tests, [tid("t1"), tid("t2")]);
    assert!(outcome.results["m2"].kill_matrix_complete);
    assert_eq!(outcome.targets[0].test_ids, [tid("t1"), tid("t2")]);
}

#[test]
fn a_single_target_that_selects_no_dry_run_test_stops_the_run() {
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let error = run_schedule(&spawner, &plan(&["m1"]), &target_config(&[tid("gone")]))
        .expect_err("no target selects a test");
    assert_eq!(
        error,
        ScheduleError::UnmatchedTargets(vec![unmatched(&tid("gone"), Unmatched::NoTest)])
    );
    assert_eq!(dry_runs_of(&spawner).len(), 1, "the file of the target ran");
    let message = error.to_string();
    assert!(
        message.contains("no test of the dry run has this id"),
        "{message}"
    );
}

fn unmatched(target: &str, reason: Unmatched) -> targets::ResolvedTarget {
    targets::ResolvedTarget {
        target: target.to_owned(),
        test_ids: Vec::new(),
        unmatched: Some(reason),
    }
}

#[test]
fn a_pruned_mutant_leaves_its_place_inside_the_limit() {
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        |_, run| Ok(killed_by_each(run, &[tid("t2")])),
    ));
    let mut plan = plan(&["m1"]);
    plan.pending = vec![ScheduledMutant {
        id: "m2".to_owned(),
        file: "src/a.ts".to_owned(),
    }];
    let outcome = run_schedule(&spawner, &plan, &target_config(&[tid("t2")])).expect("schedule");
    assert_eq!(outcome.results["m1"].status, MutantStatus::Pruned);
    assert_eq!(outcome.results["m2"].status, MutantStatus::Killed);
    assert_eq!(outcome.promoted, ["m2"]);
}

#[test]
fn a_target_timeout_skips_the_other_tests_and_leaves_incomplete_evidence() {
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        |_, run| {
            if run.test_ids == [tid("t2")] {
                Ok(run_result(run, RunStatus::Timeout, None))
            } else {
                Ok(run_result(run, RunStatus::Survived, None))
            }
        },
    ));
    let outcome =
        run_schedule(&spawner, &plan(&["m2"]), &target_config(&[tid("t2")])).expect("schedule");
    let m2 = &outcome.results["m2"];
    assert_eq!(m2.status, MutantStatus::Timeout);
    assert!(!m2.kill_matrix_complete);
    assert!(
        mutant_runs(&spawner)
            .iter()
            .all(|(_, tests, _)| *tests == [tid("t2")]),
        "a timeout of the targets runs no other test"
    );
}

#[test]
fn a_target_killer_that_fails_without_the_other_tests_leaves_incomplete_evidence() {
    // t2 passes with t1 in the dry run, but fails in a run without t1, also
    // with no active mutant: it depends on the order of the tests. Its kill
    // is no evidence, so it must not count as a unique or a shared kill.
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        |_, run| Ok(killed_by_each(run, &[tid("t2")])),
    ))
    .with_control(|_, run| {
        if run.test_ids == [tid("t2")] {
            Ok(killed_by_each_always(run, &[tid("t2")]))
        } else {
            Ok(passing_control(run))
        }
    });
    let outcome =
        run_schedule(&spawner, &plan(&["m2"]), &target_config(&[tid("t2")])).expect("schedule");
    assert!(
        !outcome.results["m2"].kill_matrix_complete,
        "an unconfirmed target killer must not give complete evidence"
    );
    assert!(
        spawner
            .controls()
            .iter()
            .any(|(_, run)| run.test_ids == [tid("t2")]),
        "the target tests get a control run when the other tests are left out"
    );
}

/// A dry run in which t2 shares its title with another test.
fn dry_with_duplicate_t2() -> DryRunResult {
    DryRunResult {
        duplicate_test_ids: vec![tid("t2")],
        ..standard_dry()
    }
}

#[test]
fn a_duplicate_test_id_is_reported_and_the_run_goes_on() {
    let spawner = FakeSpawner::new(script(
        |_| dry_with_duplicate_t2(),
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let outcome = run_schedule(&spawner, &plan(&["m1"]), &config(1)).expect("schedule");
    let issue = outcome
        .issues
        .iter()
        .find(|issue| issue.code == DUPLICATE_TEST_IDS_CODE)
        .expect("a duplicate test id is reported");
    assert!(issue.message.contains(&tid("t2")), "{}", issue.message);
    assert_eq!(outcome.results["m1"].status, MutantStatus::Survived);
}

#[test]
fn a_dry_run_without_duplicate_test_ids_reports_no_duplicate_issue() {
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let outcome = run_schedule(&spawner, &plan(&["m1"]), &config(1)).expect("schedule");
    assert!(
        outcome
            .issues
            .iter()
            .all(|issue| issue.code != DUPLICATE_TEST_IDS_CODE)
    );
}

#[test]
fn a_target_that_names_a_duplicate_test_id_stops_the_run() {
    let spawner = FakeSpawner::new(script(
        |_| dry_with_duplicate_t2(),
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let error = run_schedule(&spawner, &plan(&["m1"]), &target_config(&[tid("t2")]))
        .expect_err("a duplicate target id is an error");
    assert_eq!(
        error,
        ScheduleError::DuplicateTargetTestIds(vec![tid("t2")])
    );
    let message = error.to_string();
    assert!(message.contains(&tid("t2")), "{message}");
    assert!(message.contains("not reliable"), "{message}");
}

#[test]
fn a_file_target_drops_the_duplicate_test_ids_and_keeps_the_other_tests() {
    let spawner = FakeSpawner::new(script(
        |_| dry_with_duplicate_t2(),
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let file = test("t2", 1.0, TestStatus::Pass).file;
    let outcome =
        run_schedule(&spawner, &plan(&["m1"]), &target_config(&[file])).expect("schedule");
    assert_eq!(outcome.targets[0].test_ids, [tid("t1")]);
    assert!(
        outcome
            .issues
            .iter()
            .any(|issue| issue.code == DUPLICATE_TEST_IDS_CODE),
        "the warning names the dropped id"
    );
}

#[test]
fn a_file_target_with_only_duplicate_test_ids_is_unmatched() {
    let spawner = FakeSpawner::new(script(
        |_| DryRunResult {
            duplicate_test_ids: vec![tid("t1"), tid("t2")],
            ..standard_dry()
        },
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let file = test("t1", 1.0, TestStatus::Pass).file;
    let error = run_schedule(
        &spawner,
        &plan(&["m1"]),
        &target_config(std::slice::from_ref(&file)),
    )
    .expect_err("no test is left");
    assert_eq!(
        error,
        ScheduleError::UnmatchedTargets(vec![unmatched(&file, Unmatched::NoTest)])
    );
}

#[test]
fn duplicate_test_ids_of_a_file_with_an_unhandled_error_are_not_reported() {
    let spawner = FakeSpawner::new(script(
        |_| DryRunResult {
            unhandled_errors: vec![UnhandledError {
                message: "boom".to_owned(),
                file: Some("test/a.test.ts".to_owned()),
            }],
            ..dry_with_duplicate_t2()
        },
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let outcome = run_schedule(&spawner, &plan(&["m1"]), &config(1)).expect("schedule");
    assert!(
        outcome
            .issues
            .iter()
            .all(|issue| issue.code != DUPLICATE_TEST_IDS_CODE)
    );
}

#[test]
fn a_target_that_avoids_the_duplicate_test_id_runs() {
    let spawner = FakeSpawner::new(script(
        |_| dry_with_duplicate_t2(),
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let outcome =
        run_schedule(&spawner, &plan(&["m1"]), &target_config(&[tid("t1")])).expect("schedule");
    assert_eq!(outcome.targets[0].test_ids, [tid("t1")]);
}

#[test]
fn a_target_that_reaches_no_planned_code_is_reported_and_the_other_targets_run() {
    // The runner lists test/b.test.ts, but no planned code reaches it, so
    // the dry run does not run it.
    let (outcome, spawner) = run_targets_result(&[tid("t2"), "test/b.test.ts".to_owned()], &["t2"])
        .expect("one matched target is enough");
    assert_eq!(outcome.results["m2"].killing_tests, [tid("t2")]);
    assert!(!mutant_runs(&spawner).is_empty());
    assert_eq!(outcome.targets.len(), 2);
    assert_eq!(
        outcome.targets[1],
        unmatched("test/b.test.ts", Unmatched::NoPlannedCode)
    );
}

#[test]
fn a_target_outside_the_test_files_of_the_runner_is_reported_and_the_other_targets_run() {
    let (outcome, _) = run_targets_result(&[tid("t1"), "test/missing.test.ts".to_owned()], &["t1"])
        .expect("one matched target is enough");
    assert_eq!(outcome.results["m1"].killing_tests, [tid("t1")]);
    assert_eq!(
        outcome.targets[1],
        unmatched("test/missing.test.ts", Unmatched::NotATestFile)
    );
}

#[test]
fn targets_that_match_no_dry_run_file_stop_the_run_before_the_dry_run() {
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let error = run_schedule(
        &spawner,
        &plan(&["m1"]),
        &target_config(&[
            "test/missing.test.ts".to_owned(),
            "test/b.test.ts".to_owned(),
        ]),
    )
    .expect_err("no target matches");
    assert_eq!(
        error,
        ScheduleError::UnmatchedTargets(vec![
            unmatched("test/missing.test.ts", Unmatched::NotATestFile),
            unmatched("test/b.test.ts", Unmatched::NoPlannedCode),
        ])
    );
    assert!(dry_runs_of(&spawner).is_empty(), "no dry run happens");
    let message = error.to_string();
    assert!(message.contains("does not list the file"), "{message}");
    assert!(message.contains("reaches no planned code"), "{message}");
}

fn run_targets_result(
    targets: &[String],
    killers: &'static [&'static str],
) -> Result<(ScheduleOutcome, FakeSpawner), ScheduleError> {
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        move |_, run| {
            let killers: Vec<String> = killers.iter().map(|id| tid(id)).collect();
            Ok(killed_by_each(run, &killers))
        },
    ));
    let outcome = run_schedule(&spawner, &plan(&["m1", "m2"]), &target_config(targets))?;
    Ok((outcome, spawner))
}

#[test]
fn targets_without_a_test_file_of_the_plan_stop_the_run() {
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let mut plan = plan(&["m1"]);
    plan.test_files.clear();
    let error = run_schedule(
        &spawner,
        &plan,
        &target_config(&["test/a.test.ts".to_owned(), "src/a.ts".to_owned()]),
    )
    .expect_err("no test file reaches the planned code");
    assert_eq!(
        error,
        ScheduleError::UnmatchedTargets(vec![
            unmatched("test/a.test.ts", Unmatched::NoPlannedCode),
            unmatched("src/a.ts", Unmatched::NotATestFile),
        ])
    );
    assert!(dry_runs_of(&spawner).is_empty(), "no dry run happens");
}

#[test]
fn targets_without_a_test_file_of_the_runner_in_the_plan_stop_the_run() {
    let spawner = FakeSpawner::new(script(
        |_| standard_dry(),
        |_, run| Ok(run_result(run, RunStatus::Survived, None)),
    ));
    let mut plan = plan(&["m1"]);
    plan.test_files = vec!["test/types.test-d.ts".to_owned()];
    let error = run_schedule(
        &spawner,
        &plan,
        &target_config(&["test/a.test.ts".to_owned()]),
    )
    .expect_err("the dry run runs no file");
    assert_eq!(
        error,
        ScheduleError::UnmatchedTargets(vec![unmatched(
            "test/a.test.ts",
            Unmatched::NoPlannedCode
        )])
    );
    assert!(dry_runs_of(&spawner).is_empty(), "no dry run happens");
}