coverparser

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoProfiles is returned by [Parser.Parse] if profile contains no data.
	ErrNoProfiles = errors.New("cover profile contains no coverage data")

	// ErrNoPackages is returned by [Parser.Parse] if profile contains no package data.
	ErrNoPackages = errors.New("cover profile contains no package coverage data")
)

Functions

This section is empty.

Types

type Mode

type Mode string

Mode of a coverage report.

const (
	ModeSet    Mode = "set"    // does this statement run?
	ModeCount  Mode = "count"  // how many times does this statement run?
	ModeAtomic Mode = "atomic" // like count, but correct in multithreaded tests.
)

Coverage modes supported by Go.

type Option

type Option func(*Parser)

Option configures a Parser.

func WithGoBinPath

func WithGoBinPath(goBinPath string) Option

WithGoBinPath configures Parser to use path as go binary for commands.

By default, `go` is used.

func WithLogger

func WithLogger(logger *log.Logger) Option

WithLogger configures Parser with a logger.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses Go test coverage profile data.

func New

func New(ctx context.Context, rootDir string, opts ...Option) *Parser

New parser for Go test coverage profile data.

func (*Parser) Parse

func (p *Parser) Parse(coverprofile io.Reader) (*Report, error)

Parse test coverage data from Go coverprofile data.

Parses the content of a file created by the `go test -coverprofile=...` command and returns a report with coverage data and contents of tested files.

Uses pkg.go.dev/golang.org/x/tools/cover under the hood to parse the coverprofile data.

type Profile

type Profile struct {
	FileName   string            `json:"filename"`   // Name of file.
	Package    string            `json:"package"`    // Package where file belongs.
	Path       string            `json:"path"`       // Absolute path to file.
	Content    []byte            `json:"content"`    // Content of file at time of testing.
	Size       int               `json:"size"`       // File size.
	Coverage   float64           `json:"coverage"`   // Coverage percentage of file.
	LineCount  int               `json:"lineCount"`  // Number of lines in file.
	Boundaries []ProfileBoundary `json:"boundaries"` // Boundaries map coverage of the content.
}

Profile of a single file in coverage report.

type ProfileBoundary

type ProfileBoundary struct {
	Offset int     `json:"offset"` // Location as a byte offset in the content.
	Start  bool    `json:"start"`  // Is this the start of a block?
	Count  int     `json:"count"`  // Amount of times block was invoked in tests.
	Norm   float64 `json:"norm"`   // Coverage normalized to [0..1].
	Index  int     `json:"index"`  // Order in content.
}

ProfileBoundary represents the position in a source file of the beginning and end of a block as reported by the coverage profile.

User interfaces can use this to mark coverage of a file, e.g. by wrapping file content in HTML tags between start and end offsets.

type Report

type Report struct {
	Mode     Mode      `json:"mode"`
	Profiles []Profile `json:"profiles"`
	Time     time.Time `json:"time"`
}

Report of code coverage in a test run.

Holds coverage profile data and contents of tested files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL