1
0
mirror of https://github.com/golang/go synced 2024-11-26 17:56:55 -07:00

cmd/cover: use golang.org/x/tools/cover directly

As suggested by Bryan in CL 249759, remove the forwarding aliases in
cmd/cover and use the symbols from golang.org/x/tools directly.

cmd/cover is not an importable package, so it is fine to remove these
exported symbols.

Change-Id: I887c5e9349f2dbe4c90be57f708412b844e18081
Reviewed-on: https://go-review.googlesource.com/c/go/+/304690
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
Tobias Klauser 2021-03-25 20:48:27 +01:00 committed by Tobias Klauser
parent 5cec8b85e5
commit 691db3737c
3 changed files with 10 additions and 36 deletions

View File

@ -23,6 +23,8 @@ import (
"runtime" "runtime"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"golang.org/x/tools/cover"
) )
// funcOutput takes two file names as arguments, a coverage profile to read as input and an output // funcOutput takes two file names as arguments, a coverage profile to read as input and an output
@ -38,7 +40,7 @@ import (
// total: (statements) 91.9% // total: (statements) 91.9%
func funcOutput(profile, outputFile string) error { func funcOutput(profile, outputFile string) error {
profiles, err := ParseProfiles(profile) profiles, err := cover.ParseProfiles(profile)
if err != nil { if err != nil {
return err return err
} }
@ -144,7 +146,7 @@ func (v *FuncVisitor) Visit(node ast.Node) ast.Visitor {
} }
// coverage returns the fraction of the statements in the function that were covered, as a numerator and denominator. // coverage returns the fraction of the statements in the function that were covered, as a numerator and denominator.
func (f *FuncExtent) coverage(profile *Profile) (num, den int64) { func (f *FuncExtent) coverage(profile *cover.Profile) (num, den int64) {
// We could avoid making this n^2 overall by doing a single scan and annotating the functions, // We could avoid making this n^2 overall by doing a single scan and annotating the functions,
// but the sizes of the data structures is never very large and the scan is almost instantaneous. // but the sizes of the data structures is never very large and the scan is almost instantaneous.
var covered, total int64 var covered, total int64
@ -175,7 +177,7 @@ type Pkg struct {
} }
} }
func findPkgs(profiles []*Profile) (map[string]*Pkg, error) { func findPkgs(profiles []*cover.Profile) (map[string]*Pkg, error) {
// Run go list to find the location of every package we care about. // Run go list to find the location of every package we care about.
pkgs := make(map[string]*Pkg) pkgs := make(map[string]*Pkg)
var list []string var list []string

View File

@ -15,13 +15,15 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"golang.org/x/tools/cover"
) )
// htmlOutput reads the profile data from profile and generates an HTML // htmlOutput reads the profile data from profile and generates an HTML
// coverage report, writing it to outfile. If outfile is empty, // coverage report, writing it to outfile. If outfile is empty,
// it writes the report to a temporary file and opens it in a web browser. // it writes the report to a temporary file and opens it in a web browser.
func htmlOutput(profile, outfile string) error { func htmlOutput(profile, outfile string) error {
profiles, err := ParseProfiles(profile) profiles, err := cover.ParseProfiles(profile)
if err != nil { if err != nil {
return err return err
} }
@ -92,7 +94,7 @@ func htmlOutput(profile, outfile string) error {
// percentCovered returns, as a percentage, the fraction of the statements in // percentCovered returns, as a percentage, the fraction of the statements in
// the profile covered by the test run. // the profile covered by the test run.
// In effect, it reports the coverage of a given source file. // In effect, it reports the coverage of a given source file.
func percentCovered(p *Profile) float64 { func percentCovered(p *cover.Profile) float64 {
var total, covered int64 var total, covered int64
for _, b := range p.Blocks { for _, b := range p.Blocks {
total += int64(b.NumStmt) total += int64(b.NumStmt)
@ -108,7 +110,7 @@ func percentCovered(p *Profile) float64 {
// htmlGen generates an HTML coverage report with the provided filename, // htmlGen generates an HTML coverage report with the provided filename,
// source code, and tokens, and writes it to the given Writer. // source code, and tokens, and writes it to the given Writer.
func htmlGen(w io.Writer, src []byte, boundaries []Boundary) error { func htmlGen(w io.Writer, src []byte, boundaries []cover.Boundary) error {
dst := bufio.NewWriter(w) dst := bufio.NewWriter(w)
for i := range src { for i := range src {
for len(boundaries) > 0 && boundaries[0].Offset == i { for len(boundaries) > 0 && boundaries[0].Offset == i {

View File

@ -1,30 +0,0 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file provides support for parsing coverage profiles
// generated by "go test -coverprofile=cover.out".
// It is a alias of golang.org/x/tools/cover/profile.go.
package main
import (
"golang.org/x/tools/cover"
)
// Profile represents the profiling data for a specific file.
type Profile = cover.Profile
// ProfileBlock represents a single block of profiling data.
type ProfileBlock = cover.ProfileBlock
// ParseProfiles parses profile data in the specified file and returns a
// Profile for each source file described therein.
func ParseProfiles(fileName string) ([]*Profile, error) {
return cover.ParseProfiles(fileName)
}
// Boundary represents the position in a source file of the beginning or end of a
// block as reported by the coverage profile. In HTML mode, it will correspond to
// the opening or closing of a <span> tag and will be used to colorize the source
type Boundary = cover.Boundary