convert t.Errorf/t.Fatal to testify/require

This commit is contained in:
Daisuke Maki 2026-02-21 07:58:26 +09:00
parent 4e58dad08a
commit 8c4db9e026
12 changed files with 52 additions and 83 deletions

View file

@ -250,9 +250,8 @@ func TestActionNames(t *testing.T) {
"peco.ZoomOut",
}
for _, name := range names {
if _, ok := nameToActions[name]; !ok {
t.Errorf("Action %s should exist, but it does not", name)
}
_, ok := nameToActions[name]
require.True(t, ok, "Action %s should exist, but it does not", name)
}
}
@ -449,9 +448,7 @@ func TestRotateFilter(t *testing.T) {
prev = state.Filters().Current()
}
if first != prev {
t.Errorf("should have rotated back to first one, but didn't")
}
require.Equal(t, first, prev, "should have rotated back to first one, but didn't")
// Verify ExecQuery is triggered after rotation: type a query, rotate,
// and verify the filtered buffer updates (proving re-execution).

View file

@ -80,7 +80,7 @@ func TestExternalCmd_CancelCleansUpGoroutine(t *testing.T) {
case err := <-done:
require.ErrorIs(t, err, context.Canceled)
case <-time.After(5 * time.Second):
t.Fatal("Apply did not return after context cancellation")
require.Fail(t, "Apply did not return after context cancellation")
}
// Wait briefly for goroutine cleanup, then verify no goroutine leak
@ -94,7 +94,7 @@ func TestExternalCmd_CancelCleansUpGoroutine(t *testing.T) {
}
time.Sleep(50 * time.Millisecond)
}
t.Errorf("goroutine leak: before=%d, after=%d", before, runtime.NumGoroutine())
require.Fail(t, "goroutine leak", "before=%d, after=%d", before, runtime.NumGoroutine())
}
func TestExternalCmd_ApplyWithoutQueryContext(t *testing.T) {

View file

@ -228,7 +228,7 @@ func testFuzzyLongest(octx context.Context, t *testing.T, filter Filter) {
require.NoError(t, err, `filter.Apply should succeed`)
break OUTER
case <-ctx.Done():
t.Fatalf("unexpected timeout")
require.Fail(t, "unexpected timeout")
}
}
@ -619,7 +619,7 @@ func testFuzzyMatch(octx context.Context, t *testing.T, filter Filter) {
require.NoError(t, err, `filter.Apply should succeed`)
break OUTER
case <-ctx.Done():
t.Fatalf("unexpected timeout")
require.Fail(t, "unexpected timeout")
}
}
})

View file

@ -123,7 +123,7 @@ func TestBatchPanicReleasesLock(t *testing.T) {
case <-done:
// success — mutex was properly released
case <-time.After(2 * time.Second):
t.Fatal("Batch deadlocked — mutex was not released after panic")
require.Fail(t, "Batch deadlocked — mutex was not released after panic")
}
}
@ -150,7 +150,7 @@ func TestBatchNestedDoesNotDeadlock(t *testing.T) {
case <-done:
// success — nested Batch did not deadlock
case <-time.After(2 * time.Second):
t.Fatal("nested Batch deadlocked")
require.Fail(t, "nested Batch deadlocked")
}
}

View file

@ -86,15 +86,9 @@ func TestKeymapStrToKeyValue(t *testing.T) {
for n, v := range expected {
t.Logf(" checking %s...", n)
e, modifier, _, err := ToKey(n)
if err != nil {
t.Errorf("Key name %s not found", n)
}
if e != v {
t.Errorf("Expected '%s' to be '%d', but got '%d'", n, v, stringToKey[n])
}
if modifier != 0 {
t.Errorf("Key name '%s' is not Alt-prefixed", n)
}
require.NoError(t, err, "Key name %s not found", n)
require.Equal(t, v, e, "Expected '%s' to be '%d', but got '%d'", n, v, stringToKey[n])
require.Equal(t, ModifierKey(0), modifier, "Key name '%s' is not Alt-prefixed", n)
}
}
@ -113,18 +107,10 @@ func TestKeymapStrToKeyValueWithAlt(t *testing.T) {
for n, v := range expected {
t.Logf(" checking %s...", n)
k, modifier, ch, err := ToKey(n)
if err != nil {
t.Errorf("Failed ToKey: Key name %s", n)
}
if modifier != 1 {
t.Errorf("Key name %s has Alt prefix", n)
}
if k != v.key {
t.Errorf("Expected '%s' to be '%d', but got '%d'", n, v.key, k)
}
if ch != v.ch {
t.Errorf("Expected '%s' to be '%c', but got '%c'", n, v.ch, ch)
}
require.NoError(t, err, "Failed ToKey: Key name %s", n)
require.Equal(t, ModifierKey(1), modifier, "Key name %s has Alt prefix", n)
require.Equal(t, v.key, k, "Expected '%s' to be '%d', but got '%d'", n, v.key, k)
require.Equal(t, v.ch, ch, "Expected '%s' to be '%c', but got '%c'", n, v.ch, ch)
}
}
@ -138,19 +124,11 @@ func TestKeymapStrToKeyValueCh(t *testing.T) {
for _, n := range expected {
t.Logf(" checking %s...", n)
k, modifier, ch, err := ToKey(n)
if err != nil {
t.Errorf("Failed ToKey: Key name %s", n)
}
if k != 0 {
t.Errorf("Key name %s is mapped key", n)
}
if modifier == 1 {
t.Errorf("Key name %s has Alt prefix", n)
}
require.NoError(t, err, "Failed ToKey: Key name %s", n)
require.Equal(t, KeyType(0), k, "Key name %s is mapped key", n)
require.NotEqual(t, ModifierKey(1), modifier, "Key name %s has Alt prefix", n)
r, _ := utf8.DecodeRuneInString(n)
if ch != r {
t.Errorf("key name %s cannot convert to rune", n)
}
require.Equal(t, r, ch, "key name %s cannot convert to rune", n)
}
}

View file

@ -306,9 +306,7 @@ func TestIDGen(t *testing.T) {
sel := selection.New()
for _, l := range lines {
if sel.Has(l) {
t.Fatalf("Collision detected %d", l.ID())
}
require.False(t, sel.Has(l), "Collision detected %d", l.ID())
sel.Add(l)
}
}
@ -557,7 +555,7 @@ func TestGHIssue363(t *testing.T) {
select {
case <-ctx.Done():
t.Fatal("timeout reached")
require.Fail(t, "timeout reached")
return
case err := <-resultCh:
require.True(t, util.IsCollectResultsError(err), "isCollectResultsError")
@ -663,7 +661,7 @@ func TestExitZero(t *testing.T) {
select {
case <-ctx.Done():
t.Fatal("timeout reached")
require.Fail(t, "timeout reached")
return
case err := <-resultCh:
require.True(t, util.IsIgnorableError(err), "error should be ignorable")
@ -709,7 +707,7 @@ func TestExitZero(t *testing.T) {
if util.IsIgnorableError(err) {
st, ok := util.GetExitStatus(err)
if ok && st == 1 {
t.Fatal("--exit-0 should not trigger when input is non-empty")
require.Fail(t, "--exit-0 should not trigger when input is non-empty")
}
}
}
@ -729,7 +727,7 @@ func runPecoSelectAll(t *testing.T, p *Peco, ctx context.Context) { //nolint:rev
select {
case <-ctx.Done():
t.Fatal("timeout reached")
require.Fail(t, "timeout reached")
case err := <-resultCh:
require.True(t, util.IsCollectResultsError(err), "isCollectResultsError")
p.PrintResults()
@ -837,7 +835,7 @@ func TestPrintQuery(t *testing.T) {
select {
case <-ctx.Done():
t.Fatal("timeout reached")
require.Fail(t, "timeout reached")
return
case err := <-resultCh:
require.True(t, util.IsCollectResultsError(err), "isCollectResultsError")
@ -875,7 +873,7 @@ func TestPrintQuery(t *testing.T) {
select {
case <-ctx.Done():
t.Fatal("timeout reached")
require.Fail(t, "timeout reached")
return
case err := <-resultCh:
require.True(t, util.IsCollectResultsError(err), "isCollectResultsError")
@ -895,7 +893,7 @@ func TestMemoryBufferMarkComplete(t *testing.T) {
case <-mb.Done():
// expected
default:
t.Fatal("Done channel should be closed after MarkComplete")
require.Fail(t, "Done channel should be closed after MarkComplete")
}
})
@ -909,7 +907,7 @@ func TestMemoryBufferMarkComplete(t *testing.T) {
case <-mb.Done():
// expected
default:
t.Fatal("Done channel should be closed after MarkComplete")
require.Fail(t, "Done channel should be closed after MarkComplete")
}
})
@ -922,7 +920,7 @@ func TestMemoryBufferMarkComplete(t *testing.T) {
// After reset, done should be a new open channel
select {
case <-mb.Done():
t.Fatal("Done channel should not be closed after Reset")
require.Fail(t, "Done channel should not be closed after Reset")
default:
// expected
}
@ -934,7 +932,7 @@ func TestMemoryBufferMarkComplete(t *testing.T) {
case <-mb.Done():
// expected
default:
t.Fatal("Done channel should be closed after second MarkComplete")
require.Fail(t, "Done channel should be closed after second MarkComplete")
}
})
}
@ -1026,7 +1024,7 @@ func TestCancelFuncDataRace(t *testing.T) {
// err could be any of the "exit-N" errors; just verify it's non-nil
require.Error(t, err, "Run should return an error after Exit")
case <-time.After(5 * time.Second):
t.Fatal("timeout waiting for Run to return")
require.Fail(t, "timeout waiting for Run to return")
}
}
@ -1054,7 +1052,7 @@ func TestSelect1WithQuery(t *testing.T) {
select {
case <-ctx.Done():
t.Fatal("timeout: --select-1 --query bar should have auto-selected")
require.Fail(t, "timeout: --select-1 --query bar should have auto-selected")
case err := <-resultCh:
require.True(t, util.IsCollectResultsError(err), "expected collectResultsError")
p.PrintResults()
@ -1081,7 +1079,7 @@ func TestWaitAndCall(t *testing.T) {
elapsed := time.Since(start)
require.True(t, elapsed >= 2*time.Second, "should wait at least 2s (got %v)", elapsed)
case <-time.After(5 * time.Second):
t.Fatal("callback was not fired within 5s")
require.Fail(t, "callback was not fired within 5s")
}
})
@ -1105,7 +1103,7 @@ func TestWaitAndCall(t *testing.T) {
case <-done:
require.False(t, called, "callback should NOT fire after context cancellation")
case <-time.After(5 * time.Second):
t.Fatal("waitAndCall did not return after context cancellation")
require.Fail(t, "waitAndCall did not return after context cancellation")
}
})
}
@ -1150,7 +1148,7 @@ func TestMouseClickToggleSelection(t *testing.T) {
select {
case <-ctx.Done():
t.Fatal("timeout reached")
require.Fail(t, "timeout reached")
case err := <-resultCh:
require.True(t, util.IsCollectResultsError(err), "isCollectResultsError")
p.PrintResults()

View file

@ -108,16 +108,12 @@ func TestQueryContext(t *testing.T) {
t.Run("round-trip", func(t *testing.T) {
ctx := NewQueryContext(context.Background(), "hello")
got := QueryFromContext(ctx)
if got != "hello" {
t.Fatalf("expected %q, got %q", "hello", got)
}
require.Equal(t, "hello", got)
})
t.Run("missing key returns empty", func(t *testing.T) {
got := QueryFromContext(context.Background())
if got != "" {
t.Fatalf("expected empty string, got %q", got)
}
require.Equal(t, "", got)
})
}

View file

@ -94,7 +94,7 @@ func TestInlineScreenPollEventLogsPanic(t *testing.T) {
case _, ok := <-evCh:
require.False(t, ok, "expected channel to be closed after panic")
case <-time.After(2 * time.Second):
t.Fatal("PollEvent channel was not closed after panic")
require.Fail(t, "PollEvent channel was not closed after panic")
}
// Verify that the panic was logged (not silently swallowed).

View file

@ -176,7 +176,7 @@ func TestTcellScreenPollEventLogsPanic(t *testing.T) {
case _, ok := <-evCh:
require.False(t, ok, "expected channel to be closed after panic")
case <-time.After(2 * time.Second):
t.Fatal("PollEvent channel was not closed after panic")
require.Fail(t, "PollEvent channel was not closed after panic")
}
// Verify that the panic was logged (not silently swallowed).
@ -221,7 +221,7 @@ func TestTcellScreenSuspendHandlerExitsOnClose(t *testing.T) {
case <-exited:
// Goroutine exited via doneCh — no leak.
case <-time.After(2 * time.Second):
t.Fatal("suspend handler goroutine did not exit after Close()")
require.Fail(t, "suspend handler goroutine did not exit after Close()")
}
}
@ -253,7 +253,7 @@ func TestTcellScreenPollingGoroutineExitsOnClose(t *testing.T) {
case <-exited:
// Goroutine exited via doneCh.
case <-time.After(2 * time.Second):
t.Fatal("polling goroutine did not exit after Close()")
require.Fail(t, "polling goroutine did not exit after Close()")
}
}
@ -305,7 +305,7 @@ func TestTcellScreenSuspendThenClose(t *testing.T) {
case <-exited:
// Goroutine exited after Close() following a suspend.
case <-time.After(2 * time.Second):
t.Fatal("suspend handler goroutine did not exit after suspend + Close()")
require.Fail(t, "suspend handler goroutine did not exit after suspend + Close()")
}
}
@ -333,7 +333,7 @@ func TestTcellScreenResumeNoDeadlock(t *testing.T) {
case <-done:
// Resume completed without deadlock.
case <-time.After(2 * time.Second):
t.Fatal("Resume() deadlocked")
require.Fail(t, "Resume() deadlocked")
}
}
@ -356,7 +356,7 @@ func TestTcellScreenResumeDoesNotDropSend(t *testing.T) {
case <-received:
// The receiver goroutine got the message.
default:
t.Fatal("receiver did not get the resume message")
require.Fail(t, "receiver did not get the resume message")
}
}
@ -379,7 +379,7 @@ func TestTcellScreenResumeContextCancelled(t *testing.T) {
case <-done:
// Resume returned promptly after context cancellation.
case <-time.After(2 * time.Second):
t.Fatal("Resume() did not unblock after context cancellation")
require.Fail(t, "Resume() did not unblock after context cancellation")
}
}
@ -410,7 +410,7 @@ func TestTcellScreenResumeContextCancelledWhileWaitingForReply(t *testing.T) {
case <-done:
// Resume returned after context cancellation during reply wait.
case <-time.After(2 * time.Second):
t.Fatal("Resume() did not unblock after context cancellation while waiting for reply")
require.Fail(t, "Resume() did not unblock after context cancellation while waiting for reply")
}
// Verify context was indeed cancelled.

View file

@ -126,7 +126,7 @@ func TestCopySelf(t *testing.T) {
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Copy(self) deadlocked")
require.Fail(t, "Copy(self) deadlocked")
}
require.Equal(t, 2, s.Len())
@ -157,6 +157,6 @@ func TestCopyCrossNoDeadlock(t *testing.T) {
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("cross-Copy deadlocked")
require.Fail(t, "cross-Copy deadlocked")
}
}

View file

@ -32,7 +32,7 @@ func TestLoopContextCancel(t *testing.T) {
case err := <-errCh:
require.ErrorIs(t, err, context.Canceled)
case <-time.After(5 * time.Second):
t.Fatal("Loop did not exit after context cancellation")
require.Fail(t, "Loop did not exit after context cancellation")
}
// After Loop returns, the signal channel should be deregistered.
@ -79,7 +79,7 @@ func TestLoopSignalReceived(t *testing.T) {
case err := <-errCh:
require.NoError(t, err)
case <-time.After(5 * time.Second):
t.Fatal("Loop did not exit after signal")
require.Fail(t, "Loop did not exit after signal")
}
require.Equal(t, syscall.SIGUSR1, received, "handler should have received SIGUSR1")

View file

@ -54,7 +54,7 @@ func TestSource(t *testing.T) {
_, ok := <-s.Ready()
require.False(t, ok, "s.Ready should be false at this point")
case <-timeout:
t.Fatal("timed out waiting for source")
require.Fail(t, "timed out waiting for source")
case <-s.Ready():
}