Repository navigation
Conversation
The coverage tool now appends a Markdown report to the file named by GITHUB_STEP_SUMMARY when that variable is set. The report shows the total statement coverage, the target, whether the check passed, and a per-package breakdown aggregated from coverage.out. Previously the coverage result was only visible in the job log. Every workflow already runs this tool, so each pull request now gets a coverage report on its workflow run summary page without workflow changes, extra permissions, or new dependencies.
There was a problem hiding this comment.
Code Review
This pull request adds functionality to append a Markdown coverage report to the file specified by the GITHUB_STEP_SUMMARY environment variable, which is useful for GitHub Actions. It includes functions to parse Go coverage profiles, aggregate statement coverage by package, and format the results into a Markdown table, along with comprehensive unit tests. Feedback suggests simplifying the file closing and error handling logic in the writeStepSummary function to improve readability.
| func writeStepSummary(total, target float64) error { | ||
| name := os.Getenv("GITHUB_STEP_SUMMARY") | ||
| if name == "" { | ||
| return nil | ||
| } | ||
| profile, err := os.ReadFile("coverage.out") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| pkgs, err := packageCoverage(string(profile)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| f, err := os.OpenFile(name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if _, err := f.WriteString(formatSummary(total, target, pkgs)); err != nil { | ||
| f.Close() | ||
| return err | ||
| } | ||
| return f.Close() | ||
| } |
There was a problem hiding this comment.
Prefer simple and direct resource closing (e.g., calling Close() directly and handling the error) over complex anonymous functions with deferred closes and named return values, to keep the code simple and readable.
func writeStepSummary(total, target float64) error {
name := os.Getenv("GITHUB_STEP_SUMMARY")
if name == "" {
return nil
}
profile, err := os.ReadFile("coverage.out")
if err != nil {
return err
}
pkgs, err := packageCoverage(string(profile))
if err != nil {
return err
}
f, err := os.OpenFile(name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return err
}
_, err = f.WriteString(formatSummary(total, target, pkgs))
if cerr := f.Close(); err == nil {
err = cerr
}
return err
}References
- Prefer simple and direct resource closing (e.g., calling Close() directly and handling the error) over complex anonymous functions with deferred closes and named return values, to keep the code simple and readable.
The coverage tool now appends a Markdown report to the file named by GITHUB_STEP_SUMMARY when that variable is set. The report shows the total statement coverage, the target, whether the check passed, and a per-package breakdown aggregated from coverage.out.
Previously the coverage result was only visible in the job log. Every workflow already runs this tool, so each pull request now gets a coverage report on its workflow run summary page without workflow changes, extra permissions, or new dependencies.