-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2733fb5
commit 9e2833d
Showing
22 changed files
with
892 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Incremental tests of modified code | ||
|
||
If you're working on a large project or one with a long test suite, you may not want to test the entire codebase every time you make a change. You can use `cargo-mutants --in-diff` to test only mutants generated from recently changed code. | ||
|
||
The `--in-diff DIFF_FILE` option tests only mutants that overlap with regions changed in the diff. | ||
|
||
The diff is expected to either have a prefix of `b/` on the new filename, which is the format produced by `git diff`, or no prefix. | ||
|
||
Some ways you could use `--in-diff`: | ||
|
||
1. Before submitting code, check your uncommitted changes with `git diff`. | ||
2. In CI, or locally, check the diff between the current branch and the base branch of the pull request. | ||
|
||
Changes to non-Rust files, or files from which no mutants are produced, are ignored. | ||
|
||
`--in-diff` is applied on the output of other filters including `--package` and `--regex`. For example, `cargo mutants --in-diff --package foo` will only test mutants in the `foo` package that overlap with the diff. | ||
|
||
## Caution | ||
|
||
`--in-diff` makes tests faster by covering the mutants that are most likely to be missed in the changed code. However, it's certainly possible that edits in one region cause code in a different region or a different file to no longer be well tested. Incremental tests are helpful for giving faster feedback, but they're not a substitute for a full test run. | ||
|
||
## Example | ||
|
||
In this diff, we've added a new function `two` to `src/lib.rs`, and the existing code is unaltered. With `--in-diff`, `cargo-mutants` will only test mutants that affect the function `two`. | ||
|
||
```diff | ||
|
||
```diff | ||
--- a/src/lib.rs 2023-11-12 13:05:25.774658230 -0800 | ||
+++ b/src/lib.rs 2023-11-12 12:54:04.373806696 -0800 | ||
@@ -2,6 +2,10 @@ | ||
"one".to_owned() | ||
} | ||
|
||
+pub fn two() -> String { | ||
+ format!("{}", 2) | ||
+} | ||
+ | ||
#[cfg(test)] | ||
mod test_super { | ||
use super::*; | ||
@@ -10,4 +14,9 @@ | ||
fn test_one() { | ||
assert_eq!(one(), "one"); | ||
} | ||
+ | ||
+ #[test] | ||
+ fn test_two() { | ||
+ assert_eq!(two(), "2"); | ||
+ } | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Incremental tests of pull requests | ||
|
||
You can use `--in-diff` to test only the code that has changed in a pull request. This can be useful for incremental testing in CI, where you want to test only the code that has changed since the last commit. | ||
|
||
For example, you can use the following workflow to test only the code that has changed in a pull request: | ||
|
||
```yaml | ||
name: Tests | ||
|
||
permissions: | ||
contents: read | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
incremental-mutants: | ||
runs-on: ubuntu-latest | ||
if: github.event_name == 'pull_request' | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
- name: Relative diff | ||
run: | | ||
git branch -av | ||
git diff origin/${{ github.base_ref }}.. | tee git.diff | ||
- uses: Swatinem/rust-cache@v2 | ||
- run: cargo install cargo-mutants | ||
- name: Mutants | ||
run: | | ||
cargo mutants --no-shuffle -j 2 -vV --in-diff git.diff | ||
- name: Archive mutants.out | ||
uses: actions/upload-artifact@v3 | ||
if: always() | ||
with: | ||
name: mutants-incremental.out | ||
path: mutants.out | ||
``` |
Oops, something went wrong.