diff options
author | Brett Landau <brettlandau@google.com> | 2019-07-23 13:31:30 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2019-07-23 13:32:43 -0700 |
commit | 57745994384ee1ff94fc7bed4f814ba75e39d48e (patch) | |
tree | b1a4cc396b095259b66e8689e9046fd3a5e5de5e | |
parent | 12c256568ba784c07cf73822359faac2971e8306 (diff) |
Make runAllTests() consistent with listTests().
This change has the listTests() function return
a string slice of all the tests. Originally, I
planned not to modify the listTests() function
and instead capture the output of it and then
iterate through the captured output. I decided
against this approach as most of the test binaries
already produce a slice as they collect tests
through filepath.Walk(). Now I use this slice
and return it so that I can iterate through in
runAllTests() and also when printing out the tests.
PiperOrigin-RevId: 259599782
-rw-r--r-- | runsc/test/runtimes/proctor-go.go | 101 | ||||
-rw-r--r-- | runsc/test/runtimes/proctor-java.go | 43 | ||||
-rw-r--r-- | runsc/test/runtimes/proctor-nodejs.go | 41 | ||||
-rw-r--r-- | runsc/test/runtimes/proctor-php.go | 41 | ||||
-rw-r--r-- | runsc/test/runtimes/proctor-python.go | 41 |
5 files changed, 169 insertions, 98 deletions
diff --git a/runsc/test/runtimes/proctor-go.go b/runsc/test/runtimes/proctor-go.go index 619887327..c5387e21d 100644 --- a/runsc/test/runtimes/proctor-go.go +++ b/runsc/test/runtimes/proctor-go.go @@ -28,6 +28,7 @@ import ( "os/exec" "path/filepath" "regexp" + "strings" ) var ( @@ -52,27 +53,41 @@ func main() { os.Exit(1) } if *list { - listTests() + tests, err := listTests() + if err != nil { + log.Fatalf("Failed to list tests: %v", err) + } + for _, test := range tests { + fmt.Println(test) + } return } if *version { fmt.Println("Go version: ", os.Getenv("LANG_VER"), " is installed.") return } - runTest(*test) + if *test != "" { + runTest(*test) + return + } + runAllTests() } -func listTests() { +func listTests() ([]string, error) { // Go tool dist test tests. args := []string{"tool", "dist", "test", "-list"} cmd := exec.Command(filepath.Join(dir, "bin/go"), args...) - cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr - if err := cmd.Run(); err != nil { + cmd.Stderr = os.Stderr + out, err := cmd.Output() + if err != nil { log.Fatalf("Failed to list: %v", err) } + var testSlice []string + for _, test := range strings.Split(string(out), "\n") { + testSlice = append(testSlice, test) + } // Go tests on disk. - var tests []string if err := filepath.Walk(testDir, func(path string, info os.FileInfo, err error) error { name := filepath.Base(path) @@ -88,15 +103,13 @@ func listTests() { return nil } - tests = append(tests, path) + testSlice = append(testSlice, path) return nil }); err != nil { - log.Fatalf("Failed to walk %q: %v", dir, err) + return nil, fmt.Errorf("walking %q: %v", testDir, err) } - for _, file := range tests { - fmt.Println(file) - } + return testSlice, nil } func runTest(test string) { @@ -110,43 +123,39 @@ func runTest(test string) { "run.go", "-v", } - if test != "" { - // Check if test exists on disk by searching for file of the same name. - // This will determine whether or not it is a Go test on disk. - if _, err := os.Stat(test); err == nil { - relPath, err := filepath.Rel(testDir, test) - if err != nil { - log.Fatalf("Failed to get rel path: %v", err) - } - diskArgs = append(diskArgs, "--", relPath) - cmd := exec.Command(filepath.Join(dir, "bin/go"), diskArgs...) - cmd.Dir = testDir - cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr - if err := cmd.Run(); err != nil { - log.Fatalf("Failed to run: %v", err) - } - } else if os.IsNotExist(err) { - // File was not found, try running as Go tool test. - toolArgs = append(toolArgs, "-run", test) - cmd := exec.Command(filepath.Join(dir, "bin/go"), toolArgs...) - cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr - if err := cmd.Run(); err != nil { - log.Fatalf("Failed to run: %v", err) - } - } else { - log.Fatalf("Error searching for test: %v", err) + // Check if test exists on disk by searching for file of the same name. + // This will determine whether or not it is a Go test on disk. + if _, err := os.Stat(test); err == nil { + relPath, err := filepath.Rel(testDir, test) + if err != nil { + log.Fatalf("Failed to get rel path: %v", err) } - return + diskArgs = append(diskArgs, "--", relPath) + cmd := exec.Command(filepath.Join(dir, "bin/go"), diskArgs...) + cmd.Dir = testDir + cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr + if err := cmd.Run(); err != nil { + log.Fatalf("Failed to run: %v", err) + } + } else if os.IsNotExist(err) { + // File was not found, try running as Go tool test. + toolArgs = append(toolArgs, "-run", test) + cmd := exec.Command(filepath.Join(dir, "bin/go"), toolArgs...) + cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr + if err := cmd.Run(); err != nil { + log.Fatalf("Failed to run: %v", err) + } + } else { + log.Fatalf("Error searching for test: %v", err) } - runAllTool := exec.Command(filepath.Join(dir, "bin/go"), toolArgs...) - runAllTool.Stdout, runAllTool.Stderr = os.Stdout, os.Stderr - if err := runAllTool.Run(); err != nil { - log.Fatalf("Failed to run: %v", err) +} + +func runAllTests() { + tests, err := listTests() + if err != nil { + log.Fatalf("Failed to list tests: %v", err) } - runAllDisk := exec.Command(filepath.Join(dir, "bin/go"), diskArgs...) - runAllDisk.Dir = testDir - runAllDisk.Stdout, runAllDisk.Stderr = os.Stdout, os.Stderr - if err := runAllDisk.Run(); err != nil { - log.Fatalf("Failed to run disk tests: %v", err) + for _, test := range tests { + runTest(test) } } diff --git a/runsc/test/runtimes/proctor-java.go b/runsc/test/runtimes/proctor-java.go index 50f3789dc..0177f421d 100644 --- a/runsc/test/runtimes/proctor-java.go +++ b/runsc/test/runtimes/proctor-java.go @@ -44,17 +44,27 @@ func main() { os.Exit(1) } if *list { - listTests() + tests, err := listTests() + if err != nil { + log.Fatalf("Failed to list tests: %v", err) + } + for _, test := range tests { + fmt.Println(test) + } return } if *version { fmt.Println("Java version: ", os.Getenv("LANG_VER"), " is installed.") return } - runTest(*test) + if *test != "" { + runTest(*test) + return + } + runAllTests() } -func listTests() { +func listTests() ([]string, error) { args := []string{ "-dir:test/jdk", "-ignore:quiet", @@ -69,25 +79,32 @@ func listTests() { cmd.Stderr = os.Stderr out, err := cmd.Output() if err != nil { - log.Fatalf("Failed to list: %v", err) + return nil, fmt.Errorf("jtreg -listtests : %v", err) } - allTests := string(out) - for _, test := range strings.Split(allTests, "\n") { + var testSlice []string + for _, test := range strings.Split(string(out), "\n") { if !exclDirs.MatchString(test) { - fmt.Println(test) + testSlice = append(testSlice, test) } } + return testSlice, nil } func runTest(test string) { - // TODO(brettlandau): Change to use listTests() for running all tests. - cmd := exec.Command("make", "run-test-tier1") - if test != "" { - args := []string{"-dir:test/jdk/", test} - cmd = exec.Command(jtreg, args...) - } + args := []string{"-dir:test/jdk/", test} + cmd := exec.Command(jtreg, args...) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr if err := cmd.Run(); err != nil { log.Fatalf("Failed to run: %v", err) } } + +func runAllTests() { + tests, err := listTests() + if err != nil { + log.Fatalf("Failed to list tests: %v", err) + } + for _, test := range tests { + runTest(test) + } +} diff --git a/runsc/test/runtimes/proctor-nodejs.go b/runsc/test/runtimes/proctor-nodejs.go index 771286dd1..8ddfb67fe 100644 --- a/runsc/test/runtimes/proctor-nodejs.go +++ b/runsc/test/runtimes/proctor-nodejs.go @@ -42,18 +42,28 @@ func main() { os.Exit(1) } if *list { - listTests() + tests, err := listTests() + if err != nil { + log.Fatalf("Failed to list tests: %v", err) + } + for _, test := range tests { + fmt.Println(test) + } return } if *version { fmt.Println("Node.js version: ", os.Getenv("LANG_VER"), " is installed.") return } - runTest(*test) + if *test != "" { + runTest(*test) + return + } + runAllTests() } -func listTests() { - var files []string +func listTests() ([]string, error) { + var testSlice []string root := filepath.Join(dir, "test") err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { @@ -67,27 +77,32 @@ func listTests() { if err != nil { return err } - files = append(files, relPath) + testSlice = append(testSlice, relPath) return nil }) if err != nil { - log.Fatalf("Failed to walk %q: %v", root, err) + return nil, fmt.Errorf("walking %q: %v", root, err) } - for _, file := range files { - fmt.Println(file) - } + return testSlice, nil } func runTest(test string) { - args := []string{filepath.Join(dir, "tools", "test.py")} - if test != "" { - args = append(args, test) - } + args := []string{filepath.Join(dir, "tools", "test.py"), test} cmd := exec.Command("/usr/bin/python", args...) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr if err := cmd.Run(); err != nil { log.Fatalf("Failed to run: %v", err) } } + +func runAllTests() { + tests, err := listTests() + if err != nil { + log.Fatalf("Failed to list tests: %v", err) + } + for _, test := range tests { + runTest(test) + } +} diff --git a/runsc/test/runtimes/proctor-php.go b/runsc/test/runtimes/proctor-php.go index 3d305c709..9dfb33b04 100644 --- a/runsc/test/runtimes/proctor-php.go +++ b/runsc/test/runtimes/proctor-php.go @@ -42,18 +42,28 @@ func main() { os.Exit(1) } if *list { - listTests() + tests, err := listTests() + if err != nil { + log.Fatalf("Failed to list tests: %v", err) + } + for _, test := range tests { + fmt.Println(test) + } return } if *version { fmt.Println("PHP version: ", os.Getenv("LANG_VER"), " is installed.") return } - runTest(*test) + if *test != "" { + runTest(*test) + return + } + runAllTests() } -func listTests() { - var files []string +func listTests() ([]string, error) { + var testSlice []string err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { name := filepath.Base(path) @@ -66,27 +76,32 @@ func listTests() { if err != nil { return err } - files = append(files, relPath) + testSlice = append(testSlice, relPath) return nil }) if err != nil { - log.Fatalf("Failed to walk %q: %v", dir, err) + return nil, fmt.Errorf("walking %q: %v", dir, err) } - for _, file := range files { - fmt.Println(file) - } + return testSlice, nil } func runTest(test string) { - args := []string{"test", "TESTS="} - if test != "" { - args[1] = args[1] + test - } + args := []string{"test", "TESTS=" + test} cmd := exec.Command("make", args...) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr if err := cmd.Run(); err != nil { log.Fatalf("Failed to run: %v", err) } } + +func runAllTests() { + tests, err := listTests() + if err != nil { + log.Fatalf("Failed to list tests: %v", err) + } + for _, test := range tests { + runTest(test) + } +} diff --git a/runsc/test/runtimes/proctor-python.go b/runsc/test/runtimes/proctor-python.go index 5e8d830e1..73c8deb49 100644 --- a/runsc/test/runtimes/proctor-python.go +++ b/runsc/test/runtimes/proctor-python.go @@ -42,18 +42,28 @@ func main() { os.Exit(1) } if *list { - listTests() + tests, err := listTests() + if err != nil { + log.Fatalf("Failed to list tests: %v", err) + } + for _, test := range tests { + fmt.Println(test) + } return } if *version { fmt.Println("Python version: ", os.Getenv("LANG_VER"), " is installed.") return } - runTest(*test) + if *test != "" { + runTest(*test) + return + } + runAllTests() } -func listTests() { - var files []string +func listTests() ([]string, error) { + var testSlice []string root := filepath.Join(dir, "Lib/test") err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { @@ -67,27 +77,32 @@ func listTests() { if err != nil { return err } - files = append(files, relPath) + testSlice = append(testSlice, relPath) return nil }) if err != nil { - log.Fatalf("Failed to walk %q: %v", root, err) + return nil, fmt.Errorf("walking %q: %v", root, err) } - for _, file := range files { - fmt.Println(file) - } + return testSlice, nil } func runTest(test string) { - args := []string{"-m", "test"} - if test != "" { - args = append(args, test) - } + args := []string{"-m", "test", test} cmd := exec.Command(filepath.Join(dir, "python"), args...) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr if err := cmd.Run(); err != nil { log.Fatalf("Failed to run: %v", err) } } + +func runAllTests() { + tests, err := listTests() + if err != nil { + log.Fatalf("Failed to list tests: %v", err) + } + for _, test := range tests { + runTest(test) + } +} |