Skip to content

Commit 07c70ad

Browse files
author
Tronje Krop
authored
test: improve test coverage (#83)
Signed-off-by: Tronje Krop <[email protected]>
1 parent c14e847 commit 07c70ad

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.12
1+
0.0.13

test/pattern.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11+
// errExit is an error instance for comparing to the exit code error.
12+
var errExit = &exec.ExitError{}
13+
1114
// MainParams provides the test parameters for testing a `main`-method.
1215
type MainParams struct {
1316
Args []string
@@ -56,10 +59,10 @@ func TestMain(main func()) func(t Test, param MainParams) {
5659
cmd := exec.Command(os.Args[0], "-test.run="+t.(*Tester).t.Name())
5760
cmd.Env = append(append(os.Environ(), "TEST="+t.Name()), param.Env...)
5861
if err := cmd.Run(); err != nil || param.ExitCode != 0 {
59-
errExit := &exec.ExitError{}
60-
if errors.As(err, &errExit) {
62+
if errors.As(err, &errExit) || err != nil {
6163
require.Equal(t, param.ExitCode, errExit.ExitCode())
6264
} else {
65+
// #no-cover: impossible to reach this code.
6366
require.Fail(t, "unexpected error", err)
6467
}
6568
}

test/pattern_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package test_test
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"testing"
7+
8+
"github.com/tkrop/go-testing/test"
9+
)
10+
11+
var testMainParams = map[string]test.MainParams{
12+
"exit-0": {},
13+
"exit-1": {
14+
Env: []string{"exit=1"},
15+
ExitCode: 1,
16+
},
17+
}
18+
19+
func TestMain(t *testing.T) {
20+
test.Map(t, testMainParams).Run(test.TestMain(main))
21+
}
22+
23+
func TestMainUnexpected(t *testing.T) {
24+
t.Setenv("TEST", "other")
25+
test.Map(t, testMainParams).RunSeq(test.TestMain(main))
26+
}
27+
28+
func main() {
29+
exit, _ := strconv.Atoi(os.Getenv("exit"))
30+
os.Exit(exit)
31+
}

0 commit comments

Comments
 (0)