Skip to content

Commit bfe5158

Browse files
authored
chore: relinted code base (#99)
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 5173717 commit bfe5158

8 files changed

Lines changed: 64 additions & 38 deletions

File tree

internal/assertions/condition_test.go

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,25 @@ func TestConditionEventuallyNoLeak(t *testing.T) {
295295
})
296296
}
297297

298-
//nolint:gocognit,gocyclo,cyclop,maintidx // subtests are actually not complex
299298
func TestConditionEventuallyWith(t *testing.T) {
300299
t.Parallel()
301300

302-
t.Run("should complete with false", func(t *testing.T) {
301+
t.Run("should complete with false", testEventuallyWithShouldCompleteWithFalse())
302+
t.Run("should complete with true", testEventuallyWithShouldCompleteWithTrue())
303+
t.Run("should complete with fail, on a nanosecond tick", testEventuallyWithShouldCompleteWithFail())
304+
t.Run("should complete with fail, with latest failed condition", testEventuallyWithShouldCompleteWithFailLatest())
305+
t.Run("should complete with success, with the ticker never used", testEventuallyWithShouldCompleteWithSuccess())
306+
t.Run("collect.FailNow only fails the current tick (poller retries)", testEventuallyWithFailNowRetries())
307+
t.Run("collect.FailNow allows convergence on a later tick", testEventuallyWithFailNowConverges())
308+
t.Run("collect.Cancel aborts the whole assertion immediately", testEventuallyWithCancelAborts())
309+
t.Run("collect.Cancelf aborts with a custom message", testEventuallyWithCancelfAborts())
310+
}
311+
312+
// =====================================================
313+
// Sub tests EventuallyWith
314+
// =====================================================.
315+
func testEventuallyWithShouldCompleteWithFalse() func(*testing.T) {
316+
return func(t *testing.T) {
303317
t.Parallel()
304318

305319
mock := new(errorsCapturingT)
@@ -323,9 +337,11 @@ func TestConditionEventuallyWith(t *testing.T) {
323337
if counter < expectedCalls-1 || counter > expectedCalls+1 { // it may be 4, 5 or 6 depending on how the test schedules
324338
t.Errorf("expected %d calls to the condition, but got %d", expectedCalls, counter)
325339
}
326-
})
340+
}
341+
}
327342

328-
t.Run("should complete with true", func(t *testing.T) {
343+
func testEventuallyWithShouldCompleteWithTrue() func(*testing.T) {
344+
return func(t *testing.T) {
329345
t.Parallel()
330346

331347
mock := new(errorsCapturingT)
@@ -345,9 +361,11 @@ func TestConditionEventuallyWith(t *testing.T) {
345361
if expectedCalls != counter {
346362
t.Errorf("expected condition to be called %d times, got %d", expectedCalls, counter)
347363
}
348-
})
364+
}
365+
}
349366

350-
t.Run("should complete with fail, on a nanosecond tick", func(t *testing.T) {
367+
func testEventuallyWithShouldCompleteWithFail() func(*testing.T) {
368+
return func(t *testing.T) {
351369
t.Parallel()
352370

353371
mock := new(errorsCapturingT)
@@ -363,9 +381,11 @@ func TestConditionEventuallyWith(t *testing.T) {
363381
if len(mock.errors) != expectedErrors {
364382
t.Errorf("expected %d errors (1 from condition, 2 from Eventually), got %d", expectedErrors, len(mock.errors))
365383
}
366-
})
384+
}
385+
}
367386

368-
t.Run("should complete with fail, with latest failed condition", func(t *testing.T) {
387+
func testEventuallyWithShouldCompleteWithFailLatest() func(*testing.T) {
388+
return func(t *testing.T) {
369389
t.Parallel()
370390

371391
mock := new(errorsCapturingT)
@@ -393,9 +413,11 @@ func TestConditionEventuallyWith(t *testing.T) {
393413
if len(mock.errors) != expectedErrors {
394414
t.Errorf("expected %d errors (1 from condition, 2 from Eventually), got %d", expectedErrors, len(mock.errors))
395415
}
396-
})
416+
}
417+
}
397418

398-
t.Run("should complete with success, with the ticker never used", func(t *testing.T) {
419+
func testEventuallyWithShouldCompleteWithSuccess() func(*testing.T) {
420+
return func(t *testing.T) {
399421
t.Parallel()
400422

401423
mock := new(errorsCapturingT)
@@ -406,9 +428,11 @@ func TestConditionEventuallyWith(t *testing.T) {
406428
if !EventuallyWith(mock, condition, testTimeout, time.Second) {
407429
t.Error("expected EventuallyWith to return true")
408430
}
409-
})
431+
}
432+
}
410433

411-
t.Run("collect.FailNow only fails the current tick (poller retries)", func(t *testing.T) {
434+
func testEventuallyWithFailNowRetries() func(*testing.T) {
435+
return func(t *testing.T) {
412436
t.Parallel()
413437

414438
mock := new(errorsCapturingT)
@@ -432,9 +456,11 @@ func TestConditionEventuallyWith(t *testing.T) {
432456
if got < 2 {
433457
t.Errorf("expected the condition to be retried multiple times, got %d call(s)", got)
434458
}
435-
})
459+
}
460+
}
436461

437-
t.Run("collect.FailNow allows convergence on a later tick", func(t *testing.T) {
462+
func testEventuallyWithFailNowConverges() func(*testing.T) {
463+
return func(t *testing.T) {
438464
t.Parallel()
439465

440466
mock := new(errorsCapturingT)
@@ -458,9 +484,11 @@ func TestConditionEventuallyWith(t *testing.T) {
458484
if len(mock.errors) != 0 {
459485
t.Errorf("expected no errors reported on parent t after success, got %d: %v", len(mock.errors), mock.errors)
460486
}
461-
})
487+
}
488+
}
462489

463-
t.Run("collect.Cancel aborts the whole assertion immediately", func(t *testing.T) {
490+
func testEventuallyWithCancelAborts() func(*testing.T) {
491+
return func(t *testing.T) {
464492
t.Parallel()
465493

466494
mock := new(errorsCapturingT)
@@ -491,9 +519,11 @@ func TestConditionEventuallyWith(t *testing.T) {
491519
if len(mock.errors) == 0 {
492520
t.Error("expected at least one error reported on parent t after Cancel")
493521
}
494-
})
522+
}
523+
}
495524

496-
t.Run("collect.Cancelf aborts with a custom message", func(t *testing.T) {
525+
func testEventuallyWithCancelfAborts() func(*testing.T) {
526+
return func(t *testing.T) {
497527
t.Parallel()
498528

499529
mock := new(errorsCapturingT)
@@ -532,7 +562,7 @@ func TestConditionEventuallyWith(t *testing.T) {
532562
if !foundCustom {
533563
t.Errorf("expected custom Cancelf message in errors, got: %v", mock.errors)
534564
}
535-
})
565+
}
536566
}
537567

538568
func TestConditionPollUntilTimeout(t *testing.T) {

internal/assertions/http_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,9 @@ func httpFailCases() iter.Seq[failCase] {
252252
// ============================================================================
253253

254254
func httpHelloName(w http.ResponseWriter, r *http.Request) {
255+
r.Body = http.MaxBytesReader(w, r.Body, 100)
255256
name := r.FormValue("name")
256-
_, _ = fmt.Fprintf(w, "Hello, %s!", name) //nolint:gosec // gosec false positive: G705: XSS via taint analysis
257+
_, _ = fmt.Fprintf(w, "Hello, %s!", name) //nolint:gosec // G705: XSS via taint analysis but okay in tests
257258
}
258259

259260
func httpOK(w http.ResponseWriter, _ *http.Request) {

internal/assertions/safety.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/go-openapi/testify/v2/internal/leak"
1212
)
1313

14+
const linuxOS = "linux"
15+
1416
// NoGoRoutineLeak ensures that no goroutine did leak from inside the tested function.
1517
//
1618
// NOTE: only the go routines spawned from inside the tested function are checked for leaks.
@@ -101,7 +103,7 @@ func NoFileDescriptorLeak(t T, tested func(), msgAndArgs ...any) bool {
101103
h.Helper()
102104
}
103105

104-
if runtime.GOOS != "linux" { //nolint:goconst // well-known runtime value
106+
if runtime.GOOS != linuxOS {
105107
if s, ok := t.(skipper); ok {
106108
s.Skip("NoFileDescriptorLeak requires Linux (/proc/self/fd)")
107109
}

internal/assertions/safety_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestNoFileDescriptorLeak_Success(t *testing.T) {
7070
}
7171

7272
func TestNoFileDescriptorLeak_Failure(t *testing.T) {
73-
if runtime.GOOS != "linux" {
73+
if runtime.GOOS != linuxOS {
7474
t.Skip("file descriptor leak detection requires Linux")
7575
}
7676

@@ -103,7 +103,7 @@ func TestNoFileDescriptorLeak_Failure(t *testing.T) {
103103
}
104104

105105
func TestNoFileDescriptorLeak_SocketFiltered(t *testing.T) {
106-
if runtime.GOOS != "linux" {
106+
if runtime.GOOS != linuxOS {
107107
t.Skip("file descriptor leak detection requires Linux")
108108
}
109109

internal/fdleak/fdleak_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestLeaked_WithLeak(t *testing.T) {
6767
t.Cleanup(func() {
6868
if leakedFile != nil {
6969
leakedFile.Close()
70-
os.Remove(leakedFile.Name()) //nolint:gosec // G703 path traversal is ok: this is a test.
70+
os.Remove(leakedFile.Name())
7171
}
7272
})
7373

internal/leak/leak_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,9 @@ func TestLeaked_PreExistingGoroutineNotLabeled(t *testing.T) {
154154
})
155155

156156
// Start a goroutine before the instrumented call.
157-
wg.Add(1)
158-
go func() {
159-
defer wg.Done()
157+
wg.Go(func() {
160158
<-blocker
161-
}()
159+
})
162160

163161
tested := func() {
164162
// Does nothing — the pre-existing goroutine is not ours.
@@ -178,10 +176,7 @@ func TestLeaked_ParallelIsolation(t *testing.T) {
178176
results := make([]string, workers)
179177

180178
for i := range workers {
181-
wg.Add(1)
182-
go func() {
183-
defer wg.Done()
184-
179+
wg.Go(func() {
185180
blocker := make(chan struct{})
186181
tested := func() {
187182
go func() {
@@ -191,7 +186,7 @@ func TestLeaked_ParallelIsolation(t *testing.T) {
191186

192187
results[i] = Leaked(t.Context(), tested)
193188
close(blocker) // clean up
194-
}()
189+
})
195190
}
196191

197192
wg.Wait()

internal/spew/spew_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,5 +382,5 @@ func redirStdout(f func()) ([]byte, error) {
382382
os.Stdout = origStdout
383383
tempFile.Close()
384384

385-
return os.ReadFile(fileName) //nolint:gosec // false positive: G703: Path traversal via taint analysis
385+
return os.ReadFile(fileName)
386386
}

internal/testintegration/spew/generator.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ func NoPanicProp(ctx context.Context, g *rapid.Generator[any]) func(*rapid.T) {
4343
var w sync.WaitGroup
4444
done := make(chan struct{})
4545

46-
w.Add(1)
47-
go func() {
48-
defer w.Done()
46+
w.Go(func() {
4947
select {
5048
case <-done:
5149
cancel()
@@ -56,7 +54,7 @@ func NoPanicProp(ctx context.Context, g *rapid.Generator[any]) func(*rapid.T) {
5654

5755
return
5856
}
59-
}()
57+
})
6058

6159
go func() { // this go routine may leak if timeout kicks
6260
// Sdump should never panic

0 commit comments

Comments
 (0)