github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/logging/panic_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package logging 5 6 import ( 7 "fmt" 8 "strings" 9 "testing" 10 ) 11 12 func TestPanicRecorder(t *testing.T) { 13 rec := panics.registerPlugin("test") 14 15 output := []string{ 16 "panic: test", 17 " stack info", 18 } 19 20 for _, line := range output { 21 rec(line) 22 } 23 24 expected := fmt.Sprintf(pluginPanicOutput, "test", strings.Join(output, "\n")) 25 26 res := PluginPanics() 27 if len(res) == 0 { 28 t.Fatal("no output") 29 } 30 31 if res[0] != expected { 32 t.Fatalf("expected: %q\ngot: %q", expected, res[0]) 33 } 34 } 35 36 func TestPanicLimit(t *testing.T) { 37 rec := panics.registerPlugin("test") 38 39 rec("panic: test") 40 41 for i := 0; i < 200; i++ { 42 rec(fmt.Sprintf("LINE: %d", i)) 43 } 44 45 res := PluginPanics() 46 // take the extra content into account 47 max := strings.Count(pluginPanicOutput, "\n") + panics.maxLines 48 for _, out := range res { 49 found := strings.Count(out, "\n") 50 if found > max { 51 t.Fatalf("expected no more than %d lines, got: %d", max, found) 52 } 53 } 54 }