Skip to content

Commit

Permalink
tools/check/ut: support pattern matching on test names (#33020)
Browse files Browse the repository at this point in the history
ref #30822
  • Loading branch information
tangenta committed Mar 23, 2022
1 parent 9f72bcd commit 23d79d4
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions tools/check/ut.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"os/exec"
"path"
"regexp"
"runtime"
"sort"
"strings"
Expand All @@ -48,6 +49,9 @@ ut list
// list test cases of a single package
ut list $package
// list test cases that match a pattern
ut list $package 'r:$regex'
// run all tests
ut run
Expand All @@ -57,6 +61,9 @@ ut run $package
// run test cases of a single package
ut run $package $test
// run test cases that match a pattern
ut run $package 'r:$regex'
// build all test package
ut build
Expand Down Expand Up @@ -101,7 +108,7 @@ func cmdList(args ...string) bool {
}

// list test case of a single package
if len(args) == 1 {
if len(args) == 1 || len(args) == 2 {
pkg := args[0]
pkgs = filter(pkgs, func(s string) bool { return s == pkg })
if len(pkgs) != 1 {
Expand Down Expand Up @@ -129,6 +136,15 @@ func cmdList(args ...string) bool {
fmt.Println("list test cases for package error", err)
return false
}

if len(args) == 2 {
res, err = filterTestCases(res, args[1])
if err != nil {
fmt.Println("filter test cases error", err)
return false
}
}

for _, x := range res {
fmt.Println(x.test)
}
Expand Down Expand Up @@ -249,14 +265,11 @@ func cmdRun(args ...string) bool {
fmt.Println("list test cases error", err)
return false
}
// filter the test case to run
tmp := tasks[:0]
for _, task := range tasks {
if strings.Contains(task.test, args[1]) {
tmp = append(tmp, task)
}
tasks, err = filterTestCases(tasks, args[1])
if err != nil {
fmt.Println("filter test cases error", err)
return false
}
tasks = tmp
}

if except != "" {
Expand Down Expand Up @@ -608,6 +621,29 @@ func listTestCases(pkg string, tasks []task) ([]task, error) {
return tasks, nil
}

func filterTestCases(tasks []task, arg1 string) ([]task, error) {
if strings.HasPrefix(arg1, "r:") {
r, err := regexp.Compile(arg1[2:])
if err != nil {
return nil, err
}
tmp := tasks[:0]
for _, task := range tasks {
if r.MatchString(task.test) {
tmp = append(tmp, task)
}
}
return tmp, nil
}
tmp := tasks[:0]
for _, task := range tasks {
if strings.Contains(task.test, arg1) {
tmp = append(tmp, task)
}
}
return tmp, nil
}

func listPackages() ([]string, error) {
cmd := exec.Command("go", "list", "./...")
ss, err := cmdToLines(cmd)
Expand Down

0 comments on commit 23d79d4

Please sign in to comment.