Skip to content

Commit 6a1ac34

Browse files
brettlandaugvisor-bot
authored andcommitted
Refactor ListTests() to common.Search().
This change removes the filepath.Walk() function from proctor- go, php, and nodejs. The filepath.Walk() is now defined in common.go in Search(). Each proctor binary passes root directory and testFilter arguments to Search(). proctor-python.go no longer uses filepath.Walk() to search for tests. There is a built-in list test function within python's language test suite so that is being used instead. PiperOrigin-RevId: 261242897
1 parent 3eff053 commit 6a1ac34

File tree

5 files changed

+61
-96
lines changed

5 files changed

+61
-96
lines changed

test/runtimes/common/common.go

+29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"flag"
2020
"fmt"
2121
"os"
22+
"path/filepath"
23+
"regexp"
2224
)
2325

2426
var (
@@ -71,6 +73,33 @@ func LaunchFunc(tr TestRunner) error {
7173
return nil
7274
}
7375

76+
// Search uses filepath.Walk to perform a search of the disk for test files
77+
// and returns a string slice of tests.
78+
func Search(root string, testFilter *regexp.Regexp) ([]string, error) {
79+
var testSlice []string
80+
81+
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
82+
name := filepath.Base(path)
83+
84+
if info.IsDir() || !testFilter.MatchString(name) {
85+
return nil
86+
}
87+
88+
relPath, err := filepath.Rel(root, path)
89+
if err != nil {
90+
return err
91+
}
92+
testSlice = append(testSlice, relPath)
93+
return nil
94+
})
95+
96+
if err != nil {
97+
return nil, fmt.Errorf("walking %q: %v", root, err)
98+
}
99+
100+
return testSlice, nil
101+
}
102+
74103
func runAllTests(tr TestRunner) error {
75104
tests, err := tr.ListTests()
76105
if err != nil {

test/runtimes/go/proctor-go.go

+15-28
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var (
4040

4141
// Directories with .dir contain helper files for tests.
4242
// Exclude benchmarks and stress tests.
43-
exclDirs = regexp.MustCompile(`^.+\/(bench|stress)\/.+$|^.+\.dir.+$`)
43+
dirFilter = regexp.MustCompile(`^(bench|stress)\/.+$|^.+\.dir.+$`)
4444
)
4545

4646
type goRunner struct {
@@ -59,48 +59,35 @@ func (g goRunner) ListTests() ([]string, error) {
5959
cmd.Stderr = os.Stderr
6060
out, err := cmd.Output()
6161
if err != nil {
62-
log.Fatalf("Failed to list: %v", err)
62+
return nil, fmt.Errorf("failed to list: %v", err)
6363
}
64-
var testSlice []string
64+
var toolSlice []string
6565
for _, test := range strings.Split(string(out), "\n") {
66-
testSlice = append(testSlice, test)
66+
toolSlice = append(toolSlice, test)
6767
}
6868

6969
// Go tests on disk.
70-
if err := filepath.Walk(testDir, func(path string, info os.FileInfo, err error) error {
71-
name := filepath.Base(path)
72-
73-
if info.IsDir() {
74-
return nil
75-
}
76-
77-
if !testRegEx.MatchString(name) {
78-
return nil
79-
}
80-
81-
if exclDirs.MatchString(path) {
82-
return nil
70+
diskSlice, err := common.Search(testDir, testRegEx)
71+
if err != nil {
72+
return nil, err
73+
}
74+
// Remove items from /bench/, /stress/ and .dir files
75+
diskFiltered := diskSlice[:0]
76+
for _, file := range diskSlice {
77+
if !dirFilter.MatchString(file) {
78+
diskFiltered = append(diskFiltered, file)
8379
}
84-
85-
testSlice = append(testSlice, path)
86-
return nil
87-
}); err != nil {
88-
return nil, fmt.Errorf("walking %q: %v", testDir, err)
8980
}
9081

91-
return testSlice, nil
82+
return append(toolSlice, diskFiltered...), nil
9283
}
9384

9485
func (g goRunner) RunTest(test string) error {
9586
// Check if test exists on disk by searching for file of the same name.
9687
// This will determine whether or not it is a Go test on disk.
9788
if strings.HasSuffix(test, ".go") {
9889
// Test has suffix ".go" which indicates a disk test, run it as such.
99-
relPath, err := filepath.Rel(testDir, test)
100-
if err != nil {
101-
return fmt.Errorf("failed to get rel path: %v", err)
102-
}
103-
cmd := exec.Command(goBin, "run", "run.go", "-v", "--", relPath)
90+
cmd := exec.Command(goBin, "run", "run.go", "-v", "--", test)
10491
cmd.Dir = testDir
10592
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
10693
if err := cmd.Run(); err != nil {

test/runtimes/nodejs/proctor-nodejs.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,10 @@ func main() {
4242
}
4343

4444
func (n nodejsRunner) ListTests() ([]string, error) {
45-
var testSlice []string
46-
47-
err := filepath.Walk(testDir, func(path string, info os.FileInfo, err error) error {
48-
name := filepath.Base(path)
49-
50-
if info.IsDir() || !testRegEx.MatchString(name) {
51-
return nil
52-
}
53-
54-
relPath, err := filepath.Rel(testDir, path)
55-
if err != nil {
56-
return err
57-
}
58-
testSlice = append(testSlice, relPath)
59-
return nil
60-
})
61-
45+
testSlice, err := common.Search(testDir, testRegEx)
6246
if err != nil {
63-
return nil, fmt.Errorf("walking %q: %v", testDir, err)
47+
return nil, err
6448
}
65-
6649
return testSlice, nil
6750
}
6851

test/runtimes/php/proctor-php.go

+2-20
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"log"
2121
"os"
2222
"os/exec"
23-
"path/filepath"
2423
"regexp"
2524

2625
"gvisor.dev/gvisor/test/runtimes/common"
@@ -41,27 +40,10 @@ func main() {
4140
}
4241

4342
func (p phpRunner) ListTests() ([]string, error) {
44-
var testSlice []string
45-
46-
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
47-
name := filepath.Base(path)
48-
49-
if info.IsDir() || !testRegEx.MatchString(name) {
50-
return nil
51-
}
52-
53-
relPath, err := filepath.Rel(dir, path)
54-
if err != nil {
55-
return err
56-
}
57-
testSlice = append(testSlice, relPath)
58-
return nil
59-
})
60-
43+
testSlice, err := common.Search(dir, testRegEx)
6144
if err != nil {
62-
return nil, fmt.Errorf("walking %q: %v", dir, err)
45+
return nil, err
6346
}
64-
6547
return testSlice, nil
6648
}
6749

test/runtimes/python/proctor-python.go

+13-29
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ import (
2121
"os"
2222
"os/exec"
2323
"path/filepath"
24-
"regexp"
24+
"strings"
2525

2626
"gvisor.dev/gvisor/test/runtimes/common"
2727
)
2828

2929
var (
30-
dir = os.Getenv("LANG_DIR")
31-
testDir = filepath.Join(dir, "Lib", "test")
32-
testRegEx = regexp.MustCompile(`^test_.+\.py$`)
30+
dir = os.Getenv("LANG_DIR")
3331
)
3432

3533
type pythonRunner struct {
@@ -42,37 +40,23 @@ func main() {
4240
}
4341

4442
func (p pythonRunner) ListTests() ([]string, error) {
45-
var testSlice []string
46-
47-
err := filepath.Walk(testDir, func(path string, info os.FileInfo, err error) error {
48-
name := filepath.Base(path)
49-
50-
if info.IsDir() || !testRegEx.MatchString(name) {
51-
return nil
52-
}
53-
54-
relPath, err := filepath.Rel(testDir, path)
55-
if err != nil {
56-
return err
57-
}
58-
testSlice = append(testSlice, relPath)
59-
return nil
60-
})
61-
43+
args := []string{"-m", "test", "--list-tests"}
44+
cmd := exec.Command(filepath.Join(dir, "python"), args...)
45+
cmd.Stderr = os.Stderr
46+
out, err := cmd.Output()
6247
if err != nil {
63-
return nil, fmt.Errorf("walking %q: %v", testDir, err)
48+
return nil, fmt.Errorf("failed to list: %v", err)
6449
}
65-
66-
return testSlice, nil
50+
var toolSlice []string
51+
for _, test := range strings.Split(string(out), "\n") {
52+
toolSlice = append(toolSlice, test)
53+
}
54+
return toolSlice, nil
6755
}
6856

6957
func (p pythonRunner) RunTest(test string) error {
70-
// Python tests need to be run in the directory in which they exist.
71-
// Split the filename from it's directory and execute in the correct directory.
72-
relDir, file := filepath.Split(test)
73-
args := []string{"-m", "test", file}
58+
args := []string{"-m", "test", test}
7459
cmd := exec.Command(filepath.Join(dir, "python"), args...)
75-
cmd.Dir = filepath.Join(testDir, relDir)
7660
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
7761
if err := cmd.Run(); err != nil {
7862
return fmt.Errorf("failed to run: %v", err)

0 commit comments

Comments
 (0)