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
//! `cargo tops gate`: plan, execute, judge, report.
//!
//! - [`plan`] decides which layers run and with what, and runs nothing.
//! - [`execute`] runs the commands and times them.
//! - [`judge`] turns the runs, the floors and the laws into problems.
//! - [`report`] prints the summary, writes `report.json` and picks the exit
//!   code.
//!
//! Exit codes: 0 pass; 1 the gate ran and failed (a dry run included, and an
//! expired exemption); 2 the config or the request cannot be gated
//! (`rust-tops.yaml` missing or failing check, `rust-tops.lock` unreadable,
//! no enabled layer at or below `--max-tier`); 3 the environment (a required
//! tool or `cargo metadata` is missing, a pinned tool prints another version
//! than its pin in `rust-tops.lock`, or `report.json` cannot be written).
//! Where more than one applies, 2 wins over 3, and 3 over 1; every problem is
//! still listed in the report.
use std::path::Path;
use std::process::ExitCode;

use crate::config::TopsConfig;
use crate::laws;

pub(crate) mod execute;
pub(crate) mod judge;
pub(crate) mod plan;
pub(crate) mod report;

/// Whether the gate runs its commands or only lists them.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Mode {
    Run,
    DryRun,
}

impl From<bool> for Mode {
    /// `true` is `--dry-run`.
    fn from(dry_run: bool) -> Self {
        if dry_run {
            Self::DryRun
        } else {
            Self::Run
        }
    }
}

/// What `gate` was asked for.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Options {
    pub(crate) max_tier: u8,
    pub(crate) mode: Mode,
}

/// Why a gate could not reach a verdict from its commands.
pub(crate) struct Fault {
    /// `true` for the config (exit 2), `false` for the environment (exit 3).
    pub(crate) config: bool,
    pub(crate) error: anyhow::Error,
    /// The message is already on stdout.
    pub(crate) announced: bool,
}

impl Fault {
    pub(crate) fn config(error: anyhow::Error) -> Self {
        Self {
            config: true,
            error,
            announced: false,
        }
    }

    pub(crate) fn env(error: anyhow::Error) -> Self {
        Self {
            config: false,
            error,
            announced: false,
        }
    }

    /// An environment fault the run has already printed.
    pub(crate) fn announced(message: String) -> Self {
        Self {
            announced: true,
            ..Self::env(anyhow::anyhow!(message))
        }
    }
}

/// Run the gate for `cfg` under `dir`: always writes `report.json`.
/// `base_flag` is `--base`, the first source of the mutation base.
pub(crate) fn run(
    dir: &Path,
    cfg: &TopsConfig,
    opts: Options,
    base_flag: Option<&str>,
) -> ExitCode {
    // A gate resolves `cargo metadata` afresh, once per project.
    laws::begin_gate();
    let plan = match plan::build(dir, cfg, opts.max_tier, base_flag) {
        Ok(plan) => plan,
        Err(fault) => return report::fault(dir, Some(cfg), opts, None, &fault),
    };
    // A report from an earlier gate must never read as this one's.
    report::clear(&plan.report);
    println!(
        "rust-tops gate: {} ({}) max-tier={}",
        cfg.crate_.name, cfg.crate_.class, opts.max_tier
    );
    let prepared = match execute::prepare(dir, &plan, opts.mode) {
        Ok(prepared) => prepared,
        Err(fault) => return report::fault(dir, Some(cfg), opts, Some(&plan), &fault),
    };
    report::announce_skipped(&plan);
    let done = match execute::run(dir, &plan, opts.mode) {
        Ok(done) => done,
        Err(fault) => return report::fault(dir, Some(cfg), opts, Some(&plan), &fault),
    };
    match judge::judge(dir, cfg, &plan, &done, prepared, opts.mode) {
        Ok(judged) => report::finish(dir, cfg, opts, &plan, &done, &judged),
        Err(fault) => report::fault(dir, Some(cfg), opts, Some(&plan), &fault),
    }
}

/// The config could not be loaded or failed `check`: exit 2. The report is
/// written when `dir` exists, so the target is known.
pub(crate) fn config_error(dir: &Path, error: &anyhow::Error, opts: Options) -> ExitCode {
    laws::begin_gate();
    eprintln!("error: {error:#}");
    report::config_invalid(dir, error, opts)
}

#[cfg(test)]
mod tests {
    use super::judge::{command_count, ran_commands};
    use super::plan::{plan_for, Filters, Plan, TestCommands};
    use crate::facts::{Outcome, Run};
    use crate::steps::{self, Base, Kind, Step};
    use crate::{config, credit};
    use std::fs;
    use std::path::Path;

    fn layer(id: &str) -> config::Layer {
        serde_saphyr::from_str(&format!(
            "{{ id: {id}, enabled: true, tier: 0, runner: x }}"
        ))
        .expect("layer")
    }

    fn chains(id: &str) -> Vec<Vec<Step>> {
        match plan_for(
            &layer(id),
            Path::new("/r"),
            &Base::unused(),
            &Filters::default(),
        ) {
            Some(Plan::Chains(chains)) => chains,
            _ => panic!("{id} has no chains"),
        }
    }

    fn tool_step(chain: &[Step]) -> &Step {
        chain
            .iter()
            .find(|step| step.kind == Kind::Tool)
            .expect("a tool step")
    }

    #[test]
    fn the_g0_strings_name_the_tool_they_run() {
        let coverage = "mkdir -p target/coverage && cargo llvm-cov nextest --workspace --all-features --profile ci --lcov --output-path target/coverage/lcov.info && test -s target/coverage/lcov.info";
        let mutation = "sh -c 'git diff base...HEAD --unified=0 > /tmp/rust-tops.diff; cargo mutants --in-diff /tmp/rust-tops.diff -j 2'";
        assert_eq!(
            steps::tool_binary(&[coverage.to_string()]).as_deref(),
            Some("cargo-llvm-cov")
        );
        assert_eq!(
            steps::tool_binary(&[mutation.to_string()]).as_deref(),
            Some("cargo-mutants")
        );
    }

    #[test]
    fn coverage_and_crap_check_their_own_binary() {
        let coverage = chains("coverage");
        assert_eq!(coverage.len(), 1);
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let tops_idx_1285_44 = coverage[0].clone();
        assert_eq!(
            steps::tool_binary(&tool_step(&tops_idx_1285_44).argv).as_deref(),
            Some("cargo-llvm-cov")
        );
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let tops_idx_1293_13 = coverage[0].clone();
        assert_eq!(
            tops_idx_1293_13.last().map(|step| step.kind),
            Some(Kind::Artifact)
        );
        let crap = chains("crap");
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let tops_idx_1302_44 = crap[0].clone();
        assert_eq!(
            steps::tool_binary(&tool_step(&tops_idx_1302_44).argv).as_deref(),
            Some("cargo-crap")
        );
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let tops_idx_1310_13 = crap[0].clone();
        assert_eq!(
            tops_idx_1310_13.last().and_then(Step::artifact_path),
            Some(Path::new("/r/crap.json"))
        );
    }

    #[test]
    fn coverage_and_crap_write_under_the_artifact_dir() {
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let coverage: Vec<String> = chains("coverage")[0].iter().map(Step::display).collect();
        assert_eq!(
            coverage,
            [
                "mkdir -p /r",
                "cargo llvm-cov nextest --workspace --all-features --profile ci --lcov --output-path /r/lcov.info",
                "test -s /r/lcov.info",
                "cargo llvm-cov report --json --summary-only --output-path /r/coverage.json",
                "test -s /r/coverage.json",
            ]
        );
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let crap: Vec<String> = chains("crap")[0].iter().map(Step::display).collect();
        assert_eq!(
            crap,
            [
                "cargo crap --lcov /r/lcov.info --format json --output /r/crap.json",
                "test -s /r/crap.json",
            ]
        );
    }

    fn argv(words: &str) -> Vec<String> {
        words.split_whitespace().map(str::to_string).collect()
    }

    const COVERAGE: &str = "cargo llvm-cov nextest --workspace --all-features --profile ci --lcov";
    const UNIT: &str = "cargo nextest run --workspace --all-features --profile ci";

    #[expect(
        clippy::fn_params_excessive_bools,
        reason = "already in this crate when the strict table was turned on"
    )]
    fn tests_with(in_range: bool, coverage: &str, integration: &str) -> TestCommands {
        TestCommands {
            coverage_in_range: in_range,
            coverage: argv(coverage),
            unit: argv(UNIT),
            integration: argv(integration),
        }
    }

    fn run(layer: &str, outcome: Outcome) -> Run {
        Run {
            layer: layer.to_string(),
            cmd: format!("{layer} cmd"),
            step: format!("{layer} step"),
            outcome,
            note: None,
        }
    }

    #[test]
    fn the_shipped_test_commands_share_one_coverage_run() {
        let layers: Vec<config::Layer> = ["unit", "integration", "coverage", "property"]
            .into_iter()
            .map(layer)
            .collect();
        let enabled: Vec<&config::Layer> = layers.iter().collect();
        let tests = TestCommands::new(&enabled, Path::new("/r"), &Filters::default());
        assert!(tests.coverage_in_range);
        assert!(
            credit::coverage_grants_credit(&tests.coverage),
            "{:?}",
            tests.coverage
        );
        assert_eq!(tests.unit, tests.integration);
        for id in ["unit", "integration", "property"] {
            assert_eq!(tests.shared_from(id), Some("coverage"), "{id}");
        }
        for id in ["coverage", "doc", "static-lint", "crap"] {
            assert_eq!(tests.shared_from(id), None, "{id}");
        }
        // Out of range, nothing names a coverage command and none is shared.
        let unit_only: Vec<&config::Layer> = enabled
            .iter()
            .copied()
            .filter(|l| l.id != "coverage")
            .collect();
        let tests = TestCommands::new(&unit_only, Path::new("/r"), &Filters::default());
        assert!(!tests.coverage_in_range);
        assert!(tests.coverage.is_empty());
        assert_eq!(tests.shared_from("integration"), Some("unit"));
        assert_eq!(tests.shared_from("unit"), None);
        assert_eq!(tests.shared_from("property"), None);
    }

    #[test]
    fn a_coverage_command_missing_a_flag_shares_nothing() {
        for missing in ["--workspace", "--all-features", "--profile ci"] {
            let coverage = COVERAGE.replace(&format!(" {missing}"), "");
            let tests = tests_with(true, &coverage, UNIT);
            for id in ["unit", "integration", "property"] {
                assert_eq!(tests.shared_from(id), None, "{missing}: {id}");
            }
        }
        let tests = tests_with(
            true,
            &COVERAGE.replace("--profile ci", "--profile default"),
            UNIT,
        );
        assert_eq!(tests.shared_from("unit"), None);
    }

    #[test]
    fn integration_takes_unit_only_when_its_command_is_unit_s() {
        let tests = tests_with(false, COVERAGE, UNIT);
        assert_eq!(tests.shared_from("integration"), Some("unit"));
        let tests = tests_with(false, COVERAGE, &format!("{UNIT} --test it"));
        assert_eq!(tests.shared_from("integration"), None);
        // Unit off or out of range: no unit command, so integration runs.
        let mut tests = tests_with(false, COVERAGE, UNIT);
        tests.unit.clear();
        assert_eq!(tests.shared_from("integration"), None);
    }

    #[test]
    fn a_shared_layer_records_its_source_run_and_no_source_is_no_pass() {
        let tests = tests_with(true, COVERAGE, UNIT);
        for outcome in [Outcome::Passed, Outcome::Failed, Outcome::ToolMissing] {
            let took = tests.take("unit", "coverage", &[run("coverage", outcome)]);
            assert_eq!(took.outcome, outcome);
            assert_eq!(took.layer, "unit");
            assert_eq!(took.cmd, "coverage cmd");
            assert_eq!(took.step, "coverage step");
        }
        let took = tests.take("unit", "coverage", &[run("coverage", Outcome::Vacuous)]);
        assert_eq!(took.outcome, Outcome::Failed);
        let took = tests.take("property", "coverage", &[run("unit", Outcome::Passed)]);
        assert_eq!(took.outcome, Outcome::Failed);
        assert_eq!(took.cmd, "");
        let tests = tests_with(false, COVERAGE, UNIT);
        let took = tests.take("integration", "unit", &[run("unit", Outcome::Failed)]);
        assert_eq!(took.outcome, Outcome::Failed);
        assert_eq!(took.cmd, "unit cmd");
    }

    #[test]
    fn a_shared_run_is_not_a_command_and_its_source_is_named() {
        let runs = [
            run("coverage", Outcome::Passed),
            run("unit", Outcome::Passed),
            run("doc", Outcome::ToolMissing),
        ];
        assert_eq!(ran_commands(&runs, false, &[("unit", "coverage")]), 1);
        assert_eq!(ran_commands(&runs, true, &[]), 3);
        let layers: Vec<config::Layer> =
            ["coverage", "unit", "doc"].into_iter().map(layer).collect();
        let enabled: Vec<&config::Layer> = layers.iter().collect();
        let (line, short) = command_count(1, &enabled, &runs, false, 1, &[("unit", "coverage")]);
        assert!(short);
        assert_eq!(
            line,
            "count: 1 command(s) ran; 2 of 3 enabled layer(s) at tier <= 1 ran one; unit took the coverage run; none ran for: doc"
        );
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let (line, short) =
            command_count(2, &enabled[..2], &runs, false, 1, &[("unit", "coverage")]);
        assert!(!short);
        assert!(
            line.ends_with("ran one; unit took the coverage run"),
            "{line}"
        );
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let (line, _) = command_count(2, &enabled[..2], &runs, false, 1, &[("unit", "unit")]);
        assert!(line.ends_with("ran one; unit took the unit run"), "{line}");
    }

    #[test]
    fn the_mutation_diff_uses_merge_base_and_the_report_dir() {
        let report = Path::new("/work/target/rust-tops");
        let base = Base::Found {
            source: steps::BaseSource::Flag,
            name: "origin/trunk".to_string(),
        };
        let Some(Plan::Mutation(plan)) =
            plan_for(&layer("mutation"), report, &base, &Filters::default())
        else {
            panic!("mutation has no plan");
        };
        assert_eq!(plan.diff_path, report.join("rust-tops.diff"));
        let script = plan.diff.argv.join(" ");
        assert!(
            script.contains("git merge-base 'origin/trunk' HEAD"),
            "{script}"
        );
        assert!(!script.contains("origin/main"), "{script}");
        assert!(script.contains("&& git diff"), "{script}");
        assert!(!script.contains("mutants"), "{script}");
        assert!(!script.contains("/tmp"), "{script}");
        assert!(plan
            .mutants
            .argv
            .contains(&"/work/target/rust-tops/rust-tops.diff".to_string()));
        assert_eq!(
            steps::tool_binary(&plan.mutants.argv).as_deref(),
            Some("cargo-mutants")
        );
    }

    #[test]
    fn no_mapped_layer_writes_under_tmp() {
        for id in [
            "static-lint",
            "unit",
            "integration",
            "doc",
            "coverage",
            "crap",
            "property",
            "security-advisory",
        ] {
            for chain in chains(id) {
                for step in chain {
                    assert!(!step.display().contains("/tmp"), "{id}: {}", step.display());
                }
            }
        }
    }

    #[test]
    fn unmapped_layers_have_no_plan() {
        for id in [
            "fuzz",
            "miri",
            "sanitizer",
            "bench-criterion",
            "bench-callgrind",
            "density-audit",
            "laws",
        ] {
            assert!(
                plan_for(
                    &layer(id),
                    Path::new("/r"),
                    &Base::unused(),
                    &Filters::default()
                )
                .is_none(),
                "{id}"
            );
        }
    }

    #[test]
    fn formal_kani_runs_cargo_kani() {
        let kani = chains("formal-kani");
        assert_eq!(kani.len(), 1);
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let step = tool_step(&kani[0]);
        assert_eq!(step.argv, ["cargo", "kani", "--workspace"]);
        assert_eq!(
            steps::tool_binary(&step.argv).as_deref(),
            Some("cargo-kani")
        );
    }

    #[test]
    fn a_failed_diff_is_failed_even_over_a_stale_diff() {
        let dir = std::env::temp_dir().join(format!("cargo-tops-diff-{}", std::process::id()));
        fs::create_dir_all(&dir).expect("mkdir");
        let stale = dir.join("stale.diff");
        fs::write(&stale, "+changed\n").expect("write");
        let empty = dir.join("empty.diff");
        fs::write(&empty, "").expect("write");
        assert_eq!(steps::judge_diff(false, &stale), Outcome::Failed);
        assert_eq!(steps::judge_diff(false, &empty), Outcome::Failed);
        assert_eq!(steps::judge_diff(true, &empty), Outcome::Vacuous);
        assert_eq!(steps::judge_diff(true, &stale), Outcome::Passed);
        assert_eq!(
            steps::judge_diff(true, &dir.join("absent")),
            Outcome::Failed
        );
        #[expect(
            clippy::let_underscore_must_use,
            reason = "already in this crate when the strict table was turned on"
        )]
        let _ = fs::remove_dir_all(&dir);
        assert!(!Outcome::Vacuous.fails_the_gate());
        assert!(!Outcome::Vacuous.counts_for_a_law());
    }

    #[test]
    fn only_a_fault_the_run_already_printed_is_announced() {
        let said = super::Fault::announced("said".to_string());
        assert!(said.announced && !said.config);
        assert!(!super::Fault::env(anyhow::anyhow!("x")).announced);
        assert!(!super::Fault::config(anyhow::anyhow!("x")).announced);
    }
}