|
| 1 | +# Mutation Testing |
| 2 | + |
| 3 | +This document describes how to run mutation testing locally to mimic the outcome of a PR, without the CI limitation it provides by timing out after 6 hours. |
| 4 | +[Here is the script](../contrib/tools/local-mutation-testing.sh) to run the tests locally by running the mutants created by the changes between `HEAD` and develop. |
| 5 | +It does automatically all the steps explained below. |
| 6 | + |
| 7 | +From the root level of the stacks-core repository run |
| 8 | +```sh |
| 9 | +./contrib/tools/local-mutation-testing.sh |
| 10 | +``` |
| 11 | + |
| 12 | +## Prerequirements |
| 13 | + |
| 14 | +Install the cargo mutants library |
| 15 | +```sh |
| 16 | +cargo install --version 24.7.1 cargo-mutants --locked |
| 17 | +``` |
| 18 | + |
| 19 | + |
| 20 | +## Steps |
| 21 | +1. Be on source branch you would use for the PR. |
| 22 | +2. Create diff file comparing this branch with the `develop` branch |
| 23 | + ```sh |
| 24 | + git diff origin/develop..HEAD > git.diff |
| 25 | + ``` |
| 26 | +3. Clean up the diff file and create auxiliary files |
| 27 | + ```sh |
| 28 | + awk ' |
| 29 | + /^diff --git/ { |
| 30 | + diff_line = $0 |
| 31 | + getline |
| 32 | + if ($0 !~ /^(deleted file mode|similarity index)/) { |
| 33 | + print diff_line |
| 34 | + print |
| 35 | + } |
| 36 | + } |
| 37 | + !/^(diff --git|deleted file mode|similarity index|rename from|rename to)/ {print} |
| 38 | + ' git.diff > processed.diff |
| 39 | + |
| 40 | + # Extract mutants based on the processed diff |
| 41 | + cargo mutants --in-diff processed.diff --list > all_mutants.txt |
| 42 | +
|
| 43 | + # Create a directory for organizing mutants |
| 44 | + mkdir -p mutants_by_package |
| 45 | + |
| 46 | + # Organize mutants into files based on their main folder |
| 47 | + while IFS= read -r line; do |
| 48 | + package=$(echo "$line" | cut -d'/' -f1) |
| 49 | +
|
| 50 | + case $package in |
| 51 | + "stackslib") |
| 52 | + echo "$line" >> "mutants_by_package/stackslib.txt" |
| 53 | + ;; |
| 54 | + "testnet") |
| 55 | + echo "$line" >> "mutants_by_package/stacks-node.txt" |
| 56 | + ;; |
| 57 | + "stacks-signer") |
| 58 | + echo "$line" >> "mutants_by_package/stacks-signer.txt" |
| 59 | + ;; |
| 60 | + *) |
| 61 | + echo "$line" >> "mutants_by_package/small-packages.txt" |
| 62 | + ;; |
| 63 | + esac |
| 64 | + done < all_mutants.txt |
| 65 | + ``` |
| 66 | +4. Based on the package required to run the mutants for |
| 67 | + a. Stackslib package |
| 68 | + ```sh |
| 69 | + regex_pattern=$(sed 's/[][()\.^$*+?{}|]/\\&/g' "mutants_by_package/stackslib.txt" | paste -sd'|' -) |
| 70 | +
|
| 71 | + RUST_BACKTRACE=1 BITCOIND_TEST=1 \ |
| 72 | + cargo mutants --timeout-multiplier 1.5 --no-shuffle -vV \ |
| 73 | + -F "$regex_pattern" \ |
| 74 | + -E ": replace .{1,2} with .{1,2} in " \ |
| 75 | + --output "./stackslib_mutants" \ |
| 76 | + --test-tool=nextest \ |
| 77 | + -- --all-targets --test-threads 1 |
| 78 | + ``` |
| 79 | + b. Stacks-node (testnet) package |
| 80 | + ```sh |
| 81 | + regex_pattern=$(sed 's/[][()\.^$*+?{}|]/\\&/g' "mutants_by_package/testnet.txt" | paste -sd'|' -) |
| 82 | +
|
| 83 | + RUST_BACKTRACE=1 BITCOIND_TEST=1 \ |
| 84 | + cargo mutants --timeout-multiplier 1.5 --no-shuffle -vV \ |
| 85 | + -F "$regex_pattern" \ |
| 86 | + -E ": replace .{1,2} with .{1,2} in " \ |
| 87 | + --output "./testnet_mutants" \ |
| 88 | + --test-tool=nextest \ |
| 89 | + -- --all-targets --test-threads 1 |
| 90 | + ``` |
| 91 | + c. Stacks-signer |
| 92 | + ```sh |
| 93 | + regex_pattern=$(sed 's/[][()\.^$*+?{}|]/\\&/g' "mutants_by_package/stacks-signer.txt" | paste -sd'|' -) |
| 94 | +
|
| 95 | + RUST_BACKTRACE=1 BITCOIND_TEST=1 \ |
| 96 | + cargo mutants --timeout-multiplier 1.5 --no-shuffle -vV \ |
| 97 | + -F "$regex_pattern" \ |
| 98 | + -E ": replace .{1,2} with .{1,2} in " \ |
| 99 | + --output "./stacks-signer_mutants" \ |
| 100 | + --test-tool=nextest \ |
| 101 | + -- --all-targets --test-threads 1 |
| 102 | + ``` |
| 103 | + d. All other packages combined |
| 104 | + ```sh |
| 105 | + regex_pattern=$(sed 's/[][()\.^$*+?{}|]/\\&/g' "mutants_by_package/small-packages.txt" | paste -sd'|' -) |
| 106 | +
|
| 107 | + cargo mutants --timeout-multiplier 1.5 --no-shuffle -vV \ |
| 108 | + -F "$regex_pattern" \ |
| 109 | + -E ": replace .{1,2} with .{1,2} in " \ |
| 110 | + --output "./small-packages_mutants" \ |
| 111 | + --test-tool=nextest \ |
| 112 | + -- --all-targets --test-threads 1 |
| 113 | + ``` |
| 114 | + |
| 115 | +## How to run one specific mutant to test it |
| 116 | + |
| 117 | +Example of output which had a missing mutant |
| 118 | +```sh |
| 119 | +MISSED stacks-signer/src/runloop.rs:424:9: replace <impl SignerRunLoop for RunLoop<Signer, T>>::run_one_pass -> Option<Vec<SignerResult>> with None in 3.0s build + 9.3s test |
| 120 | +``` |
| 121 | +
|
| 122 | +Example of fix for it |
| 123 | +```sh |
| 124 | +RUST_BACKTRACE=1 BITCOIND_TEST=1 \ |
| 125 | +cargo mutants -vV \ |
| 126 | + -F "replace process_stackerdb_event" \ |
| 127 | + -E ": replace <impl SignerRunLoop for RunLoop<Signer, T>>::run_one_pass -> Option<Vec<SignerResult>> with None in " \ |
| 128 | + --test-tool=nextest \ |
| 129 | + -- \ |
| 130 | + --run-ignored all \ |
| 131 | + --fail-fast \ |
| 132 | + --test-threads 1 |
| 133 | +``` |
| 134 | +
|
| 135 | +General command to run |
| 136 | +```sh |
| 137 | +RUST_BACKTRACE=1 BITCOIND_TEST=1 \ |
| 138 | +cargo mutants -vV \ |
| 139 | + -F "replace process_stackerdb_event" \ |
| 140 | + -E ": replace [modify this] with [modify this] in " \ |
| 141 | + --test-tool=nextest \ |
| 142 | + -- \ |
| 143 | + --run-ignored all \ |
| 144 | + --fail-fast \ |
| 145 | + --test-threads 1 |
| 146 | +``` |
0 commit comments