Documentation ¶
Overview ¶
Package checks implements pre-made checks for pcg.
This package defines the `pre-commit-go.yml` configuration file format and implements all the checks.
Index ¶
- Variables
- func IsContinuousIntegration() bool
- func ProcessProfile(profile CoverageProfile, settings *CoverageSettings) (string, error)
- type Build
- type Check
- type CheckPrerequisite
- type Checks
- type Config
- type Copyright
- type Coverage
- func (c *Coverage) GetDescription() string
- func (c *Coverage) GetName() string
- func (c *Coverage) GetPrerequisites() []CheckPrerequisite
- func (c *Coverage) Run(change scm.Change, options *Options) error
- func (c *Coverage) RunGlobal(change scm.Change, options *Options, tmpDir string) (CoverageProfile, error)
- func (c *Coverage) RunLocal(change scm.Change, options *Options, tmpDir string) (CoverageProfile, error)
- func (c *Coverage) RunProfile(change scm.Change, options *Options) (profile CoverageProfile, err error)
- func (c *Coverage) SettingsForPkg(testPkg string) *CoverageSettings
- type CoverageProfile
- func (c CoverageProfile) CoveragePercent() float64
- func (c CoverageProfile) CoveredFuncs() int
- func (c CoverageProfile) Len() int
- func (c CoverageProfile) Less(i, j int) bool
- func (c CoverageProfile) NonCoveredFuncs() int
- func (c CoverageProfile) PartiallyCoveredFuncs() int
- func (c CoverageProfile) Passes(s *CoverageSettings) (string, bool)
- func (c CoverageProfile) Subset(p string) CoverageProfile
- func (c CoverageProfile) Swap(i, j int)
- func (c CoverageProfile) TotalCoveredLines() int
- func (c CoverageProfile) TotalLines() int
- type CoverageSettings
- type Custom
- type Errcheck
- type FuncCovered
- type Gofmt
- type Goimports
- type Golint
- type Govet
- type Mode
- type Options
- type Settings
- type Test
Constants ¶
This section is empty.
Variables ¶
var AllModes = []Mode{PreCommit, PrePush, ContinuousIntegration, Lint}
AllModes are all known valid modes that can be used in pre-commit-go.yml.
var KnownChecks = map[string]func() Check{ (&Build{}).GetName(): func() Check { return &Build{} }, (&Copyright{}).GetName(): func() Check { return &Copyright{} }, (&Coverage{}).GetName(): func() Check { return &Coverage{} }, (&Custom{}).GetName(): func() Check { return &Custom{} }, (&Errcheck{}).GetName(): func() Check { return &Errcheck{} }, (&Gofmt{}).GetName(): func() Check { return &Gofmt{} }, (&Goimports{}).GetName(): func() Check { return &Goimports{} }, (&Golint{}).GetName(): func() Check { return &Golint{} }, (&Govet{}).GetName(): func() Check { return &Govet{} }, (&Test{}).GetName(): func() Check { return &Test{} }, }
KnownChecks is the map of all known checks per check name.
Functions ¶
func IsContinuousIntegration ¶
func IsContinuousIntegration() bool
IsContinuousIntegration returns true if it thinks it's running on a known CI service.
func ProcessProfile ¶
func ProcessProfile(profile CoverageProfile, settings *CoverageSettings) (string, error)
ProcessProfile generates output that can be optionally printed and an error if the check failed.
Types ¶
type Build ¶
Build builds packages without tests via 'go build'.
func (*Build) GetDescription ¶
GetDescription implements Check.
func (*Build) GetPrerequisites ¶
func (b *Build) GetPrerequisites() []CheckPrerequisite
GetPrerequisites implements Check.
type Check ¶
type Check interface { // GetDescription returns the check description. GetDescription() string // GetName returns the check name. GetName() string // GetPrerequisites lists all the go packages to be installed before running // this check. GetPrerequisites() []CheckPrerequisite // Run executes the check. Run(change scm.Change, options *Options) error }
Check describes an check to be executed on the code base.
type CheckPrerequisite ¶
type CheckPrerequisite struct { // HelpCommand is the help command to run to detect if this prerequisite is // installed or not. This command should have no adverse effect and must be // fast to execute. HelpCommand []string `yaml:"help_command"` // ExpectedExitCode is the exit code expected when HelpCommand is executed. ExpectedExitCode int `yaml:"expected_exit_code"` // URL is the url to fetch as `go get URL`. URL string }
CheckPrerequisite describe a Go package that is needed to run a Check.
It must list a command that is to be executed and the expected exit code to verify that the custom tool is properly installed. If the executable is not detected, "go get $URL" will be executed.
func (*CheckPrerequisite) IsPresent ¶
func (c *CheckPrerequisite) IsPresent() bool
IsPresent returns true if the prerequisite is present on the system.
type Checks ¶
Checks helps with Check serialization.
func (*Checks) UnmarshalYAML ¶
UnmarshalYAML implements yaml.Unmarshaler.
type Config ¶
type Config struct { // MinVersion is set to the current pcg version. Earlier version will refuse // to load this file. MinVersion string `yaml:"min_version"` // Settings per mode. Settings includes the checks and the maximum allowed // time spent to run them. Modes map[Mode]Settings `yaml:"modes"` // IgnorePatterns is all paths glob patterns that should be ignored. By // default, this include any file or directory starting with "." or "_", i.e. // []string{".*", "_*"}. This is a glob that is applied to each path // component of each file. IgnorePatterns []string `yaml:"ignore_patterns"` // MaxConcurrent, if not zero, is the maximum number of concurrent processes // to run. If zero, there is no maximum. MaxConcurrent int `yaml:"-"` }
Config is the serialized form of pre-commit-go.yml.
type Copyright ¶
type Copyright struct {
Header string
}
Copyright looks for copyright headers in all files.
func (*Copyright) GetDescription ¶
GetDescription implements Check.
func (*Copyright) GetPrerequisites ¶
func (c *Copyright) GetPrerequisites() []CheckPrerequisite
GetPrerequisites implements Check.
type Coverage ¶
type Coverage struct { UseGlobalInference bool `yaml:"use_global_inference"` UseCoveralls bool `yaml:"use_coveralls"` Global CoverageSettings `yaml:"global"` PerDirDefault CoverageSettings `yaml:"per_dir_default"` PerDir map[string]*CoverageSettings `yaml:"per_dir"` IgnorePathPatterns []string `yaml:"ignore_path_patterns"` }
Coverage runs all tests with coverage.
func (*Coverage) GetDescription ¶
GetDescription implements Check.
func (*Coverage) GetPrerequisites ¶
func (c *Coverage) GetPrerequisites() []CheckPrerequisite
GetPrerequisites implements Check.
func (*Coverage) RunGlobal ¶
func (c *Coverage) RunGlobal(change scm.Change, options *Options, tmpDir string) (CoverageProfile, error)
RunGlobal runs the tests under coverage with global inference.
This means that test can contribute coverage in any other package, even outside their own package.
func (*Coverage) RunLocal ¶
func (c *Coverage) RunLocal(change scm.Change, options *Options, tmpDir string) (CoverageProfile, error)
RunLocal runs all tests and reports the merged coverage of each individual covered package.
func (*Coverage) RunProfile ¶
func (c *Coverage) RunProfile(change scm.Change, options *Options) (profile CoverageProfile, err error)
RunProfile runs a coverage run according to the settings and return results.
func (*Coverage) SettingsForPkg ¶
func (c *Coverage) SettingsForPkg(testPkg string) *CoverageSettings
SettingsForPkg returns the settings for a particular package.
If the PerDir value is set to a null pointer, returns empty coverage. Otherwise returns PerDirDefault.
type CoverageProfile ¶
type CoverageProfile []*FuncCovered
CoverageProfile is the processed results of a coverage run.
func (CoverageProfile) CoveragePercent ¶
func (c CoverageProfile) CoveragePercent() float64
CoveragePercent returns the coverage in % for this profile.
func (CoverageProfile) CoveredFuncs ¶
func (c CoverageProfile) CoveredFuncs() int
CoveredFuncs returns the number of functions completely covered.
func (CoverageProfile) Len ¶
func (c CoverageProfile) Len() int
func (CoverageProfile) Less ¶
func (c CoverageProfile) Less(i, j int) bool
func (CoverageProfile) NonCoveredFuncs ¶
func (c CoverageProfile) NonCoveredFuncs() int
NonCoveredFuncs returns the number of functions not covered.
func (CoverageProfile) PartiallyCoveredFuncs ¶
func (c CoverageProfile) PartiallyCoveredFuncs() int
PartiallyCoveredFuncs returns the number of functions partially covered.
func (CoverageProfile) Passes ¶
func (c CoverageProfile) Passes(s *CoverageSettings) (string, bool)
Passes returns a summary as if it passes the settings and true if it passes.
func (CoverageProfile) Subset ¶
func (c CoverageProfile) Subset(p string) CoverageProfile
Subset returns a new CoverageProfile that only covers the specified directory.
func (CoverageProfile) Swap ¶
func (c CoverageProfile) Swap(i, j int)
func (CoverageProfile) TotalCoveredLines ¶
func (c CoverageProfile) TotalCoveredLines() int
TotalCoveredLines returns the number of lines that were covered.
func (CoverageProfile) TotalLines ¶
func (c CoverageProfile) TotalLines() int
TotalLines returns the total number of source lines found.
type CoverageSettings ¶
type CoverageSettings struct { MinCoverage float64 `yaml:"min_coverage"` MaxCoverage float64 `yaml:"max_coverage"` }
CoverageSettings specifies coverage settings.
type Custom ¶
type Custom struct { // DisplayName is check's display name, required. DisplayName string `yaml:"display_name"` // Description is check's description, optional. Description string `yaml:"description"` // Command is check's command line, required. Command []string `yaml:"command"` // CheckExitCode specifies if the check is declared to fail when exit code is // non-zero. CheckExitCode bool `yaml:"check_exit_code"` // Prerequisites are check's prerequisite packages to install first before // running the check, optional. Prerequisites []CheckPrerequisite `yaml:"prerequisites"` }
Custom represents a user configured check running an external program.
It can be used multiple times to run multiple external checks.
func (*Custom) GetDescription ¶
GetDescription implements Check.
func (*Custom) GetPrerequisites ¶
func (c *Custom) GetPrerequisites() []CheckPrerequisite
GetPrerequisites implements Check.
type Errcheck ¶
type Errcheck struct {
Ignores string
}
Errcheck runs errcheck on packages.
func (*Errcheck) GetDescription ¶
GetDescription implements Check.
func (*Errcheck) GetPrerequisites ¶
func (e *Errcheck) GetPrerequisites() []CheckPrerequisite
GetPrerequisites implements Check.
type FuncCovered ¶
type FuncCovered struct { Source string Line int SourceRef string Name string Covered int Missing []int Total int Percent float64 }
FuncCovered is the summary of a function covered.
type Gofmt ¶
type Gofmt struct { }
Gofmt runs gofmt in check mode with code simplification enabled.
func (*Gofmt) GetDescription ¶
GetDescription implements Check.
func (*Gofmt) GetPrerequisites ¶
func (g *Gofmt) GetPrerequisites() []CheckPrerequisite
GetPrerequisites implements Check.
type Goimports ¶
type Goimports struct { }
Goimports runs goimports in check mode.
func (*Goimports) GetDescription ¶
GetDescription implements Check.
func (*Goimports) GetPrerequisites ¶
func (g *Goimports) GetPrerequisites() []CheckPrerequisite
GetPrerequisites implements Check.
type Golint ¶
type Golint struct {
Blacklist []string
}
Golint runs golint.
func (*Golint) GetDescription ¶
GetDescription implements Check.
func (*Golint) GetPrerequisites ¶
func (g *Golint) GetPrerequisites() []CheckPrerequisite
GetPrerequisites implements Check.
type Govet ¶
type Govet struct {
Blacklist []string
}
Govet runs "go tool vet".
func (*Govet) GetDescription ¶
GetDescription implements Check.
func (*Govet) GetPrerequisites ¶
func (g *Govet) GetPrerequisites() []CheckPrerequisite
GetPrerequisites implements Check.
type Mode ¶
type Mode string
Mode is one of the check mode. When running checks, the mode determine what checks are executed.
const ( PreCommit Mode = "pre-commit" PrePush Mode = "pre-push" ContinuousIntegration Mode = "continuous-integration" Lint Mode = "lint" )
All predefined modes are executed automatically based on the context, except for Lint which needs to be selected manually.
func (*Mode) UnmarshalYAML ¶
UnmarshalYAML implements yaml.Unmarshaler.
type Options ¶
type Options struct { // MaxDuration is the maximum allowed duration to run all the checks in // seconds. If it takes more time than that, it is marked as failed. MaxDuration int `yaml:"max_duration"` // contains filtered or unexported fields }
Options hold the settings for a mode shared by all checks.
func (*Options) LeaseRunToken ¶
func (o *Options) LeaseRunToken()
LeaseRunToken returns a leased run token.
A token must be returned after use via ReturnRunToken. This should be done via defer, as failure to return a run token will result in throttling or deadlock.
func (*Options) ReturnRunToken ¶
func (o *Options) ReturnRunToken()
ReturnRunToken returns a leased run token.
type Settings ¶
type Settings struct { // Checks is a map of all checks enabled for this mode, with the key being // the check type. Checks Checks `yaml:"checks"` Options Options `yaml:",inline"` }
Settings is the settings used for a mode.
type Test ¶
type Test struct {
ExtraArgs []string `yaml:"extra_args"`
}
Test runs all tests via go test.
func (*Test) GetDescription ¶
GetDescription implements Check.
func (*Test) GetPrerequisites ¶
func (t *Test) GetPrerequisites() []CheckPrerequisite
GetPrerequisites implements Check.