opentofu/internal/logging/panic_test.go
James Bardin 3f31533f86 logging.PanicHandler can now be for internal use
Repurpose logging.PanicHandler to recover internal panics and print a
message to users similar to panicwrap.
2021-10-28 11:51:39 -04:00

52 lines
927 B
Go

package logging
import (
"fmt"
"strings"
"testing"
)
func TestPanicRecorder(t *testing.T) {
rec := panics.registerPlugin("test")
output := []string{
"panic: test",
" stack info",
}
for _, line := range output {
rec(line)
}
expected := fmt.Sprintf(pluginPanicOutput, "test", strings.Join(output, "\n"))
res := PluginPanics()
if len(res) == 0 {
t.Fatal("no output")
}
if res[0] != expected {
t.Fatalf("expected: %q\ngot: %q", expected, res[0])
}
}
func TestPanicLimit(t *testing.T) {
rec := panics.registerPlugin("test")
rec("panic: test")
for i := 0; i < 200; i++ {
rec(fmt.Sprintf("LINE: %d", i))
}
res := PluginPanics()
// take the extra content into account
max := strings.Count(pluginPanicOutput, "\n") + panics.maxLines
for _, out := range res {
found := strings.Count(out, "\n")
if found > max {
t.Fatalf("expected no more than %d lines, got: %d", max, found)
}
}
}