checks

package
v0.0.0-...-a29b1dd Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2019 License: Apache-2.0 Imports: 19 Imported by: 0

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

Constants

This section is empty.

Variables

AllModes are all known valid modes that can be used in pre-commit-go.yml.

View Source
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

type Build struct {
	BuildAll  bool     `yaml:"build_all"`
	ExtraArgs []string `yaml:"extra_args"`
}

Build builds packages without tests via 'go build'.

func (*Build) GetDescription

func (b *Build) GetDescription() string

GetDescription implements Check.

func (*Build) GetName

func (b *Build) GetName() string

GetName implements Check.

func (*Build) GetPrerequisites

func (b *Build) GetPrerequisites() []CheckPrerequisite

GetPrerequisites implements Check.

func (*Build) Run

func (b *Build) Run(change scm.Change, options *Options) error

Run 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

type Checks map[string][]Check

Checks helps with Check serialization.

func (*Checks) UnmarshalYAML

func (c *Checks) UnmarshalYAML(unmarshal func(interface{}) error) error

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.

func New

func New(v string) *Config

New returns a default initialized Config instance.

func (*Config) EnabledChecks

func (c *Config) EnabledChecks(modes []Mode) ([]Check, *Options)

EnabledChecks returns all the checks enabled.

type Copyright struct {
	Header string
}

Copyright looks for copyright headers in all files.

func (*Copyright) GetDescription

func (c *Copyright) GetDescription() string

GetDescription implements Check.

func (*Copyright) GetName

func (c *Copyright) GetName() string

GetName implements Check.

func (*Copyright) GetPrerequisites

func (c *Copyright) GetPrerequisites() []CheckPrerequisite

GetPrerequisites implements Check.

func (*Copyright) Run

func (c *Copyright) Run(change scm.Change, options *Options) error

Run 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

func (c *Coverage) GetDescription() string

GetDescription implements Check.

func (*Coverage) GetName

func (c *Coverage) GetName() string

GetName implements Check.

func (*Coverage) GetPrerequisites

func (c *Coverage) GetPrerequisites() []CheckPrerequisite

GetPrerequisites implements Check.

func (*Coverage) Run

func (c *Coverage) Run(change scm.Change, options *Options) error

Run 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

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

func (c *Custom) GetDescription() string

GetDescription implements Check.

func (*Custom) GetName

func (c *Custom) GetName() string

GetName implements Check.

func (*Custom) GetPrerequisites

func (c *Custom) GetPrerequisites() []CheckPrerequisite

GetPrerequisites implements Check.

func (*Custom) Run

func (c *Custom) Run(change scm.Change, options *Options) error

Run implements Check.

type Errcheck

type Errcheck struct {
	Ignores string
}

Errcheck runs errcheck on packages.

func (*Errcheck) GetDescription

func (e *Errcheck) GetDescription() string

GetDescription implements Check.

func (*Errcheck) GetName

func (e *Errcheck) GetName() string

GetName implements Check.

func (*Errcheck) GetPrerequisites

func (e *Errcheck) GetPrerequisites() []CheckPrerequisite

GetPrerequisites implements Check.

func (*Errcheck) Run

func (e *Errcheck) Run(change scm.Change, options *Options) error

Run 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

func (g *Gofmt) GetDescription() string

GetDescription implements Check.

func (*Gofmt) GetName

func (g *Gofmt) GetName() string

GetName implements Check.

func (*Gofmt) GetPrerequisites

func (g *Gofmt) GetPrerequisites() []CheckPrerequisite

GetPrerequisites implements Check.

func (*Gofmt) Run

func (g *Gofmt) Run(change scm.Change, options *Options) error

Run implements Check.

type Goimports

type Goimports struct {
}

Goimports runs goimports in check mode.

func (*Goimports) GetDescription

func (g *Goimports) GetDescription() string

GetDescription implements Check.

func (*Goimports) GetName

func (g *Goimports) GetName() string

GetName implements Check.

func (*Goimports) GetPrerequisites

func (g *Goimports) GetPrerequisites() []CheckPrerequisite

GetPrerequisites implements Check.

func (*Goimports) Run

func (g *Goimports) Run(change scm.Change, options *Options) error

Run implements Check.

type Golint

type Golint struct {
	Blacklist []string
}

Golint runs golint.

func (*Golint) GetDescription

func (g *Golint) GetDescription() string

GetDescription implements Check.

func (*Golint) GetName

func (g *Golint) GetName() string

GetName implements Check.

func (*Golint) GetPrerequisites

func (g *Golint) GetPrerequisites() []CheckPrerequisite

GetPrerequisites implements Check.

func (*Golint) Run

func (g *Golint) Run(change scm.Change, options *Options) error

Run implements Check.

type Govet

type Govet struct {
	Blacklist []string
}

Govet runs "go tool vet".

func (*Govet) GetDescription

func (g *Govet) GetDescription() string

GetDescription implements Check.

func (*Govet) GetName

func (g *Govet) GetName() string

GetName implements Check.

func (*Govet) GetPrerequisites

func (g *Govet) GetPrerequisites() []CheckPrerequisite

GetPrerequisites implements Check.

func (*Govet) Run

func (g *Govet) Run(change scm.Change, options *Options) error

Run 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

func (m *Mode) UnmarshalYAML(unmarshal func(interface{}) error) error

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) Capture

func (o *Options) Capture(r scm.ReadOnlyRepo, args ...string) (string, int, time.Duration, error)

Capture sets GOPATH and executes a subprocess.

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

func (t *Test) GetDescription() string

GetDescription implements Check.

func (*Test) GetName

func (t *Test) GetName() string

GetName implements Check.

func (*Test) GetPrerequisites

func (t *Test) GetPrerequisites() []CheckPrerequisite

GetPrerequisites implements Check.

func (*Test) Run

func (t *Test) Run(change scm.Change, options *Options) error

Run implements Check.

Directories

Path Synopsis
internal
cover
Package cover provides support for parsing coverage profiles generated by "go test -coverprofile=cover.out".
Package cover provides support for parsing coverage profiles generated by "go test -coverprofile=cover.out".

Jump to

Keyboard shortcuts

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