From 62290215ec0d655bd02a25d745dca9aed1fe16c5 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Mon, 25 Sep 2023 14:17:22 +0200 Subject: [PATCH] Test os.exit and log.Fatalf in options_test.go --- internal/options/options_test.go | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/internal/options/options_test.go b/internal/options/options_test.go index 06ff93f..200ffa6 100644 --- a/internal/options/options_test.go +++ b/internal/options/options_test.go @@ -4,6 +4,7 @@ package options import ( "fmt" "os" + "os/exec" "testing" ) @@ -16,6 +17,7 @@ type config struct { // Parser helps parsing command line arguments and loading // stored configurations from file. func TestParse(t *testing.T) { + originalArgs := os.Args os.Args = []string{"cmd"} defaultConfigLocation := []string{"data/config.toml"} p := Parser[config]{ @@ -63,6 +65,39 @@ func TestParse(t *testing.T) { if _, _, err := p.Parse(); err == nil { t.Errorf("Failure: Invalid path expanded.") } + + // os.exit tests start here + // test the help flag + // if TEST_HELP was set, try parsing the help flag + if os.Getenv("TEST_HELP") == "1" { + os.Args = []string{"cmd", "--help"} + p.Parse() + return + } + + // Build subprocess that can be exited + cmd := exec.Command(originalArgs[0], "-test.run=TestParse") + cmd.Env = append(os.Environ(), "TEST_HELP=1") + err := cmd.Run() + if err != nil { + t.Fatalf(err.Error()) + } + + // test the version flag + if os.Getenv("TEST_VERSION") == "1" { + os.Args = []string{"cmd", "--version"} + p.Parse() + return + } + + cmd = exec.Command(originalArgs[0], "-test.run=TestParse") + cmd.Env = append(os.Environ(), "TEST_VERSION=1") + err = cmd.Run() + if err != nil { + t.Fatalf(err.Error()) + } + // Reset os.Args + os.Args = originalArgs } // TestFindConfigFile tests if findConfigFile() correctly finds existing and @@ -100,3 +135,20 @@ func TestLoadToml(t *testing.T) { t.Errorf(err.Error()) } } + +// TestErrorCheck checks whether the ErrorChecker correctly logs a fatal error +func TestErrorCheck(t *testing.T) { + if os.Getenv("TEST_ERROR") == "1" { + testError := fmt.Errorf("Succesful") + ErrorCheck(testError) + return + } + cmd := exec.Command(os.Args[0], "-test.run=TestErrorCheck") + cmd.Env = append(os.Environ(), "TEST_ERROR=1") + err := cmd.Run() + if e, ok := err.(*exec.ExitError); ok && !e.Success() { + return + } + t.Fatalf("process ran with err %v, want exit status 1", err) + +}