Repository navigation
feat(ci,scripts): add Go differential coverage tool and CI reporting - #1258
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a Go differential coverage tool under scripts/coverage-diff/ to measure test coverage specifically on newly added or modified lines of code. It integrates this tool into the build system via a new make coverage-diff target, updates the development documentation in English and Japanese, and adds corresponding unit tests. Feedback on the implementation includes handling cases where no coverprofile is generated to prevent CI pipeline failures, and replacing a custom sorting algorithm with the standard library's slices.Sort utility.
| if profilePath != "" { | ||
| f, err := os.Open(profilePath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open coverprofile %s: %w", profilePath, err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| blocksByFile, err = ParseCoverProfile(f, modulePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } |
There was a problem hiding this comment.
If a pull request only modifies packages that do not have any unit tests yet, go test will succeed but will not generate a coverprofile file. In this scenario, os.Open(profilePath) will fail with a path does not exist error, causing the tool (and the CI pipeline) to fail. We should handle os.IsNotExist(err) gracefully and proceed with an empty coverage map.
if profilePath != "" {
f, err := os.Open(profilePath)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to open coverprofile %s: %w", profilePath, err)
}
} else {
defer f.Close()
blocksByFile, err = ParseCoverProfile(f, modulePath)
if err != nil {
return err
}
}
}| func sortedFileKeys(m map[string]map[int]struct{}) []string { | ||
| keys := make([]string, 0, len(m)) | ||
| for k := range m { | ||
| keys = append(keys, k) | ||
| } | ||
| return sortedStrings(keys) | ||
| } | ||
|
|
||
| func sortedStrings(in []string) []string { | ||
| out := append([]string(nil), in...) | ||
| for i := 1; i < len(out); i++ { | ||
| for j := i; j > 0 && out[j] < out[j-1]; j-- { | ||
| out[j], out[j-1] = out[j-1], out[j] | ||
| } | ||
| } | ||
| return out | ||
| } |
There was a problem hiding this comment.
Avoid implementing a custom insertion sort algorithm (sortedStrings) when the Go standard library provides highly optimized sorting utilities in the slices package. You can import slices and use slices.Sort directly on the keys slice.
| func sortedFileKeys(m map[string]map[int]struct{}) []string { | |
| keys := make([]string, 0, len(m)) | |
| for k := range m { | |
| keys = append(keys, k) | |
| } | |
| return sortedStrings(keys) | |
| } | |
| func sortedStrings(in []string) []string { | |
| out := append([]string(nil), in...) | |
| for i := 1; i < len(out); i++ { | |
| for j := i; j > 0 && out[j] < out[j-1]; j-- { | |
| out[j], out[j-1] = out[j-1], out[j] | |
| } | |
| } | |
| return out | |
| } | |
| func sortedFileKeys(m map[string]map[int]struct{}) []string { | |
| keys := make([]string, 0, len(m)) | |
| for k := range m { | |
| keys = append(keys, k) | |
| } | |
| slices.Sort(keys) | |
| return keys | |
| } |
Summary
Adds a standard-library Go differential coverage analyzer (
scripts/coverage-diff), a localmake coverage-difftarget, and GitHub Actions reporting inpullrequest.yamlwithout relying on external coverage services.scripts/coverage-diff:jj(fork_point(@ | trunk())..@) orgit(merge-base/HEAD^1..HEAD) while excluding tests,pkg/generated/,zzz_*.go,pkg/testutil/,scripts/, andweb/.go/scannertoken inspection alongside Gocoverprofileblocks so comments, blank lines, and closing braces are not miscounted, while single-line uncovered branches are still detected.--min-annotation-lines=4, sorted by uncovered executable line count descending) for GitHub Check Annotations (::warning) to avoid over-enforcing brittle mock tests on 1-3 line guards, and outputs a Markdown table + collapsible code snippets to$GITHUB_STEP_SUMMARY.make coverage-diff(COVERAGE_BASE,COVERAGE_FULL) running fast targetedgo teston affected packages (plus companion./<dir>/implpackages when present).backend-testsin.github/workflows/pullrequest.yamlto generatego-cover.outputduring the existinggo teststep and report differential coverage safely on all PRs (including fork PRs with read-only permissions).