Repository navigation
feat(ci,scripts): add Go differential mutation testing tool and CI reporting - #1264
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new differential mutation testing tool (make mutation-diff) under scripts/mutation-diff/ to verify that unit tests meaningfully assert changed Go logic. It also refactors scripts/coverage-diff/ to extract shared Go test utilities into a new package scripts/gotestutil/, and updates the development guides and Go coding rules accordingly. Feedback on the changes points out a potential concurrency issue in the generated runtime helper's trace recording function (__khiMutRecord), suggesting the addition of a sync.Mutex to prevent race conditions and file sharing violations during concurrent test execution.
| var __khiMutSeen [%d]atomic.Bool | ||
|
|
||
| func __khiMutRecord(id int) { | ||
| if id <= 0 || id >= len(__khiMutSeen) { | ||
| return | ||
| } | ||
| if __khiMutSeen[id].Load() || __khiMutSeen[id].Swap(true) { | ||
| return | ||
| } | ||
| p := __khiMutTracePath() | ||
| if p == "" { | ||
| return | ||
| } | ||
| f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600) | ||
| if err != nil { | ||
| return | ||
| } | ||
| _, _ = f.WriteString(strconv.Itoa(id) + "\n") | ||
| _ = f.Close() | ||
| } |
There was a problem hiding this comment.
When running tests concurrently, multiple goroutines might call __khiMutRecord and attempt to open and write to the same trace file concurrently. On some operating systems (especially Windows), concurrent file opens for writing can fail with sharing violations or cause race conditions. Adding a package-level sync.Mutex to protect the file open and write operations ensures thread safety and prevents test flakiness.
var __khiMutMu sync.Mutex
var __khiMutSeen [%d]atomic.Bool
func __khiMutRecord(id int) {
if id <= 0 || id >= len(__khiMutSeen) {
return
}
if __khiMutSeen[id].Load() || __khiMutSeen[id].Swap(true) {
return
}
p := __khiMutTracePath()
if p == "" {
return
}
__khiMutMu.Lock()
defer __khiMutMu.Unlock()
f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
if err != nil {
return
}
_, _ = f.WriteString(strconv.Itoa(id) + "\n")
_ = f.Close()
}
Summary
Adds a zero-dependency Go differential mutation testing tool (
scripts/mutation-diff/make mutation-diff) and integrates soft-start PR reporting into.github/workflows/pullrequest.yaml.Line coverage (
coverage-diff) verifies whether changed lines were executed, whereasmutation-diffverifies whether unit tests meaningfully assert the behavior of changed lines and exposes redundant/dead production branches — without forcing brittle mock tests.Key Changes
scripts/gotestutil):jj&git), target Go file filtering,coverprofileloading (CoveredLineSet), and GitHub Actions workflow escaping fromscripts/coverage-diffintoscripts/gotestutilso both tools share a single implementation.scripts/mutation-diff):go/parser,go/ast,go/token, andgo test -overlayto synthesize candidate mutations on changed lines into a single temporary overlay per package (1go test -ccompile per modified package, zero working-tree mutation).KHI_MUTANT_ID=0+ lock-freeatomic.Booltrace) to skip unreached sites and executes reached mutants in parallel workers with-test.failfast.ExclusionRulePipeline: Automatically excludes boilerplateerr != nilguards,slog/log/klog/fmt.Print*/fmt.Errorf/errors.New/otelcalls,makecapacity hints /.Grow,defer/panic, and pure constant/type declarations to avoid encouraging noisy mock tests.// NOMUTANTS(<category>): <reason>Directives: Requires an explicitSuppressionCategory(equivalent,perf,observability,external) and rejects invariant/unreachable branch suppressions with guidance to simplify or delete redundant code instead.make mutation-diffinscripts/make/testing.mk.Report Backend Differential Mutation Testingstep (soft-start, exit0, inline::warningannotations +$GITHUB_STEP_SUMMARYMarkdown report with anti-bloat triage guide) in.github/workflows/pullrequest.yaml..agents/rules/go-coding-rule.mdanddocs/{en,ja}/development-contribution/development-guide.md.Verification
make pre-commit(0 issues/0 error(s))make build-go&make test-gomake mutation-diffagainst bothscripts/and recent KHImaincommits (pkg/...)