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

Enable Providing Build Tags For Tests #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
export PKG := github.com/zimmski/go-mutesting
export ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

export TEST_TIMEOUT_IN_SECONDS := 240
export TEST_TIMEOUT_IN_SECONDS := 360

$(eval $(ARGS):;@:) # turn arguments into do-nothing targets
export ARGS
Expand Down
17 changes: 12 additions & 5 deletions cmd/go-mutesting/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ const (

type options struct {
General struct {
Debug bool `long:"debug" description:"Debug log output"`
DoNotRemoveTmpFolder bool `long:"do-not-remove-tmp-folder" description:"Do not remove the tmp folder where all mutations are saved to"`
Help bool `long:"help" description:"Show this help message"`
Verbose bool `long:"verbose" description:"Verbose log output"`
Debug bool `long:"debug" description:"Debug log output"`
DoNotRemoveTmpFolder bool `long:"do-not-remove-tmp-folder" description:"Do not remove the tmp folder where all mutations are saved to"`
Help bool `long:"help" description:"Show this help message"`
Verbose bool `long:"verbose" description:"Verbose log output"`
TestTags string `long:"test-tags" description:"Build tags used when running go test"`
} `group:"General options"`

Files struct {
Expand Down Expand Up @@ -407,7 +408,13 @@ func mutateExec(opts *options, pkg *types.Package, file string, src ast.Node, mu
pkgName += "/..."
}

test, err := exec.Command("go", "test", "-timeout", fmt.Sprintf("%ds", opts.Exec.Timeout), pkgName).CombinedOutput()
var test []byte
if opts.General.TestTags != "" {
test, err = exec.Command("go", "test", "-tags", opts.General.TestTags, "-timeout", fmt.Sprintf("%ds", opts.Exec.Timeout), pkgName).CombinedOutput()
} else {
test, err = exec.Command("go", "test", "-timeout", fmt.Sprintf("%ds", opts.Exec.Timeout), pkgName).CombinedOutput()
}

if err == nil {
execExitCode = 0
} else if e, ok := err.(*exec.ExitError); ok {
Expand Down
16 changes: 13 additions & 3 deletions cmd/go-mutesting/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMain(t *testing.T) {
"../../example",
[]string{"--debug", "--exec-timeout", "1"},
returnOk,
"The mutation score is 0.450000 (9 passed, 11 failed, 8 duplicated, 0 skipped, total is 20)",
"The mutation score is 0.428571 (9 passed, 12 failed, 8 duplicated, 0 skipped, total is 21)",
)
}

Expand All @@ -25,7 +25,7 @@ func TestMainRecursive(t *testing.T) {
"../../example",
[]string{"--debug", "--exec-timeout", "1", "./..."},
returnOk,
"The mutation score is 0.476190 (10 passed, 11 failed, 8 duplicated, 0 skipped, total is 21)",
"The mutation score is 0.454545 (10 passed, 12 failed, 8 duplicated, 0 skipped, total is 22)",
)
}

Expand All @@ -35,7 +35,7 @@ func TestMainFromOtherDirectory(t *testing.T) {
"../..",
[]string{"--debug", "--exec-timeout", "1", "github.com/zimmski/go-mutesting/example"},
returnOk,
"The mutation score is 0.450000 (9 passed, 11 failed, 8 duplicated, 0 skipped, total is 20)",
"The mutation score is 0.428571 (9 passed, 12 failed, 8 duplicated, 0 skipped, total is 21)",
)
}

Expand All @@ -49,6 +49,16 @@ func TestMainMatch(t *testing.T) {
)
}

func TestTagged(t *testing.T) {
testMain(
t,
"../../example",
[]string{"--debug", "--exec-timeout", "1", "--test-tags", "tagged", "github.com/zimmski/go-mutesting/example"},
returnOk,
"The mutation score is 0.476190 (10 passed, 11 failed, 8 duplicated, 0 skipped, total is 21)",
)
}

func testMain(t *testing.T, root string, exec []string, expectedExitCode int, contains string) {
saveStderr := os.Stderr
saveStdout := os.Stdout
Expand Down
5 changes: 5 additions & 0 deletions example/tagged.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package example

func gt(a, b int) bool {
return a > b
}
21 changes: 21 additions & 0 deletions example/tagged_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// +build tagged

package example

import (
"testing"

. "github.com/stretchr/testify/assert"
)

func TestGreaterThan(t *testing.T) {
Equal(t, gt(2,1), true)
}

func TestLessThan(t *testing.T) {
Equal(t, gt(1,2), false)
}

func TestEqual(t *testing.T) {
Equal(t, gt(2,2), false)
}