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

tools/check/ut: support pattern matching on test names #33020

Merged
merged 7 commits into from
Mar 23, 2022
Merged
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
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