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
This commit is contained in:
Brett Landau
2019-08-01 18:53:35 -07:00
committed by gVisor bot
parent 3eff0531ad
commit 6a1ac34077
5 changed files with 61 additions and 96 deletions
+29
View File
@@ -19,6 +19,8 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
)
var (
@@ -71,6 +73,33 @@ func LaunchFunc(tr TestRunner) error {
return nil
}
// Search uses filepath.Walk to perform a search of the disk for test files
// and returns a string slice of tests.
func Search(root string, testFilter *regexp.Regexp) ([]string, error) {
var testSlice []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
name := filepath.Base(path)
if info.IsDir() || !testFilter.MatchString(name) {
return nil
}
relPath, err := filepath.Rel(root, path)
if err != nil {
return err
}
testSlice = append(testSlice, relPath)
return nil
})
if err != nil {
return nil, fmt.Errorf("walking %q: %v", root, err)
}
return testSlice, nil
}
func runAllTests(tr TestRunner) error {
tests, err := tr.ListTests()
if err != nil {
+15 -28
View File
@@ -40,7 +40,7 @@ var (
// Directories with .dir contain helper files for tests.
// Exclude benchmarks and stress tests.
exclDirs = regexp.MustCompile(`^.+\/(bench|stress)\/.+$|^.+\.dir.+$`)
dirFilter = regexp.MustCompile(`^(bench|stress)\/.+$|^.+\.dir.+$`)
)
type goRunner struct {
@@ -59,36 +59,27 @@ func (g goRunner) ListTests() ([]string, error) {
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
log.Fatalf("Failed to list: %v", err)
return nil, fmt.Errorf("failed to list: %v", err)
}
var testSlice []string
var toolSlice []string
for _, test := range strings.Split(string(out), "\n") {
testSlice = append(testSlice, test)
toolSlice = append(toolSlice, test)
}
// Go tests on disk.
if err := filepath.Walk(testDir, func(path string, info os.FileInfo, err error) error {
name := filepath.Base(path)
if info.IsDir() {
return nil
diskSlice, err := common.Search(testDir, testRegEx)
if err != nil {
return nil, err
}
// Remove items from /bench/, /stress/ and .dir files
diskFiltered := diskSlice[:0]
for _, file := range diskSlice {
if !dirFilter.MatchString(file) {
diskFiltered = append(diskFiltered, file)
}
if !testRegEx.MatchString(name) {
return nil
}
if exclDirs.MatchString(path) {
return nil
}
testSlice = append(testSlice, path)
return nil
}); err != nil {
return nil, fmt.Errorf("walking %q: %v", testDir, err)
}
return testSlice, nil
return append(toolSlice, diskFiltered...), nil
}
func (g goRunner) RunTest(test string) error {
@@ -96,11 +87,7 @@ func (g goRunner) RunTest(test string) error {
// This will determine whether or not it is a Go test on disk.
if strings.HasSuffix(test, ".go") {
// Test has suffix ".go" which indicates a disk test, run it as such.
relPath, err := filepath.Rel(testDir, test)
if err != nil {
return fmt.Errorf("failed to get rel path: %v", err)
}
cmd := exec.Command(goBin, "run", "run.go", "-v", "--", relPath)
cmd := exec.Command(goBin, "run", "run.go", "-v", "--", test)
cmd.Dir = testDir
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
+2 -19
View File
@@ -42,27 +42,10 @@ func main() {
}
func (n nodejsRunner) ListTests() ([]string, error) {
var testSlice []string
err := filepath.Walk(testDir, func(path string, info os.FileInfo, err error) error {
name := filepath.Base(path)
if info.IsDir() || !testRegEx.MatchString(name) {
return nil
}
relPath, err := filepath.Rel(testDir, path)
if err != nil {
return err
}
testSlice = append(testSlice, relPath)
return nil
})
testSlice, err := common.Search(testDir, testRegEx)
if err != nil {
return nil, fmt.Errorf("walking %q: %v", testDir, err)
return nil, err
}
return testSlice, nil
}
+2 -20
View File
@@ -20,7 +20,6 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"gvisor.dev/gvisor/test/runtimes/common"
@@ -41,27 +40,10 @@ func main() {
}
func (p phpRunner) ListTests() ([]string, error) {
var testSlice []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
name := filepath.Base(path)
if info.IsDir() || !testRegEx.MatchString(name) {
return nil
}
relPath, err := filepath.Rel(dir, path)
if err != nil {
return err
}
testSlice = append(testSlice, relPath)
return nil
})
testSlice, err := common.Search(dir, testRegEx)
if err != nil {
return nil, fmt.Errorf("walking %q: %v", dir, err)
return nil, err
}
return testSlice, nil
}
+13 -29
View File
@@ -21,15 +21,13 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"gvisor.dev/gvisor/test/runtimes/common"
)
var (
dir = os.Getenv("LANG_DIR")
testDir = filepath.Join(dir, "Lib", "test")
testRegEx = regexp.MustCompile(`^test_.+\.py$`)
dir = os.Getenv("LANG_DIR")
)
type pythonRunner struct {
@@ -42,37 +40,23 @@ func main() {
}
func (p pythonRunner) ListTests() ([]string, error) {
var testSlice []string
err := filepath.Walk(testDir, func(path string, info os.FileInfo, err error) error {
name := filepath.Base(path)
if info.IsDir() || !testRegEx.MatchString(name) {
return nil
}
relPath, err := filepath.Rel(testDir, path)
if err != nil {
return err
}
testSlice = append(testSlice, relPath)
return nil
})
args := []string{"-m", "test", "--list-tests"}
cmd := exec.Command(filepath.Join(dir, "python"), args...)
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("walking %q: %v", testDir, err)
return nil, fmt.Errorf("failed to list: %v", err)
}
return testSlice, nil
var toolSlice []string
for _, test := range strings.Split(string(out), "\n") {
toolSlice = append(toolSlice, test)
}
return toolSlice, nil
}
func (p pythonRunner) RunTest(test string) error {
// Python tests need to be run in the directory in which they exist.
// Split the filename from it's directory and execute in the correct directory.
relDir, file := filepath.Split(test)
args := []string{"-m", "test", file}
args := []string{"-m", "test", test}
cmd := exec.Command(filepath.Join(dir, "python"), args...)
cmd.Dir = filepath.Join(testDir, relDir)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to run: %v", err)