Skip to content

Commit 579a96b

Browse files
committed
chore: Add linter for timeouts
1 parent f1fb0af commit 579a96b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

scripts/rules.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,45 @@ func doNotCallTFailNowInsideGoroutine(m dsl.Matcher) {
8181
Report("Do not call functions that may call t.FailNow in a goroutine, as this can cause data races (see testing.go:834)")
8282
}
8383

84+
// useStandardTimeoutsAndDelaysInTests ensures all tests use common
85+
// constants for timeouts and delays in usual scenarios, this allows us
86+
// to tweak them based on platform (important to avoid CI flakes).
87+
//nolint:unused,deadcode,varnamelen
88+
func useStandardTimeoutsAndDelaysInTests(m dsl.Matcher) {
89+
// m.Import("testing")
90+
// m.Import("context")
91+
m.Import("github.com/stretchr/testify/require")
92+
m.Import("github.com/stretchr/testify/assert")
93+
m.Import("github.com/coder/coder/internal/testutil")
94+
95+
m.Match(`context.WithTimeout($ctx, $duration)`).
96+
Where(m.File().Imports("testing") && !m["duration"].Text.Matches("^testutil\\.")).
97+
At(m["duration"]).
98+
Report("Do not use magic numbers in test timeouts and delays. Use the standard testutil.Wait* or testutil.Interval* constants instead.")
99+
100+
m.Match(`
101+
$testify.$Eventually($t, func() bool {
102+
$*_
103+
}, $timeout, $interval, $*_)
104+
`).
105+
Where((m["testify"].Text == "require" || m["testify"].Text == "assert") &&
106+
(m["Eventually"].Text == "Eventually" || m["Eventually"].Text == "Eventuallyf") &&
107+
!m["timeout"].Text.Matches("^testutil\\.")).
108+
At(m["timeout"]).
109+
Report("Do not use magic numbers in test timeouts and delays. Use the standard testutil.Wait* or testutil.Interval* constants instead.")
110+
111+
m.Match(`
112+
$testify.$Eventually($t, func() bool {
113+
$*_
114+
}, $timeout, $interval, $*_)
115+
`).
116+
Where((m["testify"].Text == "require" || m["testify"].Text == "assert") &&
117+
(m["Eventually"].Text == "Eventually" || m["Eventually"].Text == "Eventuallyf") &&
118+
!m["interval"].Text.Matches("^testutil\\.")).
119+
At(m["interval"]).
120+
Report("Do not use magic numbers in test timeouts and delays. Use the standard testutil.Wait* or testutil.Interval* constants instead.")
121+
}
122+
84123
// InTx checks to ensure the database used inside the transaction closure is the transaction
85124
// database, and not the original database that creates the tx.
86125
func InTx(m dsl.Matcher) {

0 commit comments

Comments
 (0)