Skip to content

Commit 98f60eb

Browse files
dcabajirfag
authored andcommitted
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!
1 parent f2c566b commit 98f60eb

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

.golangci.yml

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

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ issues:
10221022
# golangci.com configuration
10231023
# https://github.com/golangci/golangci/wiki/Configuration
10241024
service:
1025-
golangci-lint-version: 1.19.x # use the fixed version to not introduce new linters unexpectedly
1025+
golangci-lint-version: 1.20.x # use the fixed version to not introduce new linters unexpectedly
10261026
prepare:
10271027
- echo "here I can run custom commands, but no preparation needed for this repo"
10281028
```

pkg/commands/run.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func wh(text string) string {
5454
return color.GreenString(text)
5555
}
5656

57+
const defaultTimeout = time.Minute
58+
5759
//nolint:funlen
5860
func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, isFinalInit bool) {
5961
hideFlag := func(name string) {
@@ -87,9 +89,10 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
8789
fs.IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code",
8890
exitcodes.IssuesFound, wh("Exit code when issues were found"))
8991
fs.StringSliceVar(&rc.BuildTags, "build-tags", nil, wh("Build tags"))
90-
fs.DurationVar(&rc.Timeout, "deadline", time.Minute, wh("Deadline for total work"))
92+
93+
fs.DurationVar(&rc.Timeout, "deadline", defaultTimeout, wh("Deadline for total work"))
9194
hideFlag("deadline")
92-
fs.DurationVar(&rc.Timeout, "timeout", time.Minute, wh("Timeout for total work"))
95+
fs.DurationVar(&rc.Timeout, "timeout", defaultTimeout, wh("Timeout for total work"))
9396

9497
fs.BoolVar(&rc.AnalyzeTests, "tests", true, wh("Analyze tests (*_test.go)"))
9598
fs.BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false,
@@ -397,6 +400,7 @@ func (e *Executor) executeRun(_ *cobra.Command, args []string) {
397400
}
398401
}()
399402

403+
e.setTimeoutToDeadlineIfOnlyDeadlineIsSet()
400404
ctx, cancel := context.WithTimeout(context.Background(), e.cfg.Run.Timeout)
401405
defer cancel()
402406

@@ -418,6 +422,15 @@ func (e *Executor) executeRun(_ *cobra.Command, args []string) {
418422
e.setupExitCode(ctx)
419423
}
420424

425+
// to be removed when deadline is finally decommissioned
426+
func (e *Executor) setTimeoutToDeadlineIfOnlyDeadlineIsSet() {
427+
//lint:ignore SA1019 We want to promoted the deprecated config value when needed
428+
deadlineValue := e.cfg.Run.Deadline // nolint: staticcheck
429+
if deadlineValue != 0 && e.cfg.Run.Timeout == defaultTimeout {
430+
e.cfg.Run.Timeout = deadlineValue
431+
}
432+
}
433+
421434
func (e *Executor) setupExitCode(ctx context.Context) {
422435
if ctx.Err() != nil {
423436
e.exitCode = exitcodes.Timeout

test/run_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,42 @@ func TestTimeout(t *testing.T) {
5454
ExpectOutputContains(`Timeout exceeded: try increase it by passing --timeout option`)
5555
}
5656

57+
func TestTimeoutInConfig(t *testing.T) {
58+
type tc struct {
59+
cfg string
60+
}
61+
62+
cases := []tc{
63+
{
64+
cfg: `
65+
run:
66+
deadline: 1ms
67+
`,
68+
},
69+
{
70+
cfg: `
71+
run:
72+
timeout: 1ms
73+
`,
74+
},
75+
{
76+
// timeout should override deadline
77+
cfg: `
78+
run:
79+
deadline: 100s
80+
timeout: 1ms
81+
`,
82+
},
83+
}
84+
85+
r := testshared.NewLintRunner(t)
86+
for _, c := range cases {
87+
// Run with disallowed option set only in config
88+
r.RunWithYamlConfig(c.cfg, withCommonRunArgs(minimalPkg)...).ExpectExitCode(exitcodes.Timeout).
89+
ExpectOutputContains(`Timeout exceeded: try increase it by passing --timeout option`)
90+
}
91+
}
92+
5793
func TestTestsAreLintedByDefault(t *testing.T) {
5894
testshared.NewLintRunner(t).Run(getTestDataDir("withtests")).
5995
ExpectHasIssue("`if` block ends with a `return`")

0 commit comments

Comments
 (0)