Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve test coverage (#83) #83

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading