Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
72 changes: 47 additions & 25 deletions internal/services/covgate/covgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (r *runner) run(opts Opts) error {
return err
}

pkgs, err = r.applyExclude(pkgs, opts.Exclude, w)
pkgs, excluded, err := r.applyExclude(pkgs, opts.Exclude, w)
if err != nil {
return err
}
Expand All @@ -113,51 +113,54 @@ func (r *runner) run(opts Opts) error {
start := time.Now()
results := r.runPackages(pkgs, ctx, parallelism)
wallTime := time.Since(start)
return r.printResults(w, results, wallTime)
return r.printResults(w, results, excluded, module, wallTime)
}

// applyExclude removes packages matched by the comma-separated
// exclude patterns from pkgs. It preserves the original order of
// pkgs and prints a one-line notice to w when any package is
// actually removed. An empty (or whitespace-only) exclude string
// returns pkgs unchanged with no output.
// pkgs in both returned slices and prints a one-line notice to w
// when any package is actually removed. An empty (or
// whitespace-only) exclude string returns pkgs unchanged, a nil
// excluded slice, and no output.
func (r *runner) applyExclude(
pkgs []string, exclude string, w io.Writer,
) ([]string, error) {
) (kept []string, excluded []string, err error) {
if strings.TrimSpace(exclude) == "" {
return pkgs, nil
return pkgs, nil, nil
}

excluded := make(map[string]struct{})
excludedSet := make(map[string]struct{})
for _, raw := range strings.Split(exclude, ",") {
entry := strings.TrimSpace(raw)
if entry == "" {
continue
}
matched, err := r.goListPackages(entry)
if err != nil {
return nil, fmt.Errorf("resolve exclude %q: %w", entry, err)
matched, listErr := r.goListPackages(entry)
if listErr != nil {
return nil, nil, fmt.Errorf("resolve exclude %q: %w", entry, listErr)
}
for _, p := range matched {
excluded[p] = struct{}{}
excludedSet[p] = struct{}{}
}
}

kept := make([]string, 0, len(pkgs))
kept = make([]string, 0, len(pkgs))
excluded = make([]string, 0, len(excludedSet))
for _, p := range pkgs {
if _, drop := excluded[p]; drop {
if _, drop := excludedSet[p]; drop {
excluded = append(excluded, p)
continue
}
kept = append(kept, p)
}

if removed := len(pkgs) - len(kept); removed > 0 {
if len(excluded) > 0 {
_, _ = fmt.Fprintf(
w, "Excluded %d package(s) from coverage measurement\n",
removed,
len(excluded),
)
}
return kept, nil
return kept, excluded, nil
}

func (r *runner) runPackages(
Expand All @@ -181,7 +184,11 @@ func (r *runner) runPackages(
}

func (r *runner) printResults(
w io.Writer, results []checkResult, totalTime time.Duration,
w io.Writer,
results []checkResult,
excluded []string,
module string,
totalTime time.Duration,
) error {
hasFailures := false
for _, res := range results {
Expand All @@ -191,6 +198,10 @@ func (r *runner) printResults(
}
}

for _, pkg := range excluded {
_, _ = fmt.Fprint(w, skippedRow(gocover.RelPkg(pkg, module)))
}

_, _ = fmt.Fprintf(w, "\nTotal time: %s\n", fmtDuration(totalTime))
if hasFailures {
_, _ = fmt.Fprintln(
Expand All @@ -207,12 +218,12 @@ func (r *runner) printResults(

func printHeader(w io.Writer) {
_, _ = fmt.Fprintf(
w, "%-6s %8s %8s %8s %s\n",
w, "%-7s %8s %8s %8s %s\n",
"STATUS", "COVERAGE", "REQUIRED", "TIME", "PACKAGE",
)
_, _ = fmt.Fprintf(
w, "%-6s %8s %8s %8s %s\n",
"------", "--------", "--------", "--------", "-------",
w, "%-7s %8s %8s %8s %s\n",
"-------", "--------", "--------", "--------", "-------",
)
}

Expand Down Expand Up @@ -251,7 +262,7 @@ func (r *runner) checkPackage(pkg string, ctx checkPackageCtx) checkResult {
var b strings.Builder
if testErr != nil {
_, _ = fmt.Fprintf(
&b, "%-6s %8s %8s %8s %s\n",
&b, "%-7s %8s %8s %8s %s\n",
"FAIL", "---", "---", fmtDuration(elapsed),
relPkg+" (tests failed)",
)
Expand All @@ -263,7 +274,7 @@ func (r *runner) checkPackage(pkg string, ctx checkPackageCtx) checkResult {

if coverage < threshold {
_, _ = fmt.Fprintf(
&b, "%-6s %7.1f%% %7.1f%% %8s %s\n",
&b, "%-7s %7.1f%% %7.1f%% %8s %s\n",
"FAIL", coverage, threshold, fmtDuration(elapsed), relPkg,
)
return checkResult{b.String(), false, elapsed}
Expand All @@ -274,7 +285,7 @@ func (r *runner) checkPackage(pkg string, ctx checkPackageCtx) checkResult {
if gap > ctx.tightnessTolerance {
recommended := coverage - ctx.tightnessTolerance
_, _ = fmt.Fprintf(
&b, "%-6s %7.1f%% %7.1f%% %8s "+
&b, "%-7s %7.1f%% %7.1f%% %8s "+
"%s (required lags actual by %.1fpp; "+
"update .covgate to >= %.1f)\n",
"LOOSE", coverage, threshold, fmtDuration(elapsed),
Expand All @@ -285,12 +296,23 @@ func (r *runner) checkPackage(pkg string, ctx checkPackageCtx) checkResult {
}

_, _ = fmt.Fprintf(
&b, "%-6s %7.1f%% %7.1f%% %8s %s\n",
&b, "%-7s %7.1f%% %7.1f%% %8s %s\n",
"PASS", coverage, threshold, fmtDuration(elapsed), relPkg,
)
return checkResult{b.String(), true, elapsed}
}

// skippedRow formats a single SKIPPED row using the same column
// widths as PASS/FAIL/LOOSE. relPkg is the module-relative import
// path. Used for packages removed by --exclude so the user can see
// which ones were skipped.
func skippedRow(relPkg string) string {
return fmt.Sprintf(
"%-7s %8s %8s %8s %s\n",
"SKIPPED", "---", "---", "---", relPkg,
)
}

func fmtDuration(d time.Duration) string {
d = d.Round(100 * time.Millisecond)
if d < time.Minute {
Expand Down
81 changes: 75 additions & 6 deletions internal/services/covgate/covgate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,30 @@ func TestRun_Exclude(t *testing.T) {
lookup map[string][]string
wantContains []string
wantNotContain []string
extra func(t *testing.T, out string)
}{
{
name: "NoExclude",
exclude: "",
lookup: map[string][]string{"./...": allThree},
wantContains: []string{"pkg/a", "pkg/b", "pkg/c"},
wantNotContain: []string{"Excluded"},
wantNotContain: []string{"Excluded", "SKIPPED"},
extra: nil,
},
{
name: "Subset",
exclude: "./pkg/b",
lookup: map[string][]string{"./...": allThree, "./pkg/b": {pkgB}},
wantContains: []string{
"pkg/a",
"pkg/b",
"pkg/c",
"SKIPPED",
"Excluded 1 package(s) from coverage measurement",
"All packages meet minimum coverage requirement",
},
wantNotContain: []string{"pkg/b"},
wantNotContain: []string{"FAIL"},
extra: nil,
},
{
name: "NoOpPattern",
Expand All @@ -205,7 +211,8 @@ func TestRun_Exclude(t *testing.T) {
"./does-not-exist/...": {},
},
wantContains: []string{"pkg/a", "pkg/b", "pkg/c"},
wantNotContain: []string{"Excluded"},
wantNotContain: []string{"Excluded", "SKIPPED"},
extra: nil,
},
{
// Includes empty entries before, between, and after
Expand All @@ -219,21 +226,80 @@ func TestRun_Exclude(t *testing.T) {
"./pkg/c": {pkgC},
},
wantContains: []string{
"pkg/a",
"pkg/b",
"pkg/c",
"SKIPPED",
"Excluded 2 package(s) from coverage measurement",
},
wantNotContain: []string{"pkg/a", "pkg/c"},
wantNotContain: []string{"FAIL"},
extra: nil,
},
{
name: "AllPackages",
exclude: "./...",
lookup: map[string][]string{"./...": allThree},
wantContains: []string{
"pkg/a",
"pkg/b",
"pkg/c",
"SKIPPED",
"Excluded 3 package(s) from coverage measurement",
"All packages meet minimum coverage requirement",
"Total time:",
},
wantNotContain: []string{"pkg/a", "pkg/b", "pkg/c"},
wantNotContain: []string{"FAIL"},
extra: nil,
},
{
name: "OrderingAndCount",
exclude: "./pkg/b",
lookup: map[string][]string{"./...": allThree, "./pkg/b": {pkgB}},
wantContains: []string{"pkg/a", "pkg/b", "pkg/c", "SKIPPED"},
wantNotContain: []string{"FAIL"},
extra: func(t *testing.T, out string) {
t.Helper()
idxSkipped := strings.Index(out, "SKIPPED")
idxA := strings.Index(out, "pkg/a")
idxC := strings.Index(out, "pkg/c")
if idxA < 0 || idxC < 0 || idxSkipped < 0 {
t.Fatalf("expected pkg/a, pkg/c, and SKIPPED in output:\n%s", out)
}
if idxA >= idxSkipped {
t.Errorf(
"expected measured pkg/a row (%d) before SKIPPED block (%d):\n%s",
idxA, idxSkipped, out,
)
}
if idxC >= idxSkipped {
t.Errorf(
"expected measured pkg/c row (%d) before SKIPPED block (%d):\n%s",
idxC, idxSkipped, out,
)
}
if got := strings.Count(out, "SKIPPED"); got != 1 {
t.Errorf("expected exactly 1 SKIPPED, got %d:\n%s", got, out)
}
var skippedLine string
for _, line := range strings.Split(out, "\n") {
if strings.Contains(line, "pkg/b") {
skippedLine = line
break
}
}
if skippedLine == "" {
t.Fatalf("no line contained pkg/b:\n%s", out)
}
if !strings.Contains(skippedLine, "SKIPPED") {
t.Errorf("pkg/b line missing SKIPPED: %q", skippedLine)
}
if strings.Count(skippedLine, "---") != 3 {
t.Errorf(
"pkg/b SKIPPED line expected 3 '---' placeholders, got %d: %q",
strings.Count(skippedLine, "---"), skippedLine,
)
}
},
},
}

Expand Down Expand Up @@ -276,6 +342,9 @@ func TestRun_Exclude(t *testing.T) {
t.Errorf("output unexpectedly contains %q:\n%s", unwanted, out)
}
}
if tc.extra != nil {
tc.extra(t, out)
}
})
}
}
Expand Down Expand Up @@ -655,7 +724,7 @@ func TestPrintResults_UsesWallTime(t *testing.T) {
var buf bytes.Buffer
//nolint:exhaustruct // test uses partial initialization
r := runner{}
err := r.printResults(&buf, results, 3*time.Second)
err := r.printResults(&buf, results, nil, modName, 3*time.Second)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
Loading