github.com/koko1123/flow-go-1@v0.29.6/utils/unittest/unittest_test.go (about) 1 package unittest 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 ) 8 9 // TestCrashTest_ErrorMessage tests that CrashTest() can check a function that crashed without any messages. 10 func TestCrashTest_NoMessage(t *testing.T) { 11 f := func(t *testing.T) { 12 crash_NoMessage() 13 } 14 15 CrashTest(t, f, "") 16 } 17 18 // TestCrashTest_ErrorMessage tests that CrashTest() can read standard messages from stdout before a crash. 19 func TestCrashTest_ErrorMessage(t *testing.T) { 20 f := func(t *testing.T) { 21 crash_ErrorMessage() 22 } 23 CrashTest(t, f, "about to crash") 24 } 25 26 // TestCrashTest_ExitCode tests that CrashTest() catches exit codes other than 1. 27 func TestCrashTest_ExitCode(t *testing.T) { 28 f := func(t *testing.T) { 29 crash_ExitCode2() 30 } 31 CrashTestWithExpectedStatus(t, f, "exit code == 2", 2) 32 } 33 34 // TestCrashTest_ExitCodeExplicit tests that CrashTest() catches exit codes other than 1 w/ an explicit exit code. 35 func TestCrashTest_ExitCodeExplicit(t *testing.T) { 36 f := func(t *testing.T) { 37 crash_ExitCode2() 38 } 39 CrashTestWithExpectedStatus(t, f, "exit code == 2", 4, 3, 2) 40 } 41 42 // TestCrashTest_Logger tests that CrashTest() can read fatal logger messages from stdout before a crash. This test 43 // assumes that the logger uses a hook to send fatal messages to stdout. 44 func TestCrashTest_Logger(t *testing.T) { 45 f := func(t *testing.T) { 46 crash_LoggerFatal() 47 } 48 CrashTest(t, f, "fatal crash from logger") 49 } 50 51 func crash_NoMessage() { 52 os.Exit(1) 53 } 54 55 func crash_ErrorMessage() { 56 fmt.Println("about to crash... crashing in 3...2...1...") 57 os.Exit(1) 58 } 59 60 func crash_ExitCode2() { 61 fmt.Println("crashing with a exit code == 2") 62 os.Exit(2) 63 } 64 65 func crash_LoggerFatal() { 66 // hook sends fatal messages to stdout, so they can be checked by CrashTest() 67 logger, _ := HookedLogger() 68 69 // calling Fatal() causes the process to exit 70 logger.Fatal().Msg("fatal crash from logger... crashing in 3...2...1...") 71 }