From 27c8be08f84ea0a5d9623994471b4c805430a10f Mon Sep 17 00:00:00 2001 From: Daniel Caballero Date: Tue, 15 Oct 2019 13:11:14 +0200 Subject: [PATCH] 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! --- .golangci.yml | 2 +- README.md | 2 +- pkg/commands/run.go | 17 +++++++++++++++-- test/run_test.go | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index a46c27ed..0e8feb5e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -99,6 +99,6 @@ run: # golangci.com configuration # https://github.com/golangci/golangci/wiki/Configuration 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: - echo "here I can run custom commands, but no preparation needed for this repo" diff --git a/README.md b/README.md index 7241958b..97af57b4 100644 --- a/README.md +++ b/README.md @@ -996,7 +996,7 @@ run: # golangci.com configuration # https://github.com/golangci/golangci/wiki/Configuration 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: - echo "here I can run custom commands, but no preparation needed for this repo" ``` diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 7e5f429d..a39d79c6 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -54,6 +54,8 @@ func wh(text string) string { return color.GreenString(text) } +const defaultTimeout = time.Minute + //nolint:funlen func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, isFinalInit bool) { hideFlag := func(name string) { @@ -85,9 +87,10 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is fs.IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code", exitcodes.IssuesFound, wh("Exit code when issues were found")) 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") - 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.PrintResourcesUsage, "print-resources-usage", false, @@ -390,6 +393,7 @@ func (e *Executor) executeRun(_ *cobra.Command, args []string) { } }() + e.setTimeoutToDeadlineIfOnlyDeadlineIsSet() ctx, cancel := context.WithTimeout(context.Background(), e.cfg.Run.Timeout) defer cancel() @@ -411,6 +415,15 @@ func (e *Executor) executeRun(_ *cobra.Command, args []string) { 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) { if ctx.Err() != nil { e.exitCode = exitcodes.Timeout diff --git a/test/run_test.go b/test/run_test.go index 4c4cf71a..f9f40e20 100644 --- a/test/run_test.go +++ b/test/run_test.go @@ -54,6 +54,42 @@ func TestTimeout(t *testing.T) { 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) { testshared.NewLintRunner(t).Run(getTestDataDir("withtests")). ExpectHasIssue("`if` block ends with a `return`")