-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tronje Krop <[email protected]>
- Loading branch information
Tronje Krop
authored
Mar 6, 2024
1 parent
68ee813
commit c774272
Showing
15 changed files
with
97 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ export GO ?= go | |
export GOPATH ?= $(shell $(GO) env GOPATH) | ||
export GOBIN ?= $(GOPATH)/bin | ||
# Setup go-make to utilize desired build and config scripts. | ||
GOMAKE_DEP ?= github.com/tkrop/[email protected].58 | ||
GOMAKE_DEP ?= github.com/tkrop/[email protected].61 | ||
# Request targets from go-make targets target. | ||
TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \ | ||
$(GO) install $(GOMAKE_DEP) >/dev/stderr && \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/tkrop/go-testing/test" | ||
) | ||
|
||
type MainParams struct { | ||
args []string | ||
expectExitCode int | ||
} | ||
|
||
var testMainParams = map[string]MainParams{ | ||
"no mock to generate": { | ||
args: []string{"mock"}, | ||
expectExitCode: 0, | ||
}, | ||
} | ||
|
||
func TestMain(t *testing.T) { | ||
test.Map(t, testMainParams). | ||
Run(func(t test.Test, param MainParams) { | ||
// Switch to execute main function in test process. | ||
if name := os.Getenv("TEST"); name != "" { | ||
// Ensure only expected test is running. | ||
if name == t.Name() { | ||
os.Args = param.args | ||
main() | ||
assert.Fail(t, "os-exit not called") | ||
} | ||
// Skip other test. | ||
return | ||
} | ||
|
||
// Call the main function in a separate process to prevent capture | ||
// regular process exit behavior. | ||
cmd := exec.Command(os.Args[0], "-test.run=TestMain") | ||
cmd.Env = append(os.Environ(), "TEST="+t.Name()) | ||
if err := cmd.Run(); err != nil || param.expectExitCode != 0 { | ||
errExit := &exec.ExitError{} | ||
if errors.As(err, &errExit) { | ||
assert.Equal(t, param.expectExitCode, errExit.ExitCode()) | ||
} else { | ||
assert.Fail(t, "unexpected error", err) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
module github.com/tkrop/go-testing | ||
|
||
go 1.22.0 | ||
go 1.22.1 | ||
|
||
require ( | ||
github.com/golang/mock v1.6.0 | ||
github.com/h2non/gock v1.2.0 | ||
github.com/huandu/go-clone v1.6.0 | ||
github.com/stretchr/testify v1.8.4 | ||
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a | ||
github.com/stretchr/testify v1.9.0 | ||
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 | ||
golang.org/x/text v0.13.0 | ||
golang.org/x/tools v0.18.0 | ||
golang.org/x/tools v0.19.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect | ||
github.com/kr/pretty v0.3.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
golang.org/x/mod v0.15.0 // indirect | ||
golang.org/x/mod v0.16.0 // indirect | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.