diff --git a/VERSION b/VERSION index 8cbf02c..43b2961 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.12 +0.0.13 diff --git a/test/pattern.go b/test/pattern.go index 6e9b97f..ae457b5 100644 --- a/test/pattern.go +++ b/test/pattern.go @@ -8,6 +8,9 @@ import ( "github.com/stretchr/testify/require" ) +// errExit is an error instance for comparing to the exit code error. +var errExit = &exec.ExitError{} + // MainParams provides the test parameters for testing a `main`-method. type MainParams struct { Args []string @@ -56,10 +59,10 @@ func TestMain(main func()) func(t Test, param MainParams) { cmd := exec.Command(os.Args[0], "-test.run="+t.(*Tester).t.Name()) cmd.Env = append(append(os.Environ(), "TEST="+t.Name()), param.Env...) if err := cmd.Run(); err != nil || param.ExitCode != 0 { - errExit := &exec.ExitError{} - if errors.As(err, &errExit) { + if errors.As(err, &errExit) || err != nil { require.Equal(t, param.ExitCode, errExit.ExitCode()) } else { + // #no-cover: impossible to reach this code. require.Fail(t, "unexpected error", err) } } diff --git a/test/pattern_test.go b/test/pattern_test.go new file mode 100644 index 0000000..b3c13d4 --- /dev/null +++ b/test/pattern_test.go @@ -0,0 +1,31 @@ +package test_test + +import ( + "os" + "strconv" + "testing" + + "github.com/tkrop/go-testing/test" +) + +var testMainParams = map[string]test.MainParams{ + "exit-0": {}, + "exit-1": { + Env: []string{"exit=1"}, + ExitCode: 1, + }, +} + +func TestMain(t *testing.T) { + test.Map(t, testMainParams).Run(test.TestMain(main)) +} + +func TestMainUnexpected(t *testing.T) { + t.Setenv("TEST", "other") + test.Map(t, testMainParams).RunSeq(test.TestMain(main)) +} + +func main() { + exit, _ := strconv.Atoi(os.Getenv("exit")) + os.Exit(exit) +}