targeted-mutation 0.10.0

Targeted mutation testing for Rust: find the wrong implementations your tests let through
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
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
use std::path::PathBuf;
use std::process::ExitCode;

use clap::Parser;
use targeted_mutation::{driver, report};

const HEADING_TARGET: &str = "Target & Scope";
const HEADING_MUTATION: &str = "Mutation Mode";
const HEADING_TESTS: &str = "Test Execution & Selection";
const HEADING_OUTPUT: &str = "Output & Baselines";
const HEADING_BUILD: &str = "Performance & Cargo Build";
const HEADING_WORK: &str = "Work Directory & Fixtures";

const SHORT_EXAMPLES: &str = "\
Examples:
  cargo tmt                               Analyse the current package
  cargo tmt --since main                  Analyse changes since `main`
  cargo tmt --rebuild                     Also build source-only mutants and witnesses
  cargo tmt --format sarif > tmt.sarif    Write SARIF 2.1.0 output

Run `cargo tmt --help` for detailed option descriptions and exit codes.";

const LONG_EXAMPLES: &str = "\
Examples:
  cargo tmt                               Analyse the current package
  cargo tmt --since main                  Analyse changes since `main`
  cargo tmt --rebuild                     Also build source-only mutants and witnesses
  cargo tmt --workspace --format jsonl    Stream one JSON record per workspace member
  cargo tmt --format sarif > tmt.sarif    Write SARIF 2.1.0 for code scanning
  cargo tmt --source-only --shard 0/4     Run one shard in CI

Exit codes:
  0  No gaps found
  1  Analysis or CLI error (invalid arguments or baseline tests failed)
  2  Test gaps, unstable predictions, or audit discrepancies found
  3  Incomplete evaluation (unconfirmed timeouts, no mutants executed, or source-only timeout/unevaluated)

Report counts (JSON):
  mutants_run = mutants_killed + mutants_survived
  mutants_timed_out is a subset of mutants_killed (mutants_timed_out <= mutants_killed)
  mutants_survived = survivor_breakdown.{reported + suppressed + redundant_constant + preexisting_unchanged + unchanged_not_compared + unconfirmed_gap}
  evaluation is \"partial\" when mutants_not_built, mutants_not_evaluated, skipped_sites, unparseable_source, unchanged_not_compared, unconfirmed timeouts, or source-only timeouts remain";

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, clap::ValueEnum)]
enum OutputFormat {
    /// Human-readable terminal report.
    #[default]
    Text,
    /// Pretty-printed JSON report.
    Json,
    /// One compact JSON record per completed package (streamed).
    Jsonl,
    /// SARIF 2.1.0 for code scanning tools.
    Sarif,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, clap::ValueEnum)]
enum Sharding {
    Slice,
    RoundRobin,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, clap::ValueEnum)]
enum TimeoutRecheck {
    Serial,
    None,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, clap::ValueEnum)]
enum TestTool {
    #[default]
    Cargo,
    Nextest,
}

/// Targeted mutation testing: finds the wrong implementations your tests would let
/// through, and says which input or assertion would catch them.
#[derive(Parser)]
#[command(
    name = "cargo-tmt",
    bin_name = "cargo tmt",
    version,
    after_help = SHORT_EXAMPLES,
    after_long_help = LONG_EXAMPLES
)]
struct Cli {
    // ── Target & Scope ────────────────────────────────────────────────────────
    /// Directory of the Cargo package or workspace to analyse.
    #[arg(default_value = ".")]
    dir: PathBuf,

    /// Directory of the Cargo package or workspace to analyse.
    #[arg(
        short = 'd',
        long = "dir",
        value_name = "DIR",
        conflicts_with = "dir",
        help_heading = HEADING_TARGET
    )]
    directory: Option<PathBuf>,

    /// Workspace member to analyse (repeatable or comma-separated).
    #[arg(
        short,
        long,
        value_name = "PACKAGE",
        value_delimiter = ',',
        conflicts_with = "workspace",
        help_heading = HEADING_TARGET
    )]
    package: Vec<String>,

    /// Analyse all workspace members.
    #[arg(long, help_heading = HEADING_TARGET)]
    workspace: bool,

    /// Analyse only code changed since this Git revision, plus untracked files.
    #[arg(long, value_name = "REV", help_heading = HEADING_TARGET)]
    since: Option<String>,

    /// Analyse only these lines: FILE:FROM-TO, FILE:LINE, or FILE (repeatable).
    ///
    /// Paths are relative to each analysed package.
    #[arg(long, value_name = "FILE:FROM-TO", help_heading = HEADING_TARGET)]
    lines: Vec<String>,

    // ── Mutation Mode ─────────────────────────────────────────────────────────
    /// Also build source-only mutants and verify distinguishing witnesses (slower).
    ///
    /// Runs mutants that cannot be expressed as runtime probes (such as `const fn`,
    /// `impl Iterator` bodies, and shared-target modules) and synthesizes concrete
    /// distinguishing inputs or assertions for supported survivors.
    #[arg(long, help_heading = HEADING_MUTATION)]
    rebuild: bool,

    /// Build every supported mutant directly from source instead of using probes.
    ///
    /// Useful when tests spawn children that clear environment variables. Slower than
    /// probes and omits trace-based input or assertion suggestions.
    #[arg(
        long,
        conflicts_with_all = ["rebuild", "audit", "no_verify"],
        help_heading = HEADING_MUTATION
    )]
    source_only: bool,

    /// Preview source mutation candidates without building or running tests.
    #[arg(
        long,
        conflicts_with_all = ["sarif", "baseline", "write_baseline"],
        help_heading = HEADING_MUTATION
    )]
    list: bool,

    /// Also replace `Result` returns with `Err(EXPR)` (repeatable).
    ///
    /// EXPR is Rust code evaluated in the function's scope (e.g. `"mutated".into()`).
    #[arg(
        long = "error",
        value_name = "EXPR",
        allow_hyphen_values = true,
        help_heading = HEADING_MUTATION
    )]
    error_values: Vec<String>,

    /// Replace only the operator token without parenthesizing operands (`--source-only`).
    #[arg(long, requires = "source_only", help_heading = HEADING_MUTATION)]
    literal_operators: bool,

    /// Select zero-based source-only shard K/N (`--source-only`).
    #[arg(
        long,
        requires = "source_only",
        value_name = "K/N",
        help_heading = HEADING_MUTATION
    )]
    shard: Option<report::Shard>,

    /// Method for distributing candidates across shards (default: slice).
    #[arg(
        long,
        requires = "shard",
        value_enum,
        value_name = "METHOD",
        help_heading = HEADING_MUTATION
    )]
    sharding: Option<Sharding>,

    /// Trust the trace without re-running tests against predicted survivors.
    #[arg(long, help_heading = HEADING_MUTATION)]
    no_verify: bool,

    /// Skip effect tracing on surviving mutants.
    #[arg(long, help_heading = HEADING_MUTATION)]
    no_effects: bool,

    /// Audit up to N survivors and probe mutants against full test runs.
    ///
    /// Re-runs up to N surviving mutants against every test binary, compares up to N
    /// probe mutants against fresh source builds, and records tests a second time to
    /// detect nondeterminism. Discrepancies exit with code 2.
    #[arg(
        long,
        value_name = "N",
        conflicts_with = "no_verify",
        help_heading = HEADING_MUTATION
    )]
    audit: Option<usize>,

    // ── Test Execution & Selection ────────────────────────────────────────────
    /// Also run tests from this workspace package (repeatable).
    #[arg(long, value_name = "PACKAGE", help_heading = HEADING_TESTS)]
    also_test: Vec<String>,

    /// Also run tests of workspace packages depending on this package.
    #[arg(long, help_heading = HEADING_TESTS)]
    dependents: bool,

    /// Run tests from all workspace members against every mutation.
    #[arg(
        long,
        value_name = "BOOL",
        num_args = 0..=1,
        require_equals = true,
        default_missing_value = "true",
        help_heading = HEADING_TESTS
    )]
    test_workspace: Option<bool>,

    /// Also run doc tests (recompiled per reached mutant; slower).
    #[arg(long, help_heading = HEADING_TESTS)]
    doc: bool,

    /// Test runner (`nextest` requires `--source-only`).
    #[arg(
        long,
        value_enum,
        value_name = "TOOL",
        default_value = "cargo",
        help_heading = HEADING_TESTS
    )]
    test_tool: TestTool,

    /// Nextest profile for `--test-tool nextest`.
    #[arg(long, value_name = "PROFILE", help_heading = HEADING_TESTS)]
    nextest_profile: Option<String>,

    /// Run each selected libtest test in its own process.
    #[arg(long, help_heading = HEADING_TESTS)]
    isolate_tests: bool,

    /// Test only the package's library target.
    #[arg(long, help_heading = HEADING_TESTS)]
    lib: bool,

    /// Test all binary targets.
    #[arg(long, help_heading = HEADING_TESTS)]
    bins: bool,

    /// Test only the specified integration test target (repeatable).
    #[arg(long = "test", value_name = "NAME", help_heading = HEADING_TESTS)]
    integration_test: Vec<String>,

    /// Cargo test targets to run and build when possible (repeatable).
    #[arg(long, value_name = "TARGET", help_heading = HEADING_TESTS)]
    test_target: Vec<String>,

    /// Include libtest tests whose name contains PATTERN (repeatable).
    #[arg(long, value_name = "PATTERN", help_heading = HEADING_TESTS)]
    test_filter: Vec<String>,

    /// Include exact libtest test names (repeatable).
    #[arg(long, value_name = "NAME", help_heading = HEADING_TESTS)]
    include_test: Vec<String>,

    /// Exclude exact libtest test names (repeatable).
    #[arg(long, value_name = "NAME", help_heading = HEADING_TESTS)]
    exclude_test: Vec<String>,

    /// Also run tests marked `#[ignore]`.
    #[arg(long, help_heading = HEADING_TESTS)]
    include_ignored: bool,

    /// Workspace-relative wrapper invoked with each test binary.
    #[arg(long, value_name = "PATH", help_heading = HEADING_TESTS)]
    test_wrapper: Option<PathBuf>,

    /// Grace period for `--test-wrapper` on Unix timeout (0 kills immediately).
    #[arg(
        long,
        value_name = "SECONDS",
        default_value_t = 2,
        requires = "test_wrapper",
        help_heading = HEADING_TESTS
    )]
    wrapper_grace: u64,

    // ── Output & Baselines ────────────────────────────────────────────────────
    /// Output format (default: text).
    #[arg(
        long,
        value_enum,
        value_name = "FORMAT",
        help_heading = HEADING_OUTPUT
    )]
    format: Option<OutputFormat>,

    /// Also list mutation sites that no test executed.
    #[arg(long, help_heading = HEADING_OUTPUT)]
    show_unreached: bool,

    /// Suppress progress output on stderr.
    #[arg(short, long, help_heading = HEADING_OUTPUT)]
    quiet: bool,

    /// Suppress findings already recorded in this baseline file.
    #[arg(long, value_name = "FILE", help_heading = HEADING_OUTPUT)]
    baseline: Option<PathBuf>,

    /// Save this run's findings to a baseline file for future `--baseline` runs.
    #[arg(long, value_name = "FILE", help_heading = HEADING_OUTPUT)]
    write_baseline: Option<PathBuf>,

    /// Shorthand for `--format json`.
    #[arg(
        long,
        conflicts_with_all = ["format", "jsonl", "sarif"],
        help_heading = HEADING_OUTPUT
    )]
    json: bool,

    /// Shorthand for `--format jsonl`.
    #[arg(
        long,
        conflicts_with_all = ["format", "json", "sarif"],
        help_heading = HEADING_OUTPUT
    )]
    jsonl: bool,

    /// Shorthand for `--format sarif`.
    #[arg(
        long,
        conflicts_with_all = ["format", "json", "jsonl"],
        help_heading = HEADING_OUTPUT
    )]
    sarif: bool,

    // ── Performance & Cargo Build ─────────────────────────────────────────────
    /// Mutants to run in parallel.
    ///
    /// Raise this only when tests can run concurrently with themselves (no fixed ports
    /// or shared files).
    #[arg(
        short,
        long,
        default_value_t = 1,
        value_name = "N",
        value_parser = clap::value_parser!(u32).range(1..),
        help_heading = HEADING_BUILD
    )]
    jobs: u32,

    /// Concurrent isolated baseline tests.
    #[arg(
        long,
        value_name = "N",
        default_value_t = 1,
        value_parser = clap::value_parser!(u32).range(1..),
        help_heading = HEADING_BUILD
    )]
    baseline_jobs: u32,

    /// Timeout per test runner invocation under a mutant, in seconds.
    #[arg(
        long,
        value_name = "SECONDS",
        value_parser = clap::value_parser!(u64).range(1..),
        help_heading = HEADING_BUILD
    )]
    timeout: Option<u64>,

    /// Policy for rechecking parallel worker timeouts.
    #[arg(
        long,
        value_enum,
        value_name = "POLICY",
        default_value = "serial",
        help_heading = HEADING_BUILD
    )]
    timeout_recheck: TimeoutRecheck,

    /// Save and resume completed mutant outcomes via `checkpoint.jsonl`.
    #[arg(long, conflicts_with = "list", help_heading = HEADING_BUILD)]
    checkpoint: bool,

    /// Override Cargo optimisation level for test builds (0 to 3).
    ///
    /// Applies to both probe and `--source-only` builds. Probes have near-zero overhead
    /// when optimised, which helps loop-heavy tests, at the cost of slower builds and
    /// tests differing from plain `cargo test`.
    #[arg(
        long,
        value_name = "N",
        value_parser = clap::value_parser!(u8).range(0..=3),
        help_heading = HEADING_BUILD
    )]
    opt_level: Option<u8>,

    /// Cargo features to enable, comma-separated (repeatable).
    #[arg(
        long,
        short = 'F',
        value_name = "FEATURES",
        value_delimiter = ',',
        help_heading = HEADING_BUILD
    )]
    features: Vec<String>,

    /// Build with all Cargo features enabled.
    #[arg(
        long,
        conflicts_with_all = ["features", "no_default_features"],
        help_heading = HEADING_BUILD
    )]
    all_features: bool,

    /// Build without default Cargo features.
    #[arg(long, help_heading = HEADING_BUILD)]
    no_default_features: bool,

    /// Pass `--locked` to Cargo so `Cargo.lock` stays unchanged.
    #[arg(long, help_heading = HEADING_BUILD)]
    locked: bool,

    // ── Work Directory & Fixtures ─────────────────────────────────────────────
    /// Directory for the isolated working copy and build (default: `<target-dir>/tmt`).
    #[arg(long, value_name = "DIR", help_heading = HEADING_WORK)]
    work_dir: Option<PathBuf>,

    /// Also copy gitignored files into the working copy.
    ///
    /// Needed when the build reads generated, untracked files. Git metadata,
    /// `node_modules`, and build caches remain excluded by default.
    #[arg(long, help_heading = HEADING_WORK)]
    all_files: bool,

    /// Create an independent Git index in the working copy for commands like `git ls-files`.
    #[arg(long, help_heading = HEADING_WORK)]
    git_index: bool,

    /// Copy a workspace-relative path even if gitignored (repeatable).
    #[arg(long, value_name = "PATH", help_heading = HEADING_WORK)]
    copy_fixture: Vec<PathBuf>,

    /// Exclude a workspace-relative path from `--copy-fixture` (repeatable).
    #[arg(
        long,
        value_name = "PATH",
        requires = "copy_fixture",
        help_heading = HEADING_WORK
    )]
    copy_fixture_exclude: Vec<PathBuf>,

    /// Stage workspace path FROM at TO relative to isolated Cargo output (repeatable).
    #[arg(long, value_name = "FROM=TO", help_heading = HEADING_WORK)]
    copy_output_fixture: Vec<targeted_mutation::output_fixture::Fixture>,

    /// Stage an external parent-relative path into the isolated work copy (repeatable).
    ///
    /// Accepts either `PATH` (e.g. `../../proto`) or `SRC=REL` (e.g. `/abs/proto=../../proto`),
    /// where `REL` ascends above the workspace via `..`.
    #[arg(long, value_name = "SRC[=REL]", help_heading = HEADING_WORK)]
    stage_external: Vec<driver::StageExternal>,
}

impl Cli {
    fn output_format(&self) -> OutputFormat {
        if let Some(format) = self.format {
            format
        } else if self.json {
            OutputFormat::Json
        } else if self.jsonl {
            OutputFormat::Jsonl
        } else if self.sarif {
            OutputFormat::Sarif
        } else {
            OutputFormat::Text
        }
    }

    fn validate(&self) -> Result<(), clap::Error> {
        use clap::{CommandFactory, error::ErrorKind};

        if !matches!(self.test_tool, TestTool::Nextest) && self.nextest_profile.is_some() {
            return Err(Cli::command().error(
                ErrorKind::MissingRequiredArgument,
                "--nextest-profile requires --test-tool nextest",
            ));
        }
        if self.list && self.output_format() == OutputFormat::Sarif {
            return Err(Cli::command().error(
                ErrorKind::ArgumentConflict,
                "the argument '--list' cannot be used with '--format sarif'",
            ));
        }
        Ok(())
    }
}

/// Parses and validates CLI arguments.
///
/// Help and version requests print to stdout and return `Err(ExitCode::SUCCESS)`.
/// All argument parse and validation errors print to stderr and return
/// `Err(ExitCode::FAILURE)` (exit code 1) so they are never confused with exit code 2
/// (test gaps found).
fn parse_cli(given: impl IntoIterator<Item = std::ffi::OsString>) -> Result<Cli, ExitCode> {
    let cli = match Cli::try_parse_from(arguments(given)) {
        Ok(cli) => cli,
        Err(err) => {
            if err.use_stderr() {
                eprint!("{}", report::sanitize_terminal(&err.render().to_string()));
                return Err(ExitCode::FAILURE);
            }
            let _ = err.print();
            return Err(ExitCode::SUCCESS);
        }
    };
    if let Err(err) = cli.validate() {
        eprint!("{}", report::sanitize_terminal(&err.render().to_string()));
        return Err(ExitCode::FAILURE);
    }
    Ok(cli)
}

/// Exit codes: 0 no gaps, 1 analysis/CLI error, 2 gaps found, 3 incomplete evaluation.
fn main() -> ExitCode {
    let cli = match parse_cli(std::env::args_os()) {
        Ok(cli) => cli,
        Err(code) => return code,
    };
    let output_format = cli.output_format();
    let nextest = matches!(cli.test_tool, TestTool::Nextest);
    let options = driver::Options {
        dir: cli.directory.unwrap_or(cli.dir),
        package: cli.package.first().cloned(),
        list: cli.list,
        test_workspace: cli.test_workspace.unwrap_or(false),
        verify: !cli.no_verify,
        work_dir: cli.work_dir,
        progress: !cli.quiet,
        since: cli.since,
        lines: cli.lines,
        baseline: cli.baseline,
        write_baseline: cli.write_baseline,
        also_test: cli.also_test,
        dependents: cli.dependents,
        effects: !cli.no_effects,
        rebuild: cli.rebuild,
        error_values: cli.error_values,
        source_only: cli.source_only,
        literal_operators: cli.literal_operators,
        shard: cli.shard.map(|mut shard| {
            shard.round_robin = matches!(cli.sharding, Some(Sharding::RoundRobin));
            shard
        }),
        nextest: nextest.then_some(report::Nextest {
            profile: cli.nextest_profile,
        }),
        test_selection: driver::TestSelection {
            isolate: cli.isolate_tests,
            lib: cli.lib,
            bins: cli.bins,
            integration_tests: cli.integration_test,
            targets: cli.test_target,
            include: cli.include_test,
            filter: cli.test_filter,
            exclude: cli.exclude_test,
            include_ignored: cli.include_ignored,
            wrapper: cli.test_wrapper,
        },
        opt_level: cli.opt_level,
        timeout_seconds: cli.timeout,
        baseline_jobs: cli.baseline_jobs as usize,
        serial_timeout_rechecks: matches!(cli.timeout_recheck, TimeoutRecheck::Serial),
        wrapper_grace: std::time::Duration::from_secs(cli.wrapper_grace),
        audit: cli.audit,
        cargo: driver::CargoConfig {
            features: cli.features,
            all_features: cli.all_features,
            no_default_features: cli.no_default_features,
            locked: cli.locked,
        },
        jobs: cli.jobs as usize,
        all_files: cli.all_files,
        git_index: cli.git_index,
        copy_fixtures: cli.copy_fixture,
        copy_fixture_excludes: cli.copy_fixture_exclude,
        output_fixtures: cli.copy_output_fixture,
        stage_external: cli.stage_external,
        checkpoint: cli.checkpoint,
        doc: cli.doc,
    };
    if output_format == OutputFormat::Jsonl {
        return stream_reports(
            &options,
            &cli.package,
            cli.workspace || cli.package.len() > 1,
        );
    }
    if cli.workspace || cli.package.len() > 1 {
        let members = match driver::run_packages(&options, &cli.package) {
            Ok(members) => members,
            Err(err) => {
                eprintln!(
                    "tmt: error: {}",
                    report::sanitize_terminal(&format!("{err:#}"))
                );
                return ExitCode::FAILURE;
            }
        };
        return show_workspace(members, output_format, cli.show_unreached);
    }
    match driver::run(&options) {
        Ok(result) => {
            show_report(&result, output_format, cli.show_unreached);
            ExitCode::from(report_exit(&result))
        }
        Err(err) => {
            eprintln!(
                "tmt: error: {}",
                report::sanitize_terminal(&format!("{err:#}"))
            );
            ExitCode::FAILURE
        }
    }
}

fn incomplete_source_run(result: &report::Report) -> bool {
    result.timeout_rechecks.unconfirmed > 0
        || (result.analysis.source_only
            && (result.mutants_timed_out > 0 || result.mutants_not_evaluated > 0))
}

fn report_exit(result: &report::Report) -> u8 {
    if incomplete_source_run(result) {
        3
    } else if result.has_gaps() {
        2
    } else if result.evaluation == Some(report::Evaluation::NoMutantsExecuted) {
        3
    } else {
        0
    }
}

fn report_json(result: &report::Report) -> serde_json::Value {
    if let Some(mutants) = &result.mutants {
        serde_json::json!({
            "schema_version": report::SCHEMA_VERSION,
            "package": result.package,
            "analysis": result.analysis,
            "mutants": mutants,
            "skipped": result.skipped,
        })
    } else {
        serde_json::to_value(result).expect("report serialises")
    }
}

fn show_report(result: &report::Report, format: OutputFormat, show_unreached: bool) {
    match format {
        OutputFormat::Sarif => println!("{}", report::render_sarif(result)),
        OutputFormat::Json => {
            if result.mutants.is_some() {
                println!("{:#}", report_json(result));
            } else {
                println!(
                    "{}",
                    serde_json::to_string_pretty(result).expect("report serialises")
                );
            }
        }
        OutputFormat::Text => {
            if let Some(mutants) = &result.mutants {
                println!(
                    "{}: {} source candidates",
                    report::sanitize_inline(&result.package),
                    mutants.len()
                );
                for candidate in mutants {
                    let edit = &candidate.edits[0];
                    println!(
                        "{}:{}:{}: {} -> {}",
                        report::sanitize_inline(&edit.file),
                        edit.line,
                        edit.column,
                        report::sanitize_inline(&candidate.what),
                        report::sanitize_inline(&candidate.into)
                    );
                }
            } else {
                print!("{}", report::render_text(result, show_unreached));
            }
        }
        OutputFormat::Jsonl => unreachable!("jsonl output is streamed by stream_reports"),
    }
}

fn stream_reports(options: &driver::Options, packages: &[String], workspace: bool) -> ExitCode {
    use std::io::Write as _;
    let mut stdout = std::io::stdout().lock();
    let mut status = (0, 0);
    let mut emit = |name: String, result: anyhow::Result<report::Report>| -> anyhow::Result<()> {
        let (record, rank) = match result {
            Ok(result) => {
                let code = report_exit(&result);
                let priority = if incomplete_source_run(&result) {
                    3
                } else {
                    match code {
                        2 => 2,
                        3 => 1,
                        _ => 0,
                    }
                };
                let mut record = serde_json::json!({
                    "schema_version": report::SCHEMA_VERSION,
                    "package": result.package,
                });
                record["report"] = report_json(&result);
                (record, (priority, code))
            }
            Err(err) => (
                serde_json::json!({
                    "schema_version": report::SCHEMA_VERSION,
                    "package": name,
                    "error": format!("{err:#}"),
                }),
                (4, 1),
            ),
        };
        status = status.max(rank);
        serde_json::to_writer(&mut stdout, &record)?;
        writeln!(stdout)?;
        stdout.flush()?;
        Ok(())
    };
    let result = if workspace {
        driver::run_packages_each(options, packages, &mut emit)
    } else {
        emit(
            options.package.clone().unwrap_or_default(),
            driver::run(options),
        )
    };
    if let Err(err) = result {
        eprintln!(
            "tmt: error: {}",
            report::sanitize_terminal(&format!("{err:#}"))
        );
        return ExitCode::FAILURE;
    }
    ExitCode::from(status.1)
}

fn show_workspace(
    members: Vec<(String, anyhow::Result<report::Report>)>,
    format: OutputFormat,
    show_unreached: bool,
) -> ExitCode {
    let mut reports = vec![];
    let mut errors = vec![];
    for (package, result) in members {
        match result {
            Ok(result) => reports.push(result),
            Err(err) => {
                let message = format!("{err:#}");
                eprintln!(
                    "tmt: {}: {}",
                    report::sanitize_inline(&package),
                    report::sanitize_terminal(&message)
                );
                errors.push(serde_json::json!({"package": package, "error": message}));
            }
        }
    }
    let has_errors = !errors.is_empty();
    match format {
        OutputFormat::Json => {
            let mut combined = serde_json::json!({
                "schema_version": report::SCHEMA_VERSION,
            });
            combined["packages"] =
                serde_json::Value::Array(reports.iter().map(report_json).collect());
            combined["errors"] = serde_json::Value::Array(errors);
            println!("{combined:#}");
        }
        OutputFormat::Sarif => {
            let failed: Vec<(String, String)> = errors
                .iter()
                .map(|error| {
                    let text = |key: &str| error[key].as_str().unwrap_or_default().to_string();
                    (text("package"), text("error"))
                })
                .collect();
            println!("{:#}", report::workspace_sarif(&reports, &failed));
        }
        OutputFormat::Text => {
            for result in &reports {
                show_report(result, OutputFormat::Text, show_unreached);
            }
        }
        OutputFormat::Jsonl => unreachable!("jsonl output is streamed by stream_reports"),
    }
    ExitCode::from(if has_errors {
        1
    } else if reports.iter().any(incomplete_source_run) {
        3
    } else if reports.iter().any(|report| report_exit(report) == 2) {
        2
    } else if reports.iter().any(|report| report_exit(report) == 3) {
        3
    } else {
        0
    })
}

/// The arguments as they were meant. Cargo starts `cargo tmt --json` as
/// `cargo-tmt tmt --json`: the name of the subcommand comes first and is not one of
/// the arguments. (A directory called `tmt` is given as `./tmt`.)
fn arguments(given: impl IntoIterator<Item = std::ffi::OsString>) -> Vec<std::ffi::OsString> {
    let mut all: Vec<std::ffi::OsString> = given.into_iter().collect();
    if all.get(1).is_some_and(|first| first == "tmt") {
        all.remove(1);
    }
    all
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::CommandFactory;

    fn given(args: &[&str]) -> Vec<String> {
        arguments(args.iter().map(std::ffi::OsString::from))
            .into_iter()
            .map(|arg| arg.into_string().unwrap())
            .collect()
    }

    #[test]
    fn the_name_of_the_subcommand_is_not_an_argument() {
        // As cargo starts it, and as it is started by its own name.
        assert_eq!(
            given(&["cargo-tmt", "tmt", ".", "--json"]),
            ["cargo-tmt", ".", "--json"]
        );
        assert_eq!(
            given(&["cargo-tmt", ".", "--json"]),
            ["cargo-tmt", ".", "--json"]
        );
        assert_eq!(given(&["cargo-tmt", "tmt"]), ["cargo-tmt"]);
        assert_eq!(given(&["cargo-tmt"]), ["cargo-tmt"]);
        // Only the first, and only in the first place.
        assert_eq!(given(&["cargo-tmt", "tmt", "tmt"]), ["cargo-tmt", "tmt"]);
        assert_eq!(given(&["cargo-tmt", "./tmt"]), ["cargo-tmt", "./tmt"]);
        assert_eq!(
            given(&["cargo-tmt", "-p", "tmt"]),
            ["cargo-tmt", "-p", "tmt"]
        );
        // And what is left is what the options are read from.
        let cli = Cli::parse_from(given(&["cargo-tmt", "tmt", "crate", "--jobs", "3"]));
        assert_eq!((cli.dir, cli.jobs), (PathBuf::from("crate"), 3));
        let cli = Cli::parse_from(given(&["cargo-tmt", "tmt"]));
        assert_eq!(cli.dir, PathBuf::from("."));
        assert!(Cli::try_parse_from(given(&["cargo-tmt", "tmt", "--jobs", "0"])).is_err());
    }

    #[test]
    fn output_format_resolves_unified_option_and_legacy_shorthands() {
        let default_cli = Cli::parse_from(given(&["cargo-tmt", "tmt"]));
        assert_eq!(default_cli.output_format(), OutputFormat::Text);
        assert!(default_cli.validate().is_ok());

        for (args, expected) in [
            (&["--format", "text"][..], OutputFormat::Text),
            (&["--format", "json"][..], OutputFormat::Json),
            (&["--format", "jsonl"][..], OutputFormat::Jsonl),
            (&["--format", "sarif"][..], OutputFormat::Sarif),
            (&["--json"][..], OutputFormat::Json),
            (&["--jsonl"][..], OutputFormat::Jsonl),
            (&["--sarif"][..], OutputFormat::Sarif),
            (&["--list", "--format", "json"][..], OutputFormat::Json),
            (&["--list", "--format", "jsonl"][..], OutputFormat::Jsonl),
        ] {
            let mut argv = vec!["cargo-tmt", "tmt"];
            argv.extend_from_slice(args);
            let cli = Cli::parse_from(given(&argv));
            assert_eq!(cli.output_format(), expected, "for {args:?}");
            assert!(cli.validate().is_ok(), "for {args:?}");
        }
    }

    #[test]
    fn parse_cli_exits_one_on_errors_and_zero_on_help_or_version() {
        for ok_exit in [
            &["-h"][..],
            &["--help"][..],
            &["-V"][..],
            &["--version"][..],
        ] {
            let mut argv = vec!["cargo-tmt", "tmt"];
            argv.extend_from_slice(ok_exit);
            assert_eq!(
                parse_cli(argv.iter().map(std::ffi::OsString::from)).err(),
                Some(ExitCode::SUCCESS),
                "expected exit 0 for {ok_exit:?}"
            );
        }

        // Every parse error, value-range error, and cross-option validation error exits 1
        // so CI never mistakes a CLI error for exit code 2 (test gaps found).
        for bad in [
            &["--format", "xml"][..],
            &["--bogus"][..],
            &["--format", "json", "--sarif"][..],
            &["--format", "text", "--json"][..],
            &["--format", "sarif", "--jsonl"][..],
            &["--json", "--jsonl"][..],
            &["--json", "--sarif"][..],
            &["--list", "--sarif"][..],
            &["--list", "--format", "sarif"][..],
            &["--nextest-profile", "ci"][..],
            &["--jobs", "0"][..],
            &["--baseline-jobs", "0"][..],
            &["--timeout", "0"][..],
            &["--opt-level", "4"][..],
        ] {
            let mut argv = vec!["cargo-tmt", "tmt"];
            argv.extend_from_slice(bad);
            assert_eq!(
                parse_cli(argv.iter().map(std::ffi::OsString::from)).err(),
                Some(ExitCode::FAILURE),
                "expected exit 1 for {bad:?}"
            );
        }

        let valid_nextest = parse_cli(
            [
                "cargo-tmt",
                "tmt",
                "--source-only",
                "--test-tool",
                "nextest",
                "--nextest-profile",
                "ci",
            ]
            .iter()
            .map(std::ffi::OsString::from),
        );
        assert!(valid_nextest.is_ok());
    }

    #[test]
    fn all_legacy_long_flags_remain_available() {
        let cmd = Cli::command();
        let longs: std::collections::BTreeSet<&str> = cmd
            .get_arguments()
            .filter_map(|arg| arg.get_long())
            .collect();
        for flag in [
            "dir",
            "package",
            "workspace",
            "list",
            "test-workspace",
            "json",
            "jsonl",
            "no-verify",
            "work-dir",
            "doc",
            "all-files",
            "git-index",
            "copy-fixture",
            "copy-fixture-exclude",
            "copy-output-fixture",
            "checkpoint",
            "jobs",
            "since",
            "lines",
            "baseline",
            "write-baseline",
            "also-test",
            "dependents",
            "no-effects",
            "rebuild",
            "error",
            "source-only",
            "literal-operators",
            "shard",
            "sharding",
            "test-tool",
            "nextest-profile",
            "isolate-tests",
            "test-target",
            "include-test",
            "exclude-test",
            "include-ignored",
            "test-wrapper",
            "opt-level",
            "timeout",
            "baseline-jobs",
            "timeout-recheck",
            "wrapper-grace",
            "features",
            "all-features",
            "no-default-features",
            "locked",
            "audit",
            "sarif",
            "show-unreached",
            "quiet",
            "format",
        ] {
            assert!(longs.contains(flag), "missing flag --{flag}");
        }
    }

    #[test]
    fn cli_definition_and_grouped_help_are_valid() {
        let mut cmd = Cli::command();
        cmd.clone().debug_assert();

        let long_help = cmd.render_long_help().to_string();
        for heading in [
            HEADING_TARGET,
            HEADING_MUTATION,
            HEADING_TESTS,
            HEADING_OUTPUT,
            HEADING_BUILD,
            HEADING_WORK,
        ] {
            assert!(
                long_help.contains(heading),
                "missing heading `{heading}` in --help"
            );
        }
        assert!(long_help.contains("--format <FORMAT>"));
        assert!(long_help.contains("Examples:"));
        assert!(long_help.contains("Exit codes:"));
    }

    #[test]
    fn report_exit_returns_two_on_audit_discrepancies() {
        let mut rep = report::Report {
            package: "demo".to_string(),
            ..Default::default()
        };
        assert_eq!(report_exit(&rep), 0);
        rep.audit = Some(report::Audit {
            sampled: 1,
            survivors: 1,
            not_audited: 0,
            disagreements: vec![],
            rebuilt: 0,
            compared: 0,
            not_compared: 0,
            source_disagreements: vec![],
            trace_differences: vec![report::TraceDifference {
                binary: "demo".to_string(),
                records: 0,
                rerun_failed: true,
            }],
        });
        assert_eq!(report_exit(&rep), 2);
        rep.timeout_rechecks.unconfirmed = 1;
        assert_eq!(report_exit(&rep), 3);
    }

    #[test]
    fn report_exit_pict_table() {
        let table = include_str!("../tests/data/report_evaluation.pict.tsv");
        let mut lines = table.lines();
        let _header = lines.next().expect("header");
        let dummy = || report::Location {
            file: "src/lib.rs".into(),
            line: 1,
            column: 1,
            code: "x".into(),
            ..report::Location::default()
        };
        for line in lines.filter(|line| !line.trim().is_empty()) {
            let cols: Vec<&str> = line.split('\t').collect();
            assert_eq!(cols.len(), 11, "row: {line}");
            let skipped = if cols[4] == "true" {
                vec![report::SkippedSite {
                    at: dummy(),
                    end_line: 1,
                    end_column: 2,
                    reason: report::SkipReason::UnparseableSource,
                    diagnostic: Some("parse error".into()),
                }]
            } else if cols[2] == "nonempty" {
                vec![report::SkippedSite {
                    at: dummy(),
                    end_line: 1,
                    end_column: 2,
                    reason: report::SkipReason::RequiresSourceRebuild,
                    diagnostic: None,
                }]
            } else {
                vec![]
            };
            let source_only_timeout = cols[8] == "true";
            let mut rep = report::Report {
                sites: usize::from(cols[0] == "nonzero"),
                mutants_run: usize::from(cols[1] == "nonzero"),
                mutants_killed: usize::from(cols[1] == "nonzero"),
                mutants_timed_out: usize::from(source_only_timeout),
                skipped,
                skipped_sites: usize::from(cols[3] == "nonzero"),
                mutants_not_built: usize::from(cols[5] == "nonzero"),
                mutants_not_evaluated: usize::from(cols[6] == "nonzero"),
                timeout_rechecks: report::TimeoutRechecks {
                    first_pass: usize::from(cols[7] == "nonzero"),
                    unconfirmed: usize::from(cols[7] == "nonzero"),
                    ..report::TimeoutRechecks::default()
                },
                unstable: if cols[9] == "true" {
                    vec![dummy()]
                } else {
                    vec![]
                },
                analysis: report::Analysis {
                    source_only: cols[10] == "true",
                    ..report::Analysis::default()
                },
                ..report::Report::default()
            };
            rep.evaluation = match cols[1] {
                "zero" => {
                    if cols[0] == "zero"
                        && cols[2] == "empty"
                        && cols[5] == "zero"
                        && cols[6] == "zero"
                    {
                        Some(report::Evaluation::NoSites)
                    } else {
                        Some(report::Evaluation::NoMutantsExecuted)
                    }
                }
                "nonzero" => {
                    if cols[3] == "nonzero"
                        || cols[4] == "true"
                        || cols[5] == "nonzero"
                        || cols[6] == "nonzero"
                        || cols[7] == "nonzero"
                        || source_only_timeout
                    {
                        Some(report::Evaluation::Partial)
                    } else {
                        None
                    }
                }
                other => panic!("unexpected mutants_run value: {other}"),
            };
            let expected = if cols[7] == "nonzero"
                || (cols[10] == "true" && (source_only_timeout || cols[6] == "nonzero"))
            {
                3
            } else if cols[9] == "true" {
                2
            } else if rep.evaluation == Some(report::Evaluation::NoMutantsExecuted) {
                3
            } else {
                0
            };
            assert_eq!(report_exit(&rep), expected, "row: {line}");
        }
    }
}