deadline is now deprecated, but should be taking its value from the configuration if set (#822)

* test that demostrates that deadline is not working if comes from the config

* overriding timeout with deadline when only deadline is different from its default value

* tests were not passing. default value for Deadline, that now only comes from config, is 0. Plus static check is going to fail because of deprecated cfg used

* golangci should use the latest golangci-lint version, that is the one deprecating deadline in favour of timeout

* README updated - looks the ci config in this project is used to generate usage instructions.. great!
This commit is contained in:
Daniel Caballero 2019-10-15 13:11:14 +02:00 committed by Isaev Denis
parent f2c566b7e2
commit 98f60ebaa3
4 changed files with 53 additions and 4 deletions

View File

@ -112,6 +112,6 @@ issues:
# golangci.com configuration # golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration # https://github.com/golangci/golangci/wiki/Configuration
service: service:
golangci-lint-version: 1.19.x # use the fixed version to not introduce new linters unexpectedly golangci-lint-version: 1.20.x # use the fixed version to not introduce new linters unexpectedly
prepare: prepare:
- echo "here I can run custom commands, but no preparation needed for this repo" - echo "here I can run custom commands, but no preparation needed for this repo"

View File

@ -1022,7 +1022,7 @@ issues:
# golangci.com configuration # golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration # https://github.com/golangci/golangci/wiki/Configuration
service: service:
golangci-lint-version: 1.19.x # use the fixed version to not introduce new linters unexpectedly golangci-lint-version: 1.20.x # use the fixed version to not introduce new linters unexpectedly
prepare: prepare:
- echo "here I can run custom commands, but no preparation needed for this repo" - echo "here I can run custom commands, but no preparation needed for this repo"
``` ```

View File

@ -54,6 +54,8 @@ func wh(text string) string {
return color.GreenString(text) return color.GreenString(text)
} }
const defaultTimeout = time.Minute
//nolint:funlen //nolint:funlen
func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, isFinalInit bool) { func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, isFinalInit bool) {
hideFlag := func(name string) { hideFlag := func(name string) {
@ -87,9 +89,10 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
fs.IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code", fs.IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code",
exitcodes.IssuesFound, wh("Exit code when issues were found")) exitcodes.IssuesFound, wh("Exit code when issues were found"))
fs.StringSliceVar(&rc.BuildTags, "build-tags", nil, wh("Build tags")) fs.StringSliceVar(&rc.BuildTags, "build-tags", nil, wh("Build tags"))
fs.DurationVar(&rc.Timeout, "deadline", time.Minute, wh("Deadline for total work"))
fs.DurationVar(&rc.Timeout, "deadline", defaultTimeout, wh("Deadline for total work"))
hideFlag("deadline") hideFlag("deadline")
fs.DurationVar(&rc.Timeout, "timeout", time.Minute, wh("Timeout for total work")) fs.DurationVar(&rc.Timeout, "timeout", defaultTimeout, wh("Timeout for total work"))
fs.BoolVar(&rc.AnalyzeTests, "tests", true, wh("Analyze tests (*_test.go)")) fs.BoolVar(&rc.AnalyzeTests, "tests", true, wh("Analyze tests (*_test.go)"))
fs.BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false, fs.BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false,
@ -397,6 +400,7 @@ func (e *Executor) executeRun(_ *cobra.Command, args []string) {
} }
}() }()
e.setTimeoutToDeadlineIfOnlyDeadlineIsSet()
ctx, cancel := context.WithTimeout(context.Background(), e.cfg.Run.Timeout) ctx, cancel := context.WithTimeout(context.Background(), e.cfg.Run.Timeout)
defer cancel() defer cancel()
@ -418,6 +422,15 @@ func (e *Executor) executeRun(_ *cobra.Command, args []string) {
e.setupExitCode(ctx) e.setupExitCode(ctx)
} }
// to be removed when deadline is finally decommissioned
func (e *Executor) setTimeoutToDeadlineIfOnlyDeadlineIsSet() {
//lint:ignore SA1019 We want to promoted the deprecated config value when needed
deadlineValue := e.cfg.Run.Deadline // nolint: staticcheck
if deadlineValue != 0 && e.cfg.Run.Timeout == defaultTimeout {
e.cfg.Run.Timeout = deadlineValue
}
}
func (e *Executor) setupExitCode(ctx context.Context) { func (e *Executor) setupExitCode(ctx context.Context) {
if ctx.Err() != nil { if ctx.Err() != nil {
e.exitCode = exitcodes.Timeout e.exitCode = exitcodes.Timeout

View File

@ -54,6 +54,42 @@ func TestTimeout(t *testing.T) {
ExpectOutputContains(`Timeout exceeded: try increase it by passing --timeout option`) ExpectOutputContains(`Timeout exceeded: try increase it by passing --timeout option`)
} }
func TestTimeoutInConfig(t *testing.T) {
type tc struct {
cfg string
}
cases := []tc{
{
cfg: `
run:
deadline: 1ms
`,
},
{
cfg: `
run:
timeout: 1ms
`,
},
{
// timeout should override deadline
cfg: `
run:
deadline: 100s
timeout: 1ms
`,
},
}
r := testshared.NewLintRunner(t)
for _, c := range cases {
// Run with disallowed option set only in config
r.RunWithYamlConfig(c.cfg, withCommonRunArgs(minimalPkg)...).ExpectExitCode(exitcodes.Timeout).
ExpectOutputContains(`Timeout exceeded: try increase it by passing --timeout option`)
}
}
func TestTestsAreLintedByDefault(t *testing.T) { func TestTestsAreLintedByDefault(t *testing.T) {
testshared.NewLintRunner(t).Run(getTestDataDir("withtests")). testshared.NewLintRunner(t).Run(getTestDataDir("withtests")).
ExpectHasIssue("`if` block ends with a `return`") ExpectHasIssue("`if` block ends with a `return`")