diff options
author | Brett Landau <brettlandau@google.com> | 2019-08-01 18:49:40 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2019-08-01 18:53:35 -0700 |
commit | 6a1ac3407743adf7c0493ab9da97a1dff4e4b2ac (patch) | |
tree | a22cc3387e64f1dd69f18d50de6f66b113d90615 /test/runtimes | |
parent | 3eff0531adc6d28eea49be65fa747e2b3163f44d (diff) |
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
Diffstat (limited to 'test/runtimes')
-rw-r--r-- | test/runtimes/common/common.go | 29 | ||||
-rw-r--r-- | test/runtimes/go/proctor-go.go | 43 | ||||
-rw-r--r-- | test/runtimes/nodejs/proctor-nodejs.go | 21 | ||||
-rw-r--r-- | test/runtimes/php/proctor-php.go | 22 | ||||
-rw-r--r-- | test/runtimes/python/proctor-python.go | 42 |
5 files changed, 61 insertions, 96 deletions
diff --git a/test/runtimes/common/common.go b/test/runtimes/common/common.go index 3f289d459..0ff87fa8b 100644 --- a/test/runtimes/common/common.go +++ b/test/runtimes/common/common.go @@ -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 { diff --git a/test/runtimes/go/proctor-go.go b/test/runtimes/go/proctor-go.go index 3751c2c81..3eb24576e 100644 --- a/test/runtimes/go/proctor-go.go +++ b/test/runtimes/go/proctor-go.go @@ -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 - } - - if !testRegEx.MatchString(name) { - return nil - } - - if exclDirs.MatchString(path) { - 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) } - - 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 { diff --git a/test/runtimes/nodejs/proctor-nodejs.go b/test/runtimes/nodejs/proctor-nodejs.go index 0885516e9..5f21e046b 100644 --- a/test/runtimes/nodejs/proctor-nodejs.go +++ b/test/runtimes/nodejs/proctor-nodejs.go @@ -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 } diff --git a/test/runtimes/php/proctor-php.go b/test/runtimes/php/proctor-php.go index 3ee4cf056..e6c5fabdf 100644 --- a/test/runtimes/php/proctor-php.go +++ b/test/runtimes/php/proctor-php.go @@ -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 } diff --git a/test/runtimes/python/proctor-python.go b/test/runtimes/python/proctor-python.go index 816b1f44a..35e28a7df 100644 --- a/test/runtimes/python/proctor-python.go +++ b/test/runtimes/python/proctor-python.go @@ -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) |