cover

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2021 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//CoverInitSystemAPI prepare a new round of testing
	CoverInitSystemAPI = "/v1/cover/init"
	//CoverProfileAPI is provided by the covered service to get profiles
	CoverProfileAPI = "/v1/cover/profile"
	//CoverProfileClearAPI is provided by the covered service to clear profiles
	CoverProfileClearAPI = "/v1/cover/clear"
	//CoverServicesListAPI list all the registered services
	CoverServicesListAPI = "/v1/cover/list"
	//CoverRegisterServiceAPI register a service into service center
	CoverRegisterServiceAPI = "/v1/cover/register"
	//CoverServicesRemoveAPI remove one services from the service center
	CoverServicesRemoveAPI = "/v1/cover/remove"
)
View Source
const LogFile = "goc.log"

LogFile a file to save log.

Variables

View Source
var (
	// ErrCoverPkgFailed represents the error that fails to inject the package
	ErrCoverPkgFailed = errors.New("fail to inject code to project")
	// ErrCoverListFailed represents the error that fails to list package dependencies
	ErrCoverListFailed = errors.New("fail to list package dependencies")
)
View Source
var ErrServiceAlreadyRegistered = errors.New("service already registered")

Functions

func Delta

func Delta(new Coverage, base Coverage) float32

Delta calculate two coverage delta

func Execute

func Execute(coverInfo *CoverInfo) error

Execute inject cover variables for all the .go files in the target folder

func InjectCacheCounters

func InjectCacheCounters(covers map[string][]*PackageCover, cache map[string]*PackageCover) []error

func InjectCountersHandlers

func InjectCountersHandlers(tc TestCover, dest string) error

InjectCountersHandlers generate a file _cover_http_apis.go besides the main.go file

func ListPackages

func ListPackages(dir string, args string, newgopath string) (map[string]*Package, error)

ListPackages list all packages under specific via go list command The argument newgopath is if you need to go list in a different GOPATH

func NewFileBasedServer

func NewFileBasedServer(persistenceFile string) (*server, error)

NewFileBasedServer new a file based server with persistenceFile

func NewMemoryBasedServer

func NewMemoryBasedServer() *server

NewMemoryBasedServer new a memory based server without persistenceFile

func PercentStr

func PercentStr(f float32) string

PercentStr converts a fraction number to percentage string representation

func TotalDelta

func TotalDelta(new CoverageList, base CoverageList) float32

TotalDelta calculate two coverage delta

Types

type Action

type Action interface {
	Profile(param ProfileParam) ([]byte, error)
	Clear(param ProfileParam) ([]byte, error)
	Remove(param ProfileParam) ([]byte, error)
	InitSystem() ([]byte, error)
	ListServices() ([]byte, error)
	RegisterService(svr ServiceUnderTest) ([]byte, error)
}

Action provides methods to contact with the covered service under test

func NewWorker

func NewWorker(host string) Action

NewWorker creates a worker to contact with service

type CoverInfo

type CoverInfo struct {
	Target                   string
	GoPath                   string
	IsMod                    bool
	ModRootPath              string
	GlobalCoverVarImportPath string // path for the injected global cover var file
	OneMainPackage           bool
	Args                     string
	Mode                     string
	AgentPort                string
	Center                   string
	Singleton                bool
}

CoverBuildInfo retreives some info from build

type Coverage

type Coverage struct {
	FileName      string
	NCoveredStmts int
	NAllStmts     int
	LineCovLink   string
}

Coverage stores test coverage summary data for one file

func (*Coverage) Name

func (c *Coverage) Name() string

Name returns the file name

func (*Coverage) Percentage

func (c *Coverage) Percentage() string

Percentage returns the percentage of statements covered

func (*Coverage) Ratio

func (c *Coverage) Ratio() (ratio float32, err error)

Ratio calculates the ratio of statements in a profile

type CoverageList

type CoverageList []Coverage

CoverageList is a collection and summary over multiple file Coverage objects

func CovList

func CovList(f io.Reader) (g CoverageList, err error)

CovList converts profile to CoverageList struct

func NewCoverageList

func NewCoverageList() CoverageList

NewCoverageList return empty CoverageList

func ReadFileToCoverList

func ReadFileToCoverList(path string) (g CoverageList, err error)

ReadFileToCoverList coverts profile file to CoverageList struct

func (CoverageList) Map

func (g CoverageList) Map() map[string]Coverage

Map returns maps the file name to its coverage for faster retrieval & membership check

func (CoverageList) Sort

func (g CoverageList) Sort()

Sort sorts CoverageList with filenames

func (CoverageList) TotalPercentage

func (g CoverageList) TotalPercentage() string

TotalPercentage returns the total percentage of coverage

func (CoverageList) TotalRatio

func (g CoverageList) TotalRatio() (ratio float32, err error)

TotalRatio returns the total ratio of covered statements

type DeltaCov

type DeltaCov struct {
	FileName    string
	BasePer     string
	NewPer      string
	DeltaPer    string
	LineCovLink string
}

DeltaCov contains the info of a delta coverage

func (c *DeltaCov) GetLineCovLink() string

GetLineCovLink get the LineCovLink of the DeltaCov

func (*DeltaCov) Name

func (c *DeltaCov) Name() string

Name returns the file name

func (c *DeltaCov) SetLineCovLink(link string)

SetLineCovLink set LineCovLink of the DeltaCov

type DeltaCovList

type DeltaCovList []DeltaCov

DeltaCovList is the list of DeltaCov

func GetChFileDeltaCov

func GetChFileDeltaCov(newList CoverageList, baseList CoverageList, changedFiles []string) (list DeltaCovList)

GetChFileDeltaCov get two profile diff cov of changed files

func GetDeltaCov

func GetDeltaCov(newList CoverageList, baseList CoverageList) (delta DeltaCovList)

GetDeltaCov get two profile diff cov

func GetFullDeltaCov

func GetFullDeltaCov(newList CoverageList, baseList CoverageList) (delta DeltaCovList)

GetFullDeltaCov get full delta coverage between new and base profile

func (DeltaCovList) Map

func (d DeltaCovList) Map() map[string]DeltaCov

Map returns maps the file name to its DeltaCov for faster retrieval & membership check

func (DeltaCovList) Sort

func (d DeltaCovList) Sort()

Sort sort DeltaCovList with filenames

type FileVar

type FileVar struct {
	File string
	Var  string
}

FileVar holds the name of the generated coverage variables targeting the named file.

type ModuleError

type ModuleError struct {
	Err string // error text
}

ModuleError represents the error loading module

type ModulePublic

type ModulePublic struct {
	Path      string        `json:",omitempty"` // module path
	Version   string        `json:",omitempty"` // module version
	Versions  []string      `json:",omitempty"` // available module versions
	Replace   *ModulePublic `json:",omitempty"` // replaced by this module
	Time      *time.Time    `json:",omitempty"` // time version was created
	Update    *ModulePublic `json:",omitempty"` // available update (with -u)
	Main      bool          `json:",omitempty"` // is this the main module?
	Indirect  bool          `json:",omitempty"` // module is only indirectly needed by main module
	Dir       string        `json:",omitempty"` // directory holding local copy of files, if any
	GoMod     string        `json:",omitempty"` // path to go.mod file describing module, if any
	GoVersion string        `json:",omitempty"` // go version used in module
	Error     *ModuleError  `json:",omitempty"` // error loading module
}

ModulePublic represents the package info of a module

type Package

type Package struct {
	Dir        string `json:"Dir"`        // directory containing package sources
	ImportPath string `json:"ImportPath"` // import path of package in dir
	Name       string `json:"Name"`       // package name
	Target     string `json:",omitempty"` // installed target for this package (may be executable)
	Root       string `json:",omitempty"` // Go root, Go path dir, or module root dir containing this package

	Module   *ModulePublic `json:",omitempty"`         // info about package's module, if any
	Goroot   bool          `json:"Goroot,omitempty"`   // is this package in the Go root?
	Standard bool          `json:"Standard,omitempty"` // is this package part of the standard Go library?
	DepOnly  bool          `json:"DepOnly,omitempty"`  // package is only a dependency, not explicitly listed

	// Source files
	GoFiles  []string `json:"GoFiles,omitempty"`  // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
	CgoFiles []string `json:"CgoFiles,omitempty"` // .go source files that import "C"

	// Dependency information
	Deps      []string          `json:"Deps,omitempty"` // all (recursively) imported dependencies
	Imports   []string          `json:",omitempty"`     // import paths used by this package
	ImportMap map[string]string `json:",omitempty"`     // map from source import to ImportPath (identity entries omitted)

	// Error information
	Incomplete bool            `json:"Incomplete,omitempty"` // this package or a dependency has an error
	Error      *PackageError   `json:"Error,omitempty"`      // error loading package
	DepsErrors []*PackageError `json:"DepsErrors,omitempty"` // errors loading dependencies
}

Package map a package output by go list this is subset of package struct in: https://github.com/golang/go/blob/master/src/cmd/go/internal/load/pkg.go#L58

type PackageCover

type PackageCover struct {
	Package *Package
	Vars    map[string]*FileVar
}

PackageCover holds all the generate coverage variables of a package

func AddCounters

func AddCounters(pkg *Package, mode string, globalCoverVarImportPath string) (*PackageCover, string)

AddCounters is different from official go tool cover 1. only inject covervar++ into source file 2. no declarartions for these covervars 3. return the declarations as string

type PackageError

type PackageError struct {
	ImportStack []string // shortest path from package named on command line to this one
	Pos         string   // position of error (if present, file:line:col)
	Err         string   // the error itself
}

PackageError is the error info for a package when list failed

type ProfileParam

type ProfileParam struct {
	Force             bool     `form:"force" json:"force"`
	Service           []string `form:"service" json:"service"`
	Address           []string `form:"address" json:"address"`
	CoverFilePatterns []string `form:"coverfile" json:"coverfile"`
	SkipFilePatterns  []string `form:"skipfile" json:"skipfile"`
}

ProfileParam is param of profile API

type ServiceUnderTest

type ServiceUnderTest struct {
	Name    string `form:"name" json:"name" binding:"required"`
	Address string `form:"address" json:"address" binding:"required"`
}

ServiceUnderTest is a entry under being tested

type Store

type Store interface {
	// Add adds the given service to store
	Add(s ServiceUnderTest) error

	// Get returns the registered service information with the given service's name
	Get(name string) []string

	// Get returns all the registered service information as a map
	GetAll() map[string][]string

	// Init cleanup all the registered service information
	Init() error

	// Set stores the services information into internal state
	Set(services map[string][]string) error

	// Remove the service from the store by address
	Remove(addr string) error
}

Store persistents the registered service information

func NewFileStore

func NewFileStore(persistenceFile string) (store Store, err error)

NewFileStore creates a store using local file

func NewMemoryStore

func NewMemoryStore() Store

NewMemoryStore creates a memory store

type TestCover

type TestCover struct {
	Mode                     string
	AgentPort                string
	Center                   string // cover profile host center
	Singleton                bool
	MainPkgCover             *PackageCover
	DepsCover                []*PackageCover
	CacheCover               map[string]*PackageCover
	GlobalCoverVarImportPath string
}

TestCover is a collection of all counters

Directories

Path Synopsis
internal
tool
Package edit implements buffered position-based editing of byte slices.
Package edit implements buffered position-based editing of byte slices.

Jump to

Keyboard shortcuts

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