|
| 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 mutation locally running the mutants created by the changes between the current branch and develop. It does automatically all the steps explained below. |
| 5 | + |
| 6 | +From the root level of the stacks-core repository run |
| 7 | +```sh |
| 8 | +./contrib/tools/local-mutation-testing.sh |
| 9 | +``` |
| 10 | + |
| 11 | +## Prerequirements |
| 12 | + |
| 13 | +Install the cargo mutants library |
| 14 | +```sh |
| 15 | +cargo install --version 24.7.1 cargo-mutants --locked |
| 16 | +``` |
| 17 | + |
| 18 | + |
| 19 | +## Steps |
| 20 | +1. be on source branch you would use for the PR. |
| 21 | +2. create diff file comparing this branch with the `develop` branch |
| 22 | + ```sh |
| 23 | + git diff origin/develop..HEAD > git.diff |
| 24 | + ``` |
| 25 | +3. clean up the diff file and create auxiliary files |
| 26 | + ```sh |
| 27 | + awk ' |
| 28 | + /^diff --git/ { |
| 29 | + diff_line = $0 |
| 30 | + getline |
| 31 | + if ($0 !~ /^(deleted file mode|similarity index)/) { |
| 32 | + print diff_line |
| 33 | + print |
| 34 | + } |
| 35 | + } |
| 36 | + !/^(diff --git|deleted file mode|similarity index|rename from|rename to)/ {print} |
| 37 | + ' git.diff > processed.diff |
| 38 | + |
| 39 | + # Extract mutants based on the processed diff |
| 40 | + cargo mutants --in-diff processed.diff --list > all_mutants.txt |
| 41 | +
|
| 42 | + # Create a directory for organizing mutants |
| 43 | + mkdir -p mutants_by_package |
| 44 | + |
| 45 | + # Organize mutants into files based on their main folder |
| 46 | + while IFS= read -r line; do |
| 47 | + package=$(echo "$line" | cut -d'/' -f1) |
| 48 | +
|
| 49 | + case $package in |
| 50 | + "stackslib") |
| 51 | + echo "$line" >> "mutants_by_package/stackslib.txt" |
| 52 | + ;; |
| 53 | + "testnet") |
| 54 | + echo "$line" >> "mutants_by_package/stacks-node.txt" |
| 55 | + ;; |
| 56 | + "stacks-signer") |
| 57 | + echo "$line" >> "mutants_by_package/stacks-signer.txt" |
| 58 | + ;; |
| 59 | + *) |
| 60 | + echo "$line" >> "mutants_by_package/small-packages.txt" |
| 61 | + ;; |
| 62 | + esac |
| 63 | + done < all_mutants.txt |
| 64 | + ``` |
| 65 | +4. based on the package required to run the mutants for |
| 66 | + a. stackslib package |
| 67 | + ```sh |
| 68 | + regex_pattern=$(sed 's/[][()\.^$*+?{}|]/\\&/g' "mutants_by_package/stackslib.txt" | paste -sd'|' -) |
| 69 | +
|
| 70 | + cargo mutants --timeout-multiplier 1.5 --no-shuffle -vV \ |
| 71 | + -F "$regex_pattern" \ |
| 72 | + -E ": replace .{1,2} with .{1,2} in " \ |
| 73 | + --output "./stackslib_mutants" \ |
| 74 | + --test-tool=nextest \ |
| 75 | + -- --all-targets --test-threads 1 |
| 76 | + ``` |
| 77 | + b. stacks-node (testnet) package |
| 78 | + ```sh |
| 79 | + regex_pattern=$(sed 's/[][()\.^$*+?{}|]/\\&/g' "mutants_by_package/testnet.txt" | paste -sd'|' -) |
| 80 | +
|
| 81 | + cargo mutants --timeout-multiplier 1.5 --no-shuffle -vV \ |
| 82 | + -F "$regex_pattern" \ |
| 83 | + -E ": replace .{1,2} with .{1,2} in " \ |
| 84 | + --output "./testnet_mutants" \ |
| 85 | + --test-tool=nextest \ |
| 86 | + -- --all-targets --test-threads 1 |
| 87 | + ``` |
| 88 | + c. stacks-signer |
| 89 | + ```sh |
| 90 | + regex_pattern=$(sed 's/[][()\.^$*+?{}|]/\\&/g' "mutants_by_package/stacks-signer.txt" | paste -sd'|' -) |
| 91 | +
|
| 92 | + cargo mutants --timeout-multiplier 1.5 --no-shuffle -vV \ |
| 93 | + -F "$regex_pattern" \ |
| 94 | + -E ": replace .{1,2} with .{1,2} in " \ |
| 95 | + --output "./stacks-signer_mutants" \ |
| 96 | + --test-tool=nextest \ |
| 97 | + -- --all-targets --test-threads 1 |
| 98 | + ``` |
| 99 | + d. all other packages combined |
| 100 | + ```sh |
| 101 | + regex_pattern=$(sed 's/[][()\.^$*+?{}|]/\\&/g' "mutants_by_package/small-packages.txt" | paste -sd'|' -) |
| 102 | +
|
| 103 | + cargo mutants --timeout-multiplier 1.5 --no-shuffle -vV \ |
| 104 | + -F "$regex_pattern" \ |
| 105 | + -E ": replace .{1,2} with .{1,2} in " \ |
| 106 | + --output "./small-packages_mutants" \ |
| 107 | + --test-tool=nextest \ |
| 108 | + -- --all-targets --test-threads 1 |
| 109 | + ``` |
0 commit comments