Repository navigation
feat(scripts): add redundant test detection to mutation-diff - #1265
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces functionality to detect and report potentially redundant unit tests and subtests (categorized as DUPLICATE or WEAK_TEST) during differential mutation testing. It adds a new redundancy analysis engine in scripts/mutation-diff/redundancy.go, integrates it with the mutation runner and reporting systems, and updates the documentation and Makefiles accordingly. Feedback on the changes highlights that the subtest name normalization logic in normalizeGoSubtestName should be updated to match Go's standard library sanitization rules to prevent silent mismatches when subtest names contain special characters.
| func normalizeGoSubtestName(raw string) string { | ||
| var sb strings.Builder | ||
| sb.Grow(len(raw)) | ||
| for _, r := range raw { | ||
| if unicode.IsSpace(r) { | ||
| sb.WriteByte('_') | ||
| } else { | ||
| sb.WriteRune(r) | ||
| } | ||
| } | ||
| return sb.String() | ||
| } |
There was a problem hiding this comment.
In Go's testing package, subtest names are sanitized using a safeName function that replaces any character other than alphanumeric, _, -, and . with an underscore (_). The current implementation of normalizeGoSubtestName only replaces spaces, which can lead to silent mismatches and failure to detect redundant tests when subtest names contain parentheses, slashes, or other non-safe characters (e.g., name: "should return error (invalid input)"). Updating this function to match Go's exact sanitization logic ensures robust subtest matching.
| func normalizeGoSubtestName(raw string) string { | |
| var sb strings.Builder | |
| sb.Grow(len(raw)) | |
| for _, r := range raw { | |
| if unicode.IsSpace(r) { | |
| sb.WriteByte('_') | |
| } else { | |
| sb.WriteRune(r) | |
| } | |
| } | |
| return sb.String() | |
| } | |
| func normalizeGoSubtestName(raw string) string { | |
| var sb strings.Builder | |
| sb.Grow(len(raw)) | |
| for i := 0; i < len(raw); i++ { | |
| c := raw[i] | |
| if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '.' { | |
| sb.WriteByte(c) | |
| } else { | |
| sb.WriteByte('_') | |
| } | |
| } | |
| return sb.String() | |
| } |
Summary
Follows up on #1264 by adding Kill-Matrix & Branch-Polarity redundant test detection (
--check-redundant-tests, enabled by default) toscripts/mutation-diff.While differential coverage (
coverage-diff) and surviving mutant detection (mutation-diff) guard against missing or weak tests, AI-generated PRs frequently err in the opposite direction by adding duplicate equivalence-class subtests or assertion-free smoke tests. This change reuses the Super-Mutant Schemata compile and execution pass to automatically flag redundant tests added or modified in the PR diff.Key Changes
scripts/gotestutil):IsTargetGoTestFile,ParseUnifiedDiffSplit, andDiffResult.ChangedTestLinesso redundant-test reporting is scoped strictly to newly added or modifiedTest*functions and subtests in*_test.go(preventing noise on untouched legacy tests).scripts/mutation-diff/schema.go):zzz_khi_mutant_runtime.gowith lock-free[N][3]atomic.Boolslots (<mutantID>:T,<mutantID>:F,<mutantID>:S) andParsePolarityTraceFileto record exact branch directions per leaf test.scripts/mutation-diff/redundancy.go,runner.go,report.go):-test.vper-subtest failure parsing (VerboseTestResult) to surface two high-precision categories:DUPLICATE: A newly added/changed test hasKills(cand) != ∅, exercises identical branch polarities (equalStringSet— preventing multi-call integration tests from subsuming focused unit tests), kills a subset of another test's mutants (Kills(cand) ⊆ Kills(dom)), and did not touch any budget-skipped mutant (MutantSkippedBudget).WEAK_TEST: A newly added/changed test has0mutant kills, contains onlyif err != nilchecks (or no assertions) in its AST body, and exercises a branch-polarity subset already covered by a mutant-killing test (preserving valid fixed-point boundary value assertions).::warningannotations, and a collapsible$GITHUB_STEP_SUMMARYtable.coverage-diffScript-Package Fix (scripts/coverage-diff,scripts/make/testing.mk):FilterOptionsintoPlanTestExecutionand addedCOVERAGE_INCLUDE_SCRIPTS/COVERAGE_VCSsomake coverage-diffandmake mutation-diffshare symmetric flags when analyzingscripts/.Verification
make pre-commit(0 issues/0 error(s))make build-go&make test-gomake coverage-diff COVERAGE_INCLUDE_SCRIPTS=true:87.8%(568 / 647executable changed lines covered)make mutation-diff MUTATION_INCLUDE_SCRIPTS=true:77.5%(155 / 200killed,0redundant tests in diff)maincommits (origin/main~4)