Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Expand Up @@ -112,6 +112,6 @@ issues:
# 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"
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -1022,7 +1022,7 @@ issues:
# 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"
```
Expand Down
17 changes: 15 additions & 2 deletions pkg/commands/run.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -87,9 +89,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,
Expand Down Expand Up @@ -397,6 +400,7 @@ func (e *Executor) executeRun(_ *cobra.Command, args []string) {
}
}()

e.setTimeoutToDeadlineIfOnlyDeadlineIsSet()
ctx, cancel := context.WithTimeout(context.Background(), e.cfg.Run.Timeout)
defer cancel()

Expand All @@ -418,6 +422,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
Expand Down
36 changes: 36 additions & 0 deletions test/run_test.go
Expand Up @@ -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`")
Expand Down