Skip to content

Commit 36a014a

Browse files
committed
Enable Providing Build Tags For Tests
adding the `--test-tags` flag. The value of `--test-tags` is given to `-tags` when running `go test` to check mutations.
1 parent 6d92170 commit 36a014a

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
44
export PKG := github.com/zimmski/go-mutesting
55
export ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
66

7-
export TEST_TIMEOUT_IN_SECONDS := 240
7+
export TEST_TIMEOUT_IN_SECONDS := 360
88

99
$(eval $(ARGS):;@:) # turn arguments into do-nothing targets
1010
export ARGS

cmd/go-mutesting/main.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ const (
3939

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

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

410-
test, err := exec.Command("go", "test", "-timeout", fmt.Sprintf("%ds", opts.Exec.Timeout), pkgName).CombinedOutput()
411+
var test []byte
412+
if opts.General.TestTags != "" {
413+
test, err = exec.Command("go", "test", "-tags", opts.General.TestTags, "-timeout", fmt.Sprintf("%ds", opts.Exec.Timeout), pkgName).CombinedOutput()
414+
} else {
415+
test, err = exec.Command("go", "test", "-timeout", fmt.Sprintf("%ds", opts.Exec.Timeout), pkgName).CombinedOutput()
416+
}
417+
411418
if err == nil {
412419
execExitCode = 0
413420
} else if e, ok := err.(*exec.ExitError); ok {

cmd/go-mutesting/main_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestMain(t *testing.T) {
1515
"../../example",
1616
[]string{"--debug", "--exec-timeout", "1"},
1717
returnOk,
18-
"The mutation score is 0.450000 (9 passed, 11 failed, 8 duplicated, 0 skipped, total is 20)",
18+
"The mutation score is 0.428571 (9 passed, 12 failed, 8 duplicated, 0 skipped, total is 21)",
1919
)
2020
}
2121

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

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

@@ -49,6 +49,16 @@ func TestMainMatch(t *testing.T) {
4949
)
5050
}
5151

52+
func TestTagged(t *testing.T) {
53+
testMain(
54+
t,
55+
"../../example",
56+
[]string{"--debug", "--exec-timeout", "1", "--test-tags", "tagged", "github.com/zimmski/go-mutesting/example"},
57+
returnOk,
58+
"The mutation score is 0.476190 (10 passed, 11 failed, 8 duplicated, 0 skipped, total is 21)",
59+
)
60+
}
61+
5262
func testMain(t *testing.T, root string, exec []string, expectedExitCode int, contains string) {
5363
saveStderr := os.Stderr
5464
saveStdout := os.Stdout

example/tagged.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package example
2+
3+
func gt(a, b int) bool {
4+
return a > b
5+
}

example/tagged_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// +build tagged
2+
3+
package example
4+
5+
import (
6+
"testing"
7+
8+
. "github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestGreaterThan(t *testing.T) {
12+
Equal(t, gt(2,1), true)
13+
}
14+
15+
func TestLessThan(t *testing.T) {
16+
Equal(t, gt(1,2), false)
17+
}
18+
19+
func TestEqual(t *testing.T) {
20+
Equal(t, gt(2,2), false)
21+
}

0 commit comments

Comments
 (0)