Reviewer · Testing
Regression test commit structure
When fixing a bug or changing behavior, structure the change as two commits so the diff itself demonstrates the fix: 1. **Test commit first**: Add or update the test(s) so they pass against the *current* (buggy) behavior, reproducing the issue from the bug report where possible. This proves the test actually exercises the code path in question.
When fixing a bug or changing behavior, structure the change as two commits so the diff itself demonstrates the fix:
- Test commit first: Add or update the test(s) so they pass against the current (buggy) behavior, reproducing the issue from the bug report where possible. This proves the test actually exercises the code path in question.
- Fix commit second: Apply the fix and update the test’s expected output/snapshot in the same commit. The resulting diff on the snapshot makes the behavior change obvious to reviewers and future readers, and keeps history bisectable.
If a test can’t be written to pass in the “before” state (e.g. it would require asserting an internal panic), still add it ahead of the fix and only flip its expected outcome in the fix commit—don’t fold test-writing and fix together.
Pair this with assertion style that keeps these diffs meaningful: prefer full snapshot assertions (with_stdout_data/with_stderr_data using str![[...]], .is_json(), assert_e2e().eq(...)) over partial contains/_contains checks or manual String::contains assertions. Snapshots show complete, structured output, can be auto-updated by tooling, and make it clear exactly what changed between the “before” and “after” commits. contains-style checks hide context, can mask unrelated regressions, and don’t produce a reviewable diff when behavior changes.
Example of the pattern:
// Commit 1 (test): passes today, captures existing behavior
#[cargo_test]
fn update_preserves_unrelated_dependency_choices() {
// ...
p.cargo("update -p c").with_stderr_data(str![[r#"
[old expected output]
"#]]).run();
}
// Commit 2 (fix): same test, snapshot updated to new behavior
#[cargo_test]
fn update_preserves_unrelated_dependency_choices() {
// ...
p.cargo("update -p c").with_stderr_data(str![[r#"
[new expected output showing the fix]
"#]]).run();
}
Avoid:
assert!(lockfile.contains(r#"name = "bar""#)); // weak, no diff value
Prefer:
assert_e2e().eq(p.read_file("Cargo.lock"), str![[r#"..."#]]);