Skip to content

Commit

Permalink
Merge pull request #19 from Songmu/tags
Browse files Browse the repository at this point in the history
care -tags flag to list test cases
  • Loading branch information
Songmu authored May 22, 2022
2 parents cb58b45 + 098ab1b commit 4189f7b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
7 changes: 4 additions & 3 deletions cmd_regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type cmdRegexp struct{}

func (c *cmdRegexp) run(ctx context.Context, argv []string, outStream io.Writer, errStream io.Writer) error {
// FIXME:
if len(argv) < 3 {
return fmt.Errorf("not enough arguments")
}
Expand All @@ -25,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, total, idx)
str, err := getOut(pkgs, detectTags(argv), total, idx)
if err != nil {
return err
}
_, err = fmt.Fprintln(outStream, str)
return err
}

func getOut(pkgs []string, total, idx int) (string, error) {
func getOut(pkgs []string, tags string, 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)
testLists, err := getTestListsFromPkgs(pkgs, tags)
if err != nil {
return "", err
}
Expand Down
27 changes: 25 additions & 2 deletions gotesplit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"os"
"os/exec"
"regexp"
"sort"
"strings"
)
Expand Down Expand Up @@ -59,8 +60,12 @@ Options:
return run(ctx, *total, *index, *junitDir, argv, outStream, errStream)
}

func getTestListsFromPkgs(pkgs []string) ([]testList, error) {
args := append([]string{"test", "-list", "."}, pkgs...)
func getTestListsFromPkgs(pkgs []string, tags string) ([]testList, error) {
args := []string{"test", "-list"}
if tags != "" {
args = append(args, tags)
}
args = append(append(args, "."), pkgs...)
buf := &bytes.Buffer{}
c := exec.Command("go", args...)
c.Stdout = buf
Expand All @@ -72,6 +77,24 @@ func getTestListsFromPkgs(pkgs []string) ([]testList, error) {
return getTestLists(buf.String()), nil
}

var tagsReg = regexp.MustCompile(`^--?tags(=.*)?$`)

func detectTags(argv []string) string {
l := len(argv)
for i := 0; i < l; i++ {
tags := argv[i]
m := tagsReg.FindStringSubmatch(tags)
if len(m) < 2 {
continue
}
if m[1] == "" && i+1 < l {
tags += "=" + argv[i+1]
}
return tags
}
return ""
}

type testList struct {
pkg string
list []string
Expand Down
21 changes: 21 additions & 0 deletions gotesplit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,24 @@ ok github.com/x-motemen/ghq/logger 0.106s`
t.Errorf("expect: %#v\ngot: %#v", expect, got)
}
}

func TestDetectTags(t *testing.T) {
testCases := []struct {
input []string
expect string
}{
{[]string{"aa", "bb"}, ""},
{[]string{"aa", "-tags", "bb"}, "-tags=bb"},
{[]string{"aa", "--tags=ccc", "bb"}, "--tags=ccc"},
{[]string{"aa", "-tags"}, "-tags"},
}

for _, tc := range testCases {
t.Run(tc.expect, func(t *testing.T) {
out := detectTags(tc.input)
if out != tc.expect {
t.Errorf("got: %s, expect: %s", out, tc.expect)
}
})
}
}
2 changes: 1 addition & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func run(ctx context.Context, total, idx uint, junitDir string, argv []string, o
}
}

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

0 comments on commit 4189f7b

Please sign in to comment.