Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
test: add a panic catcher test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Apr 18, 2022
1 parent ef3e50c commit 8e16a4a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions internal/catch/catch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package catch

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"
)

func TestCatch(t *testing.T) {
buf := new(bytes.Buffer)

oldPanicWriter := panicWriter
t.Cleanup(func() { panicWriter = oldPanicWriter })
panicWriter = buf

panicAndCatch := func() (err error) {
defer func() { HandlePanic(recover(), &err, "somewhere") }()

panic("here")
}

err := panicAndCatch()
require.Error(t, err)
require.Contains(t, err.Error(), "panic in somewhere: here")

require.Contains(t, buf.String(), "caught panic: here")
}

0 comments on commit 8e16a4a

Please sign in to comment.