cargo-tops 0.5.0

init / check / gate for the Rust-TOPS protocol. Orchestrates existing Cargo tools; does not replace them.
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
//! Run a plan: each command in order, timed. A dry run lists the commands
//! and runs none.
use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::process::{ChildStderr, Command, ExitStatus, Stdio};
use std::time::{Instant, SystemTime};

use anyhow::{Context, Result};

use super::plan::{GatePlan, Unit, What};
use super::{Fault, Mode};
use crate::config::Layer;
use crate::evidence;
use crate::facts::{Outcome, Run};
use crate::lock::{self, Line};
use crate::mutation::{self, Outcomes};
use crate::steps::{self, Base, Kind, MutationPlan, Step};
use crate::{laws, TOOL_LOCK};

/// What the laws layer needs before any work starts.
pub(crate) struct Prepared {
    /// The pinned Bend, when the laws layer is on and this is not a dry run.
    pub(crate) judge: Option<laws::Judge>,
}

/// When a layer's commands began, as wall time and as a stopwatch.
struct Clock {
    began: SystemTime,
    watch: Instant,
}

impl Clock {
    fn start() -> Self {
        Self {
            began: SystemTime::now(),
            watch: Instant::now(),
        }
    }
}

/// Wall time and tool version of one layer's commands.
pub(crate) struct Timing {
    pub(crate) layer: String,
    /// When its commands began: a report older than this is not theirs.
    pub(crate) began: SystemTime,
    pub(crate) seconds: f64,
    pub(crate) tool_version: Option<String>,
}

/// What the commands did.
pub(crate) struct Executed<'a> {
    pub(crate) started: SystemTime,
    pub(crate) runs: Vec<Run>,
    pub(crate) timing: Vec<Timing>,
    /// Layers that add no command, each with the layer whose run it takes.
    pub(crate) shared: Vec<(&'a str, &'static str)>,
    /// Enabled, in range, and no plan: a skip is never a pass (H-37).
    pub(crate) not_run: Vec<String>,
    /// Commands named, whether or not they ran.
    pub(crate) listed: usize,
    /// What `cargo mutants` left in this run's `outcomes.json`: `None` when it
    /// did not finish, `Err` when the file is not the shape [`mutation`] reads.
    pub(crate) found: Option<Result<Outcomes, String>>,
}

/// The pinned Bend is fetched first: a missing Bend fails before any work.
pub(crate) fn prepare(dir: &Path, plan: &GatePlan<'_>, mode: Mode) -> Result<Prepared, Fault> {
    if !plan.laws_on || mode == Mode::DryRun {
        return Ok(Prepared { judge: None });
    }
    match laws::ensure_bend(dir) {
        Ok(judge) => Ok(Prepared { judge: Some(judge) }),
        Err(msg) => {
            println!("\nfail: {msg}");
            Err(Fault::announced(msg))
        }
    }
}

/// `cargo` subcommands that have no `--version` of their own.
const CARGO_BUILTINS: [&str; 6] = ["test", "build", "check", "doc", "run", "bench"];

/// The first line `<tool> --version` prints, or `None` when the tool does
/// not answer. A `cargo` subcommand asks itself; a built-in asks `cargo`.
fn version_of(dir: &Path, argv: &[String], cache: &mut Answers) -> Option<String> {
    let (program, rest) = argv.split_first()?;
    ask_cached(
        dir,
        &version_ask(program, rest.first().map(String::as_str)),
        cache,
    )
    .ok()
    .filter(|line| !line.is_empty())
}

/// What each version command answered: its first non-empty line, or why it did
/// not answer.
type Answers = HashMap<String, Result<String, String>>;

/// The first line `asks` prints (empty when it prints none), or why it did not
/// answer within the limit. Each distinct argv is run once per gate, so a
/// pinned tool the gate checked and the version the report names come from
/// the same answer.
fn ask_cached(dir: &Path, asks: &[&str], cache: &mut Answers) -> Result<String, String> {
    cache
        .entry(asks.join(" "))
        .or_insert_with(|| {
            lock::run_probe(dir, asks, lock::PROBE_LIMIT).map(|out| {
                out.lines()
                    .map(str::trim)
                    .find(|line| !line.is_empty())
                    .unwrap_or_default()
                    .to_string()
            })
        })
        .clone()
}

/// The argv that asks `program` for its version, given its first argument.
fn version_ask<'a>(program: &'a str, first: Option<&'a str>) -> Vec<&'a str> {
    match (program, first) {
        ("cargo", Some(sub)) if !sub.starts_with('-') && !CARGO_BUILTINS.contains(&sub) => {
            vec!["cargo", sub, "--version"]
        }
        ("cargo", _) => vec!["cargo", "--version"],
        (other, _) => vec![other, "--version"],
    }
}

/// The first tool step of `chain`: the command whose version names the layer.
fn first_tool(chain: &[Step]) -> Option<&[String]> {
    chain
        .iter()
        .find(|step| step.kind == Kind::Tool)
        .map(|step| step.argv.as_slice())
}

/// Execute `plan`. Every command prints its `==>` line first, then runs.
pub(crate) fn run<'a>(dir: &Path, plan: &GatePlan<'a>, mode: Mode) -> Result<Executed<'a>, Fault> {
    let mut runner = Runner {
        dir,
        plan,
        live: mode == Mode::Run,
        done: Executed {
            started: SystemTime::now(),
            runs: Vec::new(),
            timing: Vec::new(),
            shared: Vec::new(),
            not_run: Vec::new(),
            listed: 0,
            found: None,
        },
        versions: HashMap::new(),
        asked: HashMap::new(),
    };
    for warning in &plan.pin_warnings {
        println!("{warning}");
    }
    for unit in &plan.units {
        // A layer that needs no pinned tool has no pins to ask.
        if !runner.pinned_tools_stop(unit.layer) {
            runner.unit(unit)?;
        }
    }
    if runner.live {
        runner.take_shared();
    } else {
        runner.list_laws();
    }
    Ok(runner.done)
}

/// The state of one execution.
struct Runner<'p, 'a> {
    dir: &'p Path,
    plan: &'p GatePlan<'a>,
    /// `false` for a dry run: commands are listed, never run.
    live: bool,
    done: Executed<'a>,
    versions: Answers,
    /// What each pinned tool answered, asked once per gate.
    asked: HashMap<&'static str, Line>,
}

impl<'a> Runner<'_, 'a> {
    fn unit(&mut self, unit: &Unit<'a>) -> Result<(), Fault> {
        let layer = unit.layer;
        match &unit.what {
            What::Shared(from) => self.shared(layer, from),
            What::NotRun => self.not_run(layer),
            What::Mutation(mutation) => self.mutation(layer, mutation)?,
            What::Chains(chains) => {
                for chain in chains {
                    self.chain(layer, chain)?;
                }
            }
        }
        Ok(())
    }

    /// Asks each pinned tool `layer` needs for its version, before any of the
    /// layer's commands ([`Runner::ask_pins`]). `true` when the layer stopped
    /// on one. A dry run only names the pins.
    fn pinned_tools_stop(&mut self, layer: &Layer) -> bool {
        let pins = self.plan.pins_of(&layer.id);
        if pins.is_empty() {
            return false;
        }
        let named: Vec<String> = pins
            .iter()
            .map(|(row, pin)| format!("{} {pin}", row.key))
            .collect();
        println!(
            "\n==> [{}] {TOOL_LOCK} pins, asked first: {}",
            layer.id,
            named.join(", ")
        );
        if !self.live {
            return false;
        }
        let clock = Clock::start();
        let Some(run) = self.ask_pins(layer) else {
            return false;
        };
        self.done.record(run, &clock, None);
        true
    }

    /// The run that stands for `layer` when a pinned tool it needs is at
    /// another version ([`Outcome::ToolMismatch`]) or will not answer
    /// ([`Outcome::ToolMissing`]), the note carrying every such tool in the
    /// words of [`lock::Line::text`]; `None` when every tool answers with its
    /// pin. Each tool is asked once per gate.
    fn ask_pins(&mut self, layer: &Layer) -> Option<Run> {
        let dir = self.dir;
        let mut stops = Vec::new();
        for (row, pin) in self.plan.pins_of(&layer.id) {
            let line = self.asked.entry(row.key).or_insert_with(|| {
                lock::check_row(row, pin, &mut |argv| {
                    ask_cached(dir, argv, &mut self.versions)
                })
            });
            stops.extend(line.stop());
        }
        let first = stops.first()?;
        let note: Vec<&str> = stops.iter().map(|stop| stop.text.as_str()).collect();
        let outcome = if stops.iter().any(|stop| stop.mismatch) {
            Outcome::ToolMismatch
        } else {
            Outcome::ToolMissing
        };
        Some(pin_stop_run(
            &layer.id,
            outcome,
            &first.asked,
            note.join("; "),
        ))
    }

    fn shared(&mut self, layer: &'a Layer, from: &'static str) {
        println!(
            "\n==> [{}] no command of its own: it takes the {from} run's outcome",
            layer.id
        );
        self.done.shared.push((&layer.id, from));
    }

    fn not_run(&mut self, layer: &Layer) {
        let tier = crate::tiers::tier_for(layer);
        println!(
            "\nNotRun [{}]: enabled at tier {tier} (<= --max-tier {}) and no command is mapped",
            layer.id, self.plan.max_tier
        );
        self.done.not_run.push(format!(
            "{}: NotRun (enabled at tier {tier}, no command mapped; disable it or lower --max-tier)",
            layer.id
        ));
    }

    fn mutation(&mut self, layer: &Layer, mutation: &MutationPlan) -> Result<(), Fault> {
        println!(
            "\n==> [{}] {} && {}",
            layer.id,
            mutation.diff.display(),
            mutation.mutants.display()
        );
        self.done.listed += 1;
        if self.live {
            let clock = Clock::start();
            let Mutated { run, found } =
                run_mutation(self.dir, &layer.id, mutation).map_err(Fault::env)?;
            self.done.found = found;
            // The tool is asked for its version only when it ran: a vacuous
            // or failed diff runs no `cargo mutants`.
            let version = (run.outcome != Outcome::ToolMissing
                && run.cmd.contains(&mutation.mutants.display()))
            .then(|| version_of(self.dir, &mutation.mutants.argv, &mut self.versions))
            .flatten();
            self.done.record(run, &clock, version);
        }
        Ok(())
    }

    fn chain(&mut self, layer: &Layer, chain: &[Step]) -> Result<(), Fault> {
        println!(
            "\n==> [{}] {}",
            layer.id,
            chain
                .iter()
                .map(Step::display)
                .collect::<Vec<_>>()
                .join(" && ")
        );
        self.done.listed += 1;
        if !self.live {
            return Ok(());
        }
        let clock = Clock::start();
        let run = run_chain(self.dir, &layer.id, chain).map_err(Fault::env)?;
        let version = first_tool(chain)
            // A tool that is not there has no version to ask for, and crap's
            // is named only when the lock pinned it.
            .filter(|_| run.outcome != Outcome::ToolMissing)
            .filter(|_| layer.id != "crap" || !self.plan.pins_of("crap").is_empty())
            .and_then(|argv| version_of(self.dir, argv, &mut self.versions));
        self.done.record(run, &clock, version);
        Ok(())
    }

    /// A dry run names what the laws layer would run.
    fn list_laws(&self) {
        if self.plan.laws_on {
            println!(
                "\n==> [laws] sh {} (only if Bend from {} is not installed)",
                laws::FETCH,
                laws::LOCK
            );
            println!(
                "\n==> [laws] bend {}/cases.bend",
                laws::report_display(self.dir)
            );
        }
    }

    /// The run `layer` takes from `from`. `property` and `acceptance` pass
    /// only when that run also wrote a JUnit report holding a passed test
    /// they select by name; the report is read, never produced (HN-P3).
    fn credited(&self, layer: &str, from: &str) -> Run {
        let mut run = self.plan.tests.take(layer, from, &self.done.runs);
        let Some(filter) = self.plan.filters.for_layer(layer) else {
            return run;
        };
        if run.outcome != Outcome::Passed {
            return run;
        }
        let root = laws::workspace_root(self.dir).unwrap_or_else(|| self.dir.to_path_buf());
        // A report older than the credited run's own start is not its report.
        let since = self
            .done
            .timing
            .iter()
            .find(|timing| timing.layer == from)
            .map_or(self.done.started, |timing| timing.began);
        if let Err(why) = evidence::credit(layer, &root, filter, since) {
            run.outcome = Outcome::Failed;
            run.note = Some(why);
        }
        run
    }

    /// A layer that adds no command records its source's run.
    fn take_shared(&mut self) {
        for (layer, from) in self.done.shared.clone() {
            let run = self.credited(layer, from);
            match &run.note {
                Some(note) => {
                    println!("[{layer}] took the {from} run, but it is no credit: {note}");
                }
                None => println!("[{layer}] took the {from} run's outcome: {}", run.outcome),
            }
            self.done.runs.push(run);
            self.done.timing.push(Timing {
                layer: layer.to_string(),
                began: SystemTime::now(),
                seconds: 0.0,
                tool_version: None,
            });
        }
    }
}

impl Executed<'_> {
    /// Keep `run` with the time since `clock` and the tool's version.
    fn record(&mut self, run: Run, clock: &Clock, tool_version: Option<String>) {
        self.timing.push(Timing {
            layer: run.layer.clone(),
            began: clock.began,
            seconds: clock.watch.elapsed().as_secs_f64(),
            tool_version,
        });
        self.runs.push(run);
    }

    /// Seconds spent on `layer`, over all its commands.
    pub(crate) fn seconds(&self, layer: &str) -> f64 {
        self.timing
            .iter()
            .filter(|timing| timing.layer == layer)
            .map(|timing| timing.seconds)
            .sum::<f64>()
            // An empty float sum is -0.0; a layer that never ran took 0.0.
            + 0.0
    }

    /// The tool versions seen for `layer`, once each, joined.
    pub(crate) fn tool_version(&self, layer: &str) -> Option<String> {
        let mut seen: Vec<&str> = Vec::new();
        for version in self
            .timing
            .iter()
            .filter(|timing| timing.layer == layer)
            .filter_map(|timing| timing.tool_version.as_deref())
        {
            if !seen.contains(&version) {
                seen.push(version);
            }
        }
        (!seen.is_empty()).then(|| seen.join("; "))
    }
}

/// The steps that ran, joined, and the outcome of the last one.
pub(crate) fn finish(layer: &str, ran: &[String], outcome: Outcome) -> Run {
    Run {
        layer: layer.to_string(),
        cmd: ran.join(" && "),
        step: ran.last().cloned().unwrap_or_default(),
        outcome,
        note: None,
    }
}

pub(crate) fn run_chain(dir: &Path, layer: &str, chain: &[Step]) -> Result<Run> {
    let mut ran = Vec::new();
    let mut outcome = Outcome::Passed;
    for step in chain {
        ran.push(step.display());
        outcome = run_step(dir, step)?;
        if !steps::chain_continues(outcome) {
            break;
        }
    }
    Ok(finish(layer, &ran, outcome))
}

/// A layer that stopped because a pinned tool prints another version than its
/// pin (`outcome` is a mismatch) or would not answer (a missing tool). The same `note` is the stdout line,
/// the layer's detail and its problem.
fn pin_stop_run(layer: &str, outcome: Outcome, asked: &str, note: String) -> Run {
    let label = if outcome == Outcome::ToolMismatch {
        "tool-mismatch"
    } else {
        "tool-missing"
    };
    println!("{label} [{layer}]: {note}");
    Run {
        layer: layer.to_string(),
        cmd: String::new(),
        step: asked.to_string(),
        outcome,
        note: Some(note),
    }
}

/// A mutation layer's run, and what its `outcomes.json` held.
pub(crate) struct Mutated {
    pub(crate) run: Run,
    pub(crate) found: Option<Result<Outcomes, String>>,
}

impl Mutated {
    fn without_outcomes(run: Run) -> Self {
        Self { run, found: None }
    }
}

/// `cargo-mutants` is looked up before the diff, so an empty diff cannot
/// hide a missing tool. A base that was named and is not a commit fails the
/// layer before any command runs. A failed diff is `Failed`; nothing waives
/// it. An empty one is `Vacuous` and `cargo mutants` does not run, except
/// that no base at all fails under CI. [`steps::stop_note`] words every one
/// of these stops. When `cargo mutants` runs, an `outcomes.json` from an
/// earlier run is removed first, and exit 0, 2 (missed) and 3 (timeouts)
/// are a finished run whose ledger the judge reads.
pub(crate) fn run_mutation(dir: &Path, layer: &str, plan: &MutationPlan) -> Result<Mutated> {
    if let Err(missing) = steps::check_tool(&plan.mutants.argv) {
        println!("{missing}");
        return Ok(Mutated::without_outcomes(finish(
            layer,
            &[plan.mutants.display()],
            Outcome::ToolMissing,
        )));
    }
    if matches!(plan.base, Base::Unresolved { .. }) {
        return Ok(Mutated::without_outcomes(stopped(
            layer,
            &[],
            &plan.base,
            Outcome::Failed,
        )));
    }
    let mut ran = vec![plan.diff.display()];
    let diff_ok = run_step(dir, &plan.diff)? == Outcome::Passed;
    let diff = steps::judge_diff(diff_ok, &plan.diff_path);
    let Some(mutants) = plan.after_diff(diff) else {
        return Ok(Mutated::without_outcomes(stopped(
            layer, &ran, &plan.base, diff,
        )));
    };
    ran.push(mutants.display());
    mutation::clear(&plan.output)?;
    let ended = run_mutants(dir, mutants)?;
    Ok(settled(
        finish(layer, &ran, ended.outcome),
        &ended,
        &plan.output,
    ))
}

/// The run of `cargo mutants` once it ended. A command that failed says what
/// its exit means; one that finished (exit 0, 2 or 3) is judged from the
/// outcomes it wrote.
fn settled(mut run: Run, ended: &Ended, output: &Path) -> Mutated {
    if ended.outcome != Outcome::Passed {
        // A missing tool has its own line; a failed run says what its exit means.
        run.note = (ended.outcome == Outcome::Failed)
            .then(|| format!("`{}` {}", run.step, exit_meaning(ended.code)));
        return Mutated::without_outcomes(run);
    }
    let found = mutation::read(output, ended.code.unwrap_or_default(), &ended.stderr);
    if let Err(why) = &found {
        run.outcome = Outcome::Failed;
        run.note = Some(why.clone());
    }
    Mutated {
        run,
        found: Some(found),
    }
}

/// What a failed `cargo mutants` exit means, in cargo-mutants' own terms.
fn exit_meaning(code: Option<i32>) -> String {
    match code {
        Some(1) => "exited 1: it was given arguments it does not accept".to_string(),
        Some(4) => {
            "exited 4: the tests fail in the unmutated tree, so no mutant was tested".to_string()
        }
        Some(code) => format!("exited {code}"),
        None => "was ended by a signal".to_string(),
    }
}

/// The run of a layer that stopped on its base or its diff: prints the
/// note and keeps it with the run, so the report says the same. A diff
/// command that ran and failed is named in the kept note, since it did fail;
/// a base that was never tried, or an empty diff, names no command.
fn stopped(layer: &str, ran: &[String], base: &Base, diff: Outcome) -> Run {
    let (outcome, note) = steps::stop_note(base, diff)
        .unwrap_or_else(|| (diff, format!("the mutation diff ended as {diff}")));
    println!("{outcome}: {note}");
    let command_failed = diff == Outcome::Failed && !matches!(base, Base::Unresolved { .. });
    let kept = match ran.last() {
        Some(step) if command_failed => format!("`{step}` failed: {note}"),
        _ => note,
    };
    Run {
        note: Some(kept),
        ..finish(layer, ran, outcome)
    }
}

pub(crate) fn run_step(dir: &Path, step: &Step) -> Result<Outcome> {
    match step.kind {
        Kind::Artifact => {
            return Ok(step.artifact_path().map_or(Outcome::Failed, |path| {
                steps::artifact_outcome(&dir.join(path))
            }));
        }
        Kind::Tool => {
            if let Err(missing) = steps::check_tool(&step.argv) {
                println!("{missing}");
                return Ok(Outcome::ToolMissing);
            }
        }
        Kind::Command => {}
    }
    Ok(match spawn(dir, step)? {
        Spawned::Exited(status) if status.success() => Outcome::Passed,
        Spawned::Exited(_) | Spawned::NoProgram => Outcome::Failed,
        Spawned::Missing => Outcome::ToolMissing,
    })
}

/// How `cargo mutants` ended: its outcome, exit code and what it printed.
struct Ended {
    outcome: Outcome,
    code: Option<i32>,
    stderr: String,
}

impl Ended {
    fn without_output(outcome: Outcome) -> Self {
        Self {
            outcome,
            code: None,
            stderr: String::new(),
        }
    }
}

/// `cargo mutants` exits 2 when mutants were missed and 3 when some timed
/// out: those and 0 are a finished run, judged by the ledger. Any other exit
/// is a failed command. What it prints to stderr is shown and kept, because
/// a run that found nothing to mutate says so there and writes no file.
fn run_mutants(dir: &Path, step: &Step) -> Result<Ended> {
    let Some((program, args)) = step.program() else {
        return Ok(Ended::without_output(Outcome::Failed));
    };
    let mut child = match Command::new(program)
        .args(args)
        .current_dir(dir)
        .stderr(Stdio::piped())
        .spawn()
    {
        Ok(child) => child,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            println!(
                "{}",
                steps::MissingTool {
                    binary: program.to_string()
                }
            );
            return Ok(Ended::without_output(Outcome::ToolMissing));
        }
        Err(err) => return Err(err).with_context(|| format!("spawn: {}", step.display())),
    };
    let stderr = tee(child.stderr.take());
    let status = child
        .wait()
        .with_context(|| format!("wait: {}", step.display()))?;
    let code = status.code();
    let outcome = if matches!(code, Some(0 | 2 | 3)) {
        Outcome::Passed
    } else {
        Outcome::Failed
    };
    Ok(Ended {
        outcome,
        code,
        stderr,
    })
}

/// Everything `pipe` yields, passed on to our own stderr as it comes.
fn tee(pipe: Option<ChildStderr>) -> String {
    let Some(pipe) = pipe else {
        return String::new();
    };
    let mut kept = String::new();
    for line in BufReader::new(pipe).split(b'\n').map_while(Result::ok) {
        let line = String::from_utf8_lossy(&line).into_owned();
        eprintln!("{line}");
        kept.push_str(&line);
        kept.push('\n');
    }
    kept
}

/// How a spawned command ended.
enum Spawned {
    Exited(ExitStatus),
    /// The program is not there; the missing-tool line is already printed.
    Missing,
    /// The step names no program.
    NoProgram,
}

fn spawn(dir: &Path, step: &Step) -> Result<Spawned> {
    let Some((program, args)) = step.program() else {
        return Ok(Spawned::NoProgram);
    };
    match Command::new(program).args(args).current_dir(dir).status() {
        Ok(status) => Ok(Spawned::Exited(status)),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            println!(
                "{}",
                steps::MissingTool {
                    binary: program.to_string()
                }
            );
            Ok(Spawned::Missing)
        }
        Err(err) => Err(err).with_context(|| format!("spawn: {}", step.display())),
    }
}

#[cfg(test)]
mod tests {
    use super::{exit_meaning, run_mutants, run_step, version_ask, Executed, Timing};
    use crate::facts::Outcome;
    use crate::steps::Step;
    use std::fs;
    use std::path::Path;
    use std::time::SystemTime;

    #[test]
    fn a_plugin_asks_itself_and_a_built_in_asks_cargo() {
        assert_eq!(
            version_ask("cargo", Some("deny")),
            ["cargo", "deny", "--version"]
        );
        for built_in in ["test", "build", "check", "doc", "run", "bench"] {
            assert_eq!(
                version_ask("cargo", Some(built_in)),
                ["cargo", "--version"],
                "{built_in}"
            );
        }
        assert_eq!(
            version_ask("cargo", Some("--version")),
            ["cargo", "--version"]
        );
        assert_eq!(version_ask("cargo", None), ["cargo", "--version"]);
        assert_eq!(version_ask("bend", Some("run")), ["bend", "--version"]);
    }

    fn timing(layer: &str, seconds: f64) -> Timing {
        Timing {
            layer: layer.to_string(),
            began: SystemTime::UNIX_EPOCH,
            seconds,
            tool_version: None,
        }
    }

    #[test]
    fn a_layer_is_timed_over_its_own_commands_only() {
        let done = Executed {
            started: SystemTime::now(),
            runs: Vec::new(),
            timing: vec![timing("a", 1.5), timing("b", 4.0), timing("a", 2.0)],
            shared: Vec::new(),
            not_run: Vec::new(),
            listed: 0,
            found: None,
        };
        assert!((done.seconds("a") - 3.5).abs() < 1e-9);
        assert!((done.seconds("b") - 4.0).abs() < 1e-9);
        // A layer that never ran took 0.0, not -0.0.
        assert!(done.seconds("c").abs() < 1e-9 && done.seconds("c").is_sign_positive());
    }

    #[test]
    fn a_missing_program_is_a_missing_tool_and_any_other_spawn_error_is_not() {
        let dir = std::env::temp_dir().join(format!("cargo-tops-exec-{}", std::process::id()));
        fs::remove_dir_all(&dir).ok();
        fs::create_dir_all(&dir).expect("mkdir");
        let missing = Step::command([dir.join("no-such-program").display().to_string()]);
        assert_eq!(
            run_step(Path::new("."), &missing).expect("a missing program is an outcome"),
            Outcome::ToolMissing
        );
        // A file with no execute bit exists but cannot be spawned.
        let plain = dir.join("plain");
        fs::write(&plain, "x").expect("write");
        let blocked = Step::command([plain.display().to_string()]);
        let err = run_step(Path::new("."), &blocked).expect_err("not spawnable");
        assert!(format!("{err:#}").contains("spawn"), "{err:#}");
        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn cargo_mutants_exits_0_2_and_3_are_a_finished_run() {
        let wants = [
            (0, Outcome::Passed),
            (1, Outcome::Failed),
            (2, Outcome::Passed),
            (3, Outcome::Passed),
            (4, Outcome::Failed),
            (101, Outcome::Failed),
        ];
        for (code, want) in wants {
            let step = Step::command(["sh".to_string(), "-c".to_string(), format!("exit {code}")]);
            let got = run_mutants(Path::new("."), &step).expect("it ran");
            assert_eq!(got.outcome, want, "exit {code}");
            assert_eq!(got.code, Some(code), "exit {code}");
            // Any other command still fails on every nonzero exit.
            let plain = run_step(Path::new("."), &step).expect("it ran");
            let plain_want = if code == 0 {
                Outcome::Passed
            } else {
                Outcome::Failed
            };
            assert_eq!(plain, plain_want, "exit {code}");
        }
        let nowhere = Step::command(["/no/such/program/here"]);
        assert_eq!(
            run_mutants(Path::new("."), &nowhere)
                .expect("an outcome")
                .outcome,
            Outcome::ToolMissing
        );
    }

    #[test]
    fn a_mutants_spawn_error_is_an_error_and_a_signal_is_named() {
        let dir = std::env::temp_dir().join(format!("cargo-tops-mutexec-{}", std::process::id()));
        fs::remove_dir_all(&dir).ok();
        fs::create_dir_all(&dir).expect("mkdir");
        let plain = dir.join("plain");
        fs::write(&plain, "x").expect("write");
        let blocked = Step::command([plain.display().to_string()]);
        let err = run_mutants(Path::new("."), &blocked)
            .err()
            .expect("not spawnable");
        assert!(format!("{err:#}").contains("spawn"), "{err:#}");
        fs::remove_dir_all(&dir).ok();
        let killed = Step::command(["sh".to_string(), "-c".to_string(), "kill -9 $$".to_string()]);
        let ended = run_mutants(Path::new("."), &killed).expect("it ran");
        assert_eq!((ended.outcome, ended.code), (Outcome::Failed, None));
        assert!(exit_meaning(None).contains("signal"));
        assert!(exit_meaning(Some(4)).contains("unmutated tree"));
        assert!(exit_meaning(Some(1)).contains("exited 1"));
        assert_eq!(exit_meaning(Some(77)), "exited 77");
    }
}