Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions pkg/testcoverage/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,18 @@
}

func Analyze(cfg Config, current, base []coverage.Stats) AnalyzeResult {
var hasFileOverrides, hasPackageOverrides bool
thr := cfg.Threshold

Check failure on line 78 in pkg/testcoverage/check.go

View workflow job for this annotation

GitHub Actions / lint

assignments should only be cuddled with other assignments (wsl)
overrideRules, hasOverrides := compileOverridePathRules(cfg)
overrideRules := compileOverridePathRules(cfg)

if len(cfg.Override) > 0 {
hasFileOverrides, hasPackageOverrides = detectOverrides(cfg.Override)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if len(cfg.Override) > 0 { seems unnecessary, detectOverrides should return correct result for any size of cfg.Override


return AnalyzeResult{
Threshold: thr,
HasOverrides: hasOverrides,
HasFileOverrides: hasFileOverrides,
HasPackageOverrides: hasPackageOverrides,
FilesBelowThreshold: checkCoverageStatsBelowThreshold(current, thr.File, overrideRules),
PackagesBelowThreshold: checkCoverageStatsBelowThreshold(
makePackageStats(current), thr.Package, overrideRules,
Expand All @@ -90,6 +96,21 @@
}
}

func detectOverrides(overrides []Override) (hasFileOverrides, hasPackageOverrides bool) {

Check failure on line 99 in pkg/testcoverage/check.go

View workflow job for this annotation

GitHub Actions / lint

named return "hasFileOverrides" with type "bool" found (nonamedreturns)
for _, override := range overrides {
if strings.HasSuffix(override.Path, ".go") {
hasFileOverrides = true
}
if strings.HasPrefix(override.Path, "^") {

Check failure on line 104 in pkg/testcoverage/check.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
hasPackageOverrides = true
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to have these if statements like this:

if is file override {
  ...
} else {
 if it is not file override then it must be package override
}

also file overrides may have .go$ suffix, for example here .

Copy link
Contributor Author

@subhambhardwaj subhambhardwaj Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the check here to

if override name contains ".go" suffix || ".go$" suffix {
    // file override
} else {
    // pkg override
}

if hasFileOverrides && hasPackageOverrides {

Check failure on line 107 in pkg/testcoverage/check.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't have anything against of merging this if, but it seems unnecessary. these overrides are usually very small, lets say there is 100 of them (99.99% of users will have less then 10). with this size, this for cycle will finish in 1ns or less, so this if wont produce any noticeably benefits. and on another hand because this repo requires 100% coverage, this if will required to have additional tests case that needs to be added or ignored.

return // Early return if both found
}
}
return

Check failure on line 111 in pkg/testcoverage/check.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
}

func saveCoverageBreakdown(cfg Config, stats []coverage.Stats) error {
if cfg.BreakdownFileName == "" {
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/testcoverage/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ func reportCoverage(w io.Writer, result AnalyzeResult) {

thr := result.Threshold

if thr.File > 0 || result.HasOverrides { // File threshold report
if thr.File > 0 || result.HasFileOverrides { // File threshold report
fmt.Fprintf(tabber, "File coverage threshold (%d%%) satisfied:\t", thr.File)
fmt.Fprint(tabber, statusStr(len(result.FilesBelowThreshold) == 0))
reportIssuesForHuman(tabber, result.FilesBelowThreshold)
fmt.Fprint(tabber, "\n")
}

if thr.Package > 0 || result.HasOverrides { // Package threshold report
if thr.Package > 0 || result.HasPackageOverrides { // Package threshold report
fmt.Fprintf(tabber, "Package coverage threshold (%d%%) satisfied:\t", thr.Package)
fmt.Fprint(tabber, statusStr(len(result.PackagesBelowThreshold) == 0))
reportIssuesForHuman(tabber, result.PackagesBelowThreshold)
Expand Down
3 changes: 2 additions & 1 deletion pkg/testcoverage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type AnalyzeResult struct {
TotalStats coverage.Stats
HasBaseBreakdown bool
Diff []FileCoverageDiff
HasOverrides bool
HasFileOverrides bool
HasPackageOverrides bool
}

func (r *AnalyzeResult) Pass() bool {
Expand Down
6 changes: 3 additions & 3 deletions pkg/testcoverage/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func matches(regexps []regRule, str string) (int, bool) {
return 0, false
}

func compileOverridePathRules(cfg Config) ([]regRule, bool) {
func compileOverridePathRules(cfg Config) []regRule {
if len(cfg.Override) == 0 {
return nil, false
return nil
}

compiled := make([]regRule, len(cfg.Override))
Expand All @@ -33,5 +33,5 @@ func compileOverridePathRules(cfg Config) ([]regRule, bool) {
}
}

return compiled, true
return compiled
}
Loading