cmd/screentest,internal/screentest: add flag to run subset of tests

Add a -run flag to screentest, similar to the one for `go test`,
so the user can run only one or a few tests.

I found this useful when converting release notes to markdown.

Change-Id: I13efe9c5e95b4c939ce8d1b9442f723d51c04e05
Reviewed-on: https://go-review.googlesource.com/c/website/+/539497
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Jonathan Amsterdam 2023-11-03 17:41:24 -04:00
Родитель 91adbeedb9
Коммит 42c7c5d758
3 изменённых файлов: 31 добавлений и 13 удалений

Просмотреть файл

@ -17,6 +17,8 @@ The flags are:
number of testcases to run concurrently number of testcases to run concurrently
-d -d
chrome debugger url chrome debugger url
-run
run only tests matching regexp
*/ */
package main package main
@ -26,6 +28,7 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"strings" "strings"
@ -37,6 +40,7 @@ var (
vars = flag.String("v", "", "variables provided to script templates as comma separated KEY:VALUE pairs") vars = flag.String("v", "", "variables provided to script templates as comma separated KEY:VALUE pairs")
concurrency = flag.Int("c", (runtime.NumCPU()+1)/2, "number of testcases to run concurrently") concurrency = flag.Int("c", (runtime.NumCPU()+1)/2, "number of testcases to run concurrently")
debuggerURL = flag.String("d", "", "chrome debugger url") debuggerURL = flag.String("d", "", "chrome debugger url")
run = flag.String("run", "", "regexp to match test")
) )
func main() { func main() {
@ -71,6 +75,13 @@ func main() {
Vars: parsedVars, Vars: parsedVars,
DebuggerURL: *debuggerURL, DebuggerURL: *debuggerURL,
} }
if *run != "" {
re, err := regexp.Compile(*run)
if err != nil {
log.Fatal(err)
}
opts.Filter = re.MatchString
}
if err := screentest.CheckHandler(glob, opts); err != nil { if err := screentest.CheckHandler(glob, opts); err != nil {
log.Fatal(err) log.Fatal(err)
} }

Просмотреть файл

@ -159,6 +159,10 @@ type CheckOptions struct {
// screentest tries to find the Chrome executable on the system and starts // screentest tries to find the Chrome executable on the system and starts
// a new instance. // a new instance.
DebuggerURL string DebuggerURL string
// If set, only tests for which Filter returns true are included.
// Filter is called on the test name.
Filter func(string) bool
} }
// CheckHandler runs the test scripts matched by glob. If any errors are // CheckHandler runs the test scripts matched by glob. If any errors are
@ -188,11 +192,11 @@ func CheckHandler(glob string, opts CheckOptions) error {
defer cancel() defer cancel()
var buf bytes.Buffer var buf bytes.Buffer
for _, file := range files { for _, file := range files {
tests, err := readTests(file, opts.Vars) tests, err := readTests(file, opts.Vars, opts.Filter)
if err != nil { if err != nil {
return fmt.Errorf("readTestdata(%q): %w", file, err) return fmt.Errorf("readTestdata(%q): %w", file, err)
} }
if len(tests) == 0 { if len(tests) == 0 && opts.Filter == nil {
return fmt.Errorf("no tests found in %q", file) return fmt.Errorf("no tests found in %q", file)
} }
if err := cleanOutput(ctx, tests); err != nil { if err := cleanOutput(ctx, tests); err != nil {
@ -248,7 +252,7 @@ func TestHandler(t *testing.T, glob string, opts TestOpts) {
)...) )...)
defer cancel() defer cancel()
for _, file := range files { for _, file := range files {
tests, err := readTests(file, opts.Vars) tests, err := readTests(file, opts.Vars, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -415,7 +419,7 @@ func (t *testcase) String() string {
} }
// readTests parses the testcases from a text file. // readTests parses the testcases from a text file.
func readTests(file string, vars map[string]string) ([]*testcase, error) { func readTests(file string, vars map[string]string, filter func(string) bool) ([]*testcase, error) {
tmpl := template.New(filepath.Base(file)).Funcs(template.FuncMap{ tmpl := template.New(filepath.Base(file)).Funcs(template.FuncMap{
"ints": func(start, end int) []int { "ints": func(start, end int) []int {
var out []int var out []int
@ -561,6 +565,9 @@ func readTests(file string, vars map[string]string) ([]*testcase, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("url.Parse(%q): %w", originB+pathname, err) return nil, fmt.Errorf("url.Parse(%q): %w", originB+pathname, err)
} }
if filter != nil && !filter(testName) {
continue
}
test := &testcase{ test := &testcase{
name: testName, name: testName,
tasks: tasks, tasks: tasks,

Просмотреть файл

@ -173,7 +173,7 @@ func TestReadTests(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := readTests(tt.args.filename, map[string]string{"Authorization": "Bearer token"}) got, err := readTests(tt.args.filename, map[string]string{"Authorization": "Bearer token"}, nil)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("readTests() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("readTests() error = %v, wantErr %v", err, tt.wantErr)
return return