targeted-mutation 0.6.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
use std::path::PathBuf;
use std::process::ExitCode;

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

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

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

#[derive(Clone, Copy, Default, 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)]
struct Cli {
    /// Directory of the cargo package to analyse.
    #[arg(default_value = ".")]
    dir: PathBuf,
    /// Directory of the Cargo package or workspace to analyse.
    #[arg(short = 'd', long = "dir", conflicts_with = "dir")]
    directory: Option<PathBuf>,
    /// Package to analyse, when the directory is a workspace (repeatable).
    #[arg(short, long, value_delimiter = ',', conflicts_with = "workspace")]
    package: Vec<String>,
    /// Analyse all workspace members.
    #[arg(long)]
    workspace: bool,
    /// List source candidates and their edits without building or running tests.
    #[arg(long, conflicts_with_all = ["sarif", "baseline", "write_baseline"])]
    list: bool,
    /// Run the whole workspace's tests against every mutation.
    #[arg(long, num_args = 0..=1, require_equals = true, default_missing_value = "true")]
    test_workspace: Option<bool>,
    /// Print the report as JSON.
    #[arg(long)]
    json: bool,
    /// Emit one compact JSON report or error per completed package.
    #[arg(long, conflicts_with_all = ["json", "sarif"])]
    jsonl: bool,
    /// Trust the trace: do not re-run tests against predicted survivors.
    #[arg(long)]
    no_verify: bool,
    /// Directory for the working copy and its build (default: <target dir>/tmt).
    #[arg(long, value_name = "DIR")]
    work_dir: Option<PathBuf>,
    /// Also run the doc tests. They are recompiled for every mutant they reach.
    #[arg(long)]
    doc: bool,
    /// Copy files that git ignores as well (needed when the build reads generated,
    /// untracked files). By default they are left out of the working copy.
    #[arg(long)]
    all_files: bool,
    /// Reproduce the source Git index in an independent repository in the work copy.
    #[arg(long)]
    git_index: bool,
    /// Copy a workspace-relative file or directory even when Git ignores it (repeatable).
    #[arg(long, value_name = "PATH")]
    copy_fixture: Vec<PathBuf>,
    /// Omit this workspace-relative entry from an explicitly copied fixture (repeatable).
    #[arg(long, value_name = "PATH", requires = "copy_fixture")]
    copy_fixture_exclude: Vec<PathBuf>,
    /// Stage workspace fixture FROM at path TO relative to the isolated Cargo output (repeatable).
    #[arg(long, value_name = "FROM=TO")]
    copy_output_fixture: Vec<targeted_mutation::output_fixture::Fixture>,
    /// Save and reuse completed mutant outcomes in checkpoint.jsonl under the work directory.
    #[arg(long, conflicts_with = "list")]
    checkpoint: bool,
    /// Mutants to run at the same time. Only raise it if the tests can run concurrently
    /// with themselves (no fixed ports, no shared files).
    #[arg(short, long, default_value_t = 1, value_name = "N")]
    jobs: usize,
    /// Analyse only the code changed since this git revision, and untracked files.
    #[arg(long, value_name = "REV")]
    since: Option<String>,
    /// Analyse only these lines: FILE:FROM-TO, FILE:LINE or FILE (relative to the package).
    #[arg(long, value_name = "FILE:FROM-TO")]
    lines: Vec<String>,
    /// Do not report the findings listed in this file.
    #[arg(long, value_name = "FILE")]
    baseline: Option<PathBuf>,
    /// Write this run's findings to a file that later runs can use as --baseline.
    #[arg(long, value_name = "FILE")]
    write_baseline: Option<PathBuf>,
    /// Also run the tests of this workspace package (repeatable).
    #[arg(long, value_name = "PACKAGE")]
    also_test: Vec<String>,
    /// Also run the tests of every workspace package that depends on the analysed one.
    #[arg(long)]
    dependents: bool,
    /// Do not trace surviving mutants to say what they change.
    #[arg(long)]
    no_effects: bool,
    /// Also run mutants that need a build of their own and verify distinguishing inputs
    /// for supported survivors. Slower than probes alone.
    #[arg(long)]
    rebuild: bool,
    /// Also replace Result returns with Err(EXPR), using this Rust expression (repeatable).
    #[arg(long = "error", value_name = "EXPR", allow_hyphen_values = true)]
    error_values: Vec<String>,
    /// Build every supported mutant in the selected source instead of using probes.
    #[arg(long, conflicts_with_all = ["rebuild", "audit", "no_verify"])]
    source_only: bool,
    /// Replace just the operator token, using Rust precedence for the new spelling.
    #[arg(long, requires = "source_only")]
    literal_operators: bool,
    /// Select a zero-based source-only shard K/N.
    #[arg(long, requires = "source_only", value_name = "K/N")]
    shard: Option<report::Shard>,
    /// Method for distributing candidates across shards (default: slice).
    #[arg(long, requires = "shard", value_enum)]
    sharding: Option<Sharding>,
    /// Select the runner (nextest requires --source-only).
    #[arg(long, value_enum, default_value = "cargo")]
    test_tool: TestTool,
    /// Nextest profile to use with --test-tool nextest.
    #[arg(long, value_name = "PROFILE")]
    nextest_profile: Option<String>,
    /// Run one selected libtest test per process, including its child threads.
    #[arg(long)]
    isolate_tests: bool,
    /// Select these Cargo test artifact names for running and, where possible, for building (repeatable).
    #[arg(long)]
    test_target: Vec<String>,
    /// Include these exact libtest names (repeatable).
    #[arg(long)]
    include_test: Vec<String>,
    /// Exclude these exact libtest names (repeatable).
    #[arg(long)]
    exclude_test: Vec<String>,
    /// Also allow tests marked #[ignore].
    #[arg(long)]
    include_ignored: bool,
    /// Executable in the workspace copy to invoke with the test binary and its arguments.
    #[arg(long, value_name = "PATH")]
    test_wrapper: Option<PathBuf>,
    /// Build the tests at this optimisation level (0 to 3) instead of the profile's own.
    /// The probes cost next to nothing in optimised code, which pays off when tests
    /// spend their time in loops; the build takes longer, and the tests no longer run
    /// exactly as `cargo test` builds them.
    #[arg(long, value_name = "N", value_parser = clap::value_parser!(u8).range(0..=3))]
    opt_level: Option<u8>,
    /// Time allowed for each test runner under a mutant, in seconds.
    #[arg(long, value_name = "SECONDS", value_parser = clap::value_parser!(u64).range(1..))]
    timeout: Option<u64>,
    /// Concurrent isolated baselines (default: one).
    #[arg(long, value_name = "N", default_value_t = 1, value_parser = clap::value_parser!(u32).range(1..))]
    baseline_jobs: u32,
    /// Retry timeouts from parallel mutation workers in isolation.
    #[arg(long, value_enum, default_value = "serial")]
    timeout_recheck: TimeoutRecheck,
    /// Seconds for a test wrapper to clean up after timeout on Unix. Zero kills immediately.
    #[arg(
        long,
        value_name = "SECONDS",
        default_value_t = 2,
        requires = "test_wrapper"
    )]
    wrapper_grace: u64,
    /// Features of the package to build with (as for `cargo test`; repeatable).
    #[arg(long, short = 'F', value_name = "FEATURES", value_delimiter = ',')]
    features: Vec<String>,
    /// Build with all features.
    #[arg(long, conflicts_with_all = ["features", "no_default_features"])]
    all_features: bool,
    /// Build without the default features.
    #[arg(long)]
    no_default_features: bool,
    /// Do not let cargo change the lock file.
    #[arg(long)]
    locked: bool,
    /// Check the run itself: run up to N of the surviving mutants once more against
    /// every test (not only the ones chosen for them), and record the tests a second
    /// time to see whether they do the same.
    #[arg(long, value_name = "N", conflicts_with = "no_verify")]
    audit: Option<usize>,
    /// Print the report as SARIF 2.1.0.
    #[arg(long, conflicts_with = "json")]
    sarif: bool,
    /// Also list the code that no test executes.
    #[arg(long)]
    show_unreached: bool,
    /// No progress output on stderr.
    #[arg(short, long)]
    quiet: bool,
}

/// Exit codes: 0 no gaps, 1 analysis failed, 2 gaps found, 3 incomplete evaluation.
fn main() -> ExitCode {
    let cli = Cli::parse_from(arguments(std::env::args_os()));
    let nextest = matches!(cli.test_tool, TestTool::Nextest);
    if !nextest && cli.nextest_profile.is_some() {
        eprintln!("tmt: --nextest-profile requires --test-tool nextest");
        return ExitCode::FAILURE;
    }
    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,
            targets: cli.test_target,
            include: cli.include_test,
            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,
        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,
        checkpoint: cli.checkpoint,
        doc: cli.doc,
    };
    if cli.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: {err:#}");
                return ExitCode::FAILURE;
            }
        };
        return show_workspace(members, cli.json, cli.sarif, cli.show_unreached);
    }
    match driver::run(&options) {
        Ok(result) => {
            show_report(&result, cli.json, cli.sarif, cli.show_unreached);
            ExitCode::from(report_exit(&result))
        }
        Err(err) => {
            eprintln!("tmt: error: {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!({
            "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, json: bool, sarif: bool, show_unreached: bool) {
    if sarif {
        println!("{}", report::render_sarif(result));
    } else if json {
        if result.mutants.is_some() {
            println!("{:#}", report_json(result));
        } else {
            println!(
                "{}",
                serde_json::to_string_pretty(result).expect("report serialises")
            );
        }
    } else if let Some(mutants) = &result.mutants {
        println!("{}: {} source candidates", result.package, mutants.len());
        for candidate in mutants {
            let edit = &candidate.edits[0];
            println!(
                "{}:{}:{}: {} -> {}",
                edit.file, edit.line, edit.column, candidate.what, candidate.into
            );
        }
    } else {
        print!("{}", report::render_text(result, show_unreached));
    }
}

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!({"package": result.package});
                record["report"] = report_json(&result);
                (record, (priority, code))
            }
            Err(err) => (
                serde_json::json!({"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: {err:#}");
        return ExitCode::FAILURE;
    }
    ExitCode::from(status.1)
}

fn show_workspace(
    members: Vec<(String, anyhow::Result<report::Report>)>,
    json: bool,
    sarif: bool,
    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: {package}: {message}");
                errors.push(serde_json::json!({"package": package, "error": message}));
            }
        }
    }
    let has_errors = !errors.is_empty();
    if json {
        let mut combined = serde_json::json!({});
        combined["packages"] = serde_json::Value::Array(reports.iter().map(report_json).collect());
        combined["errors"] = serde_json::Value::Array(errors);
        println!("{combined:#}");
    } else if 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));
    } else {
        for result in &reports {
            show_report(result, false, false, show_unreached);
        }
    }
    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::*;

    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("."));
    }
}