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

Add -race to list when it is specified for test options #26

Merged
merged 1 commit into from
Sep 27, 2023
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
6 changes: 3 additions & 3 deletions cmd_regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ func (c *cmdRegexp) run(ctx context.Context, argv []string, outStream io.Writer,
return fmt.Errorf("invalid index: %s", err)
}

str, err := getOut(pkgs, detectTags(argv), total, idx)
str, err := getOut(pkgs, detectTags(argv), detectRace(argv), total, idx)
if err != nil {
return err
}
_, err = fmt.Fprintln(outStream, str)
return err
}

func getOut(pkgs []string, tags string, total, idx int) (string, error) {
func getOut(pkgs []string, tags string, withRace bool, total, idx int) (string, error) {
if total < 1 {
return "", fmt.Errorf("invalid total: %d", total)
}
if idx >= total {
return "", fmt.Errorf("index shoud be between 0 to total-1, but: %d (total:%d)", idx, total)
}
testLists, err := getTestListsFromPkgs(pkgs, tags)
testLists, err := getTestListsFromPkgs(pkgs, tags, withRace)
if err != nil {
return "", err
}
Expand Down
17 changes: 16 additions & 1 deletion gotesplit.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,16 @@ Options:
return run(ctx, *total, *index, *junitDir, argv, outStream, errStream)
}

func getTestListsFromPkgs(pkgs []string, tags string) ([]testList, error) {
func getTestListsFromPkgs(pkgs []string, tags string, withRace bool) ([]testList, error) {
args := []string{"test", "-list", "."}
if tags != "" {
args = append(args, tags)
}
if withRace {
// If -race is specified for test options, add -race to list
// to prevent compilation from being executed twice.
args = append(args, "-race")
}
args = append(args, pkgs...)
buf := &bytes.Buffer{}
c := exec.Command("go", args...)
Expand Down Expand Up @@ -95,6 +100,16 @@ func detectTags(argv []string) string {
return ""
}

func detectRace(argv []string) bool {
l := len(argv)
for i := 0; i < l; i++ {
if argv[i] == "-race" || argv[i] == "--race" {
return true
}
}
return false
}

type testList struct {
pkg string
list []string
Expand Down
25 changes: 24 additions & 1 deletion gotesplit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ func TestDetectTags(t *testing.T) {
}
}

func TestDetectRace(t *testing.T) {
testCases := []struct {
input []string
expect bool
desc string
}{
{[]string{"-race"}, true, "-race only"},
{[]string{"-tags", "aaa", "-race", "-bench"}, true, "-race with other flags"},
{[]string{"--race", "-p", "1"}, true, "--race with other flags"},
{[]string{}, false, "no flags"},
{[]string{"-short", "-p", "1"}, false, "flags without -race"},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
out := detectRace(tc.input)
if out != tc.expect {
t.Errorf("got: %t, expect: %t", out, tc.expect)
}
})
}
}

func TestGetTestListFromPkgs(t *testing.T) {
if err := os.Chdir("testdata/withtags"); err != nil {
wd, _ := os.Getwd()
Expand All @@ -139,7 +162,7 @@ func TestGetTestListFromPkgs(t *testing.T) {
},
}}

got, err := getTestListsFromPkgs([]string{"."}, "-tags=a")
got, err := getTestListsFromPkgs([]string{"."}, "-tags=a", false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func run(ctx context.Context, total, idx uint, junitDir string, argv []string, o
}
}

testLists, err := getTestListsFromPkgs(pkgs, detectTags(testOpts))
testLists, err := getTestListsFromPkgs(pkgs, detectTags(testOpts), detectRace(testOpts))
if err != nil {
return err
}
Expand Down