internal/coverage: refactor EmitPercent in preparation for bugfix

Refactor cformat.EmitPercent to accept a package filter (list of
packages to report). This is a no-op in terms of exposed coverage
functionality, but we will need this feature in a subsequent patch.

Updates #65570.

Change-Id: I04ddc624a634837ea31c12ec395aa1295a0ea1f1
Reviewed-on: https://go-review.googlesource.com/c/go/+/592204
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Than McIntosh 2024-06-12 21:54:53 +00:00
parent 1683628d29
commit 7196db9e66
4 changed files with 38 additions and 13 deletions

View File

@ -325,7 +325,7 @@ func (d *dstate) Finish() {
// d.format maybe nil here if the specified input dir was empty.
if d.format != nil {
if d.cmd == percentMode {
d.format.EmitPercent(os.Stdout, "", false, false)
d.format.EmitPercent(os.Stdout, nil, "", false, false)
}
if d.cmd == funcMode {
d.format.EmitFuncs(os.Stdout)

View File

@ -103,7 +103,7 @@ func ProcessCoverTestDir(dir string, cfile string, cm string, cpkg string, w io.
}
// Emit percent.
if err := ts.cf.EmitPercent(w, cpkg, true, true); err != nil {
if err := ts.cf.EmitPercent(w, nil, cpkg, true, true); err != nil {
return err
}

View File

@ -66,7 +66,7 @@ lit.go:99.0,100.0 1 0`)
// Percent output with no aggregation.
noCoverPkg := ""
if err := fm.EmitPercent(&b2, noCoverPkg, false, false); err != nil {
if err := fm.EmitPercent(&b2, nil, noCoverPkg, false, false); err != nil {
t.Fatalf("EmitPercent returned %v", err)
}
wantPercent := strings.Fields(`
@ -81,7 +81,7 @@ lit.go:99.0,100.0 1 0`)
// Percent mode with aggregation.
withCoverPkg := " in ./..."
if err := fm.EmitPercent(&b3, withCoverPkg, false, true); err != nil {
if err := fm.EmitPercent(&b3, nil, withCoverPkg, false, true); err != nil {
t.Fatalf("EmitPercent returned %v", err)
}
wantPercent = strings.Fields(`
@ -110,6 +110,24 @@ total (statements) 62.5%`)
t.Logf("perc2 is %s\n", b3.String())
t.Logf("funcs is %s\n", b4.String())
}
// Percent output with specific packages selected.
{
var b strings.Builder
selpkgs := []string{"foo/bar", "my/pack1"}
if err := fm.EmitPercent(&b, selpkgs, noCoverPkg, false, false); err != nil {
t.Fatalf("EmitPercent returned %v", err)
}
wantPercent := strings.Fields(`
my/pack1 coverage: 66.7% of statements
`)
gotPercent := strings.Fields(b.String())
if !slices.Equal(wantPercent, gotPercent) {
t.Errorf("emit percent: got:\n%+v\nwant:\n%+v\n",
gotPercent, wantPercent)
}
}
}
func TestEmptyPackages(t *testing.T) {
@ -122,7 +140,7 @@ func TestEmptyPackages(t *testing.T) {
{
var b strings.Builder
noCoverPkg := ""
if err := fm.EmitPercent(&b, noCoverPkg, true, false); err != nil {
if err := fm.EmitPercent(&b, nil, noCoverPkg, true, false); err != nil {
t.Fatalf("EmitPercent returned %v", err)
}
wantPercent := strings.Fields(`
@ -140,7 +158,7 @@ func TestEmptyPackages(t *testing.T) {
{
var b strings.Builder
noCoverPkg := ""
if err := fm.EmitPercent(&b, noCoverPkg, true, true); err != nil {
if err := fm.EmitPercent(&b, nil, noCoverPkg, true, true); err != nil {
t.Fatalf("EmitPercent returned %v", err)
}
wantPercent := strings.Fields(`

View File

@ -23,7 +23,7 @@ package cformat
// }
// }
// }
// myformatter.EmitPercent(os.Stdout, "", true, true)
// myformatter.EmitPercent(os.Stdout, nil, "", true, true)
// myformatter.EmitTextual(somefile)
//
// These apis are linked into tests that are built with "-cover", and
@ -199,17 +199,21 @@ func (fm *Formatter) EmitTextual(w io.Writer) error {
return nil
}
// EmitPercent writes out a "percentage covered" string to the writer 'w'.
func (fm *Formatter) EmitPercent(w io.Writer, covpkgs string, noteEmpty bool, aggregate bool) error {
pkgs := make([]string, 0, len(fm.pm))
for importpath := range fm.pm {
pkgs = append(pkgs, importpath)
// EmitPercent writes out a "percentage covered" string to the writer
// 'w', selecting the set of packages in 'pkgs' and suffixing the
// printed string with 'inpkgs'.
func (fm *Formatter) EmitPercent(w io.Writer, pkgs []string, inpkgs string, noteEmpty bool, aggregate bool) error {
if len(pkgs) == 0 {
pkgs = make([]string, 0, len(fm.pm))
for importpath := range fm.pm {
pkgs = append(pkgs, importpath)
}
}
rep := func(cov, tot uint64) error {
if tot != 0 {
if _, err := fmt.Fprintf(w, "coverage: %.1f%% of statements%s\n",
100.0*float64(cov)/float64(tot), covpkgs); err != nil {
100.0*float64(cov)/float64(tot), inpkgs); err != nil {
return err
}
} else if noteEmpty {
@ -224,6 +228,9 @@ func (fm *Formatter) EmitPercent(w io.Writer, covpkgs string, noteEmpty bool, ag
var totalStmts, coveredStmts uint64
for _, importpath := range pkgs {
p := fm.pm[importpath]
if p == nil {
continue
}
if !aggregate {
totalStmts, coveredStmts = 0, 0
}