build(xtask): local mutants pinned to one job and a third of the cores, jobs flag rejected; mutants skill rewritten to the wrapper
3b195f93e73f8be25766c90db13983dbae5d2aba
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 857f1ac05253
There are many coding harnesses - but this one is fast
3b195f93e73f8be25766c90db13983dbae5d2aba
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+64 / −92 text lines
+0 / −6 text lines
## Trigger- New or edited `struct` | `enum` | container field | `Box` | `Arc` | `Vec<Vec<_>>` | `HashMap` on a type with many instances or on a per-frame, per-turn, per-request path.- Choosing between `Vec`, arena, map, tree, linked structure.- Any loop over more than a cache's worth of records.+26 / −74 text lines
description: Use when writing or editing tests and checking whether they actually catch mutations; fast scoped cargo-mutants workflows, flag choices, and result triage.description: "Use when writing or editing tests, or triaging a missed mutant: scoped cargo-mutants runs through `cargo x mutants` and what to do with each outcome."Test strength, not coverage. One run answers: which bugs could ship because no test notices them?Test strength, not coverage: which bugs ship because no test notices.Every run copies the tree into a scratch dir and builds cold once per job; every mutant then rebuilds incrementally and runs tests.Cost ≈ (cold build × jobs) + (mutants × test time).So: narrow scope first, jobs low, invocations chunky.## Recipes, fastest first1. Preview mutants — free, no build:Always through `cargo x mutants`, always scoped; the wrapper refuses an unscoped run.Locally it is pinned: one job, `nice -n 19`, a third of the cores for build and tests, 120 s per mutant, 300 s per build, output under `target/mutants.out/`.Those pins are not overridable locally (`-j` is rejected); `--all` is CI's and follows its own rules.cargo-mutants mutants --list -f src/thing.rs --line-col truecargo-mutants mutants --list --json | jq lengthcargo x mutants --list -f smith-core/src/session.rs # inventory, no buildcargo x mutants -f smith-core/src/session.rs # one file, while editing testsjj diff --git > /tmp/d.patch; cargo x mutants -D /tmp/d.patch # changed lines onlycargo x mutants -f smith-core/src/session.rs --iterate # rerun only missed and unviablecargo x mutants --shard 0/8 # whole tree: one shard per invocation, 0..7(`--list` builds nothing; call the binary directly or through `cargo x mutants -f … -- --list`.)2. One-file loop — the default while editing tests:```bashcargo x mutants -f smith-core/src/session.rs```The wrapper skips the baseline; run `cargo x test` first.First invocation pays one cold build; mutants after are incremental.3. Changed lines only — after editing existing code:```bashjj diff --git > /tmp/d.patchcargo x mutants -D /tmp/d.patch```The diff matches code under test only: test-only diffs produce zero mutants.Git-style `b/` prefixes or bare paths both work; composes with `-p`.4. Tightening loop — fix tests, then skip what is already caught:```bashcargo x mutants -f smith-core/src/session.rs --iterate```5. Whole tree, one shard at a time; never the whole tree in one go:```bashcargo x mutants --shard 0/8 # then 1/8 … 7/8, each its own invocation````cargo x mutants` refuses an unscoped run (`--all` exists for CI only): afull run copies the tree per job and has crashed the machine. One job,`nice -n 19`, 120 s per-mutant timeout are the wrapper's defaults.## Flags, decided- jobs stay at 1 locally (`CARGO_MUTANTS_JOBS` overrides); each job is a full scratch tree and a full nextest fan-out, four jobs crashed this machine.- `-D/--in-diff` — best scope tool for edits.- `--iterate` — reuses prior outcomes; only reruns missed and unviable mutants.- `-f/-e` file globs, `-F/-E` mutant-name regexes — narrower beats faster.- `--list [--json] [--line-col]` — free inventory; `--json` is list-only, not run output.- Never `--in-place` — mutating the real tree under jj invites snapshot churn and loses the safety copy.- No `--fail-fast` on cargo-mutants; nextest's fail-fast comes from the project's `mutants` nextest profile.## Reading results## Read## Triage a missed mutant1. Meaningful behavior → write the test that fails under it (regression-first), rerun with `--iterate`.2. Not observable → the code is dead or the guard is redundant; delete it.3. Untestable by design (`Debug` output) → `exclude_re` in `.cargo/mutants.toml`, never a `#[mutants::skip]` dependency.4. A miss that needs a production change is a bug: fix it with the test that failed first.1. Meaningful behavior → write the test that fails under it; rerun with `--iterate`.2. Untestable by design (debug formatting, trivial getters) → exclude: `-E 'Debug|fmt'` ad hoc, or `exclude_re` in the project's mutants config.3. Never add the `mutants` crate for `#[mutants::skip]` unless the project already ships it — config excludes are dependency-free.## Do not+1 / −1 text lines
- Strategies from the domain: build valid values by construction, not by filtering; filter only for rare exclusions.- Keep the default case count small enough for the commit tier; raise it via `PROPTEST_CASES` in nightly.- Keep the default case count small enough for the commit tier; raise `PROPTEST_CASES` when hunting.+1 / −1 text lines
- One snapshot per rendered surface; name it after the surface and state (`help_top_level`, `rpc_prompt_then_abort`).- Redact identity and time before snapshotting (ids, timestamps, paths, versions); a snapshot that changes every run is noise.- Use structured snapshots (`assert_json_snapshot!`, `assert_yaml_snapshot!`) for data; string snapshots for text people read.+36 / −10 text lines
let jobs = std::env::var("CARGO_MUTANTS_JOBS").unwrap_or_else(|_| "1".to_string()); // Lowest scheduling priority: a mutants run is a background chore and // must never starve the shell, even with one job its nextest fans out. let threads = std::env::var("NEXTEST_TEST_THREADS").unwrap_or_else(|_| { std::thread::available_parallelism() .map_or(2, |cores| (cores.get() / 3).max(2)) .to_string() }); // Local runs are pinned to one job and a third of the cores for both // the build and the test phase: four jobs and a full fan-out crashed // this machine. Only `--all` (CI) reads `CARGO_MUTANTS_JOBS`. if !all && passthrough.iter().any(|arg| jobs_flag(arg)) { return fail("cargo x mutants runs one job locally; drop -j/--jobs\n"); } let jobs = if all { std::env::var("CARGO_MUTANTS_JOBS").unwrap_or_else(|_| "1".to_string()) } else { "1".to_string() }; let cores = std::thread::available_parallelism().map_or(2, |cores| (cores.get() / 3).max(2)); #[test] fn local_mutants_reject_a_job_count() { let flag = |arg: &str| super::jobs_flag(&std::ffi::OsString::from(arg)); assert!(flag("-j")); assert!(flag("-j4")); assert!(flag("--jobs")); assert!(flag("--jobs=2")); assert!(!flag("-jx")); assert!(!flag("--shard")); }