Skip to content

Commit

Permalink
test: improve test coverage (#83)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
tkrop committed Sep 9, 2024
1 parent c14e847 commit c5efebb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.12
0.0.13
7 changes: 5 additions & 2 deletions test/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/pattern_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit c5efebb

Please sign in to comment.