summaryrefslogtreecommitdiffhomepage
path: root/runsc/test/runtimes/proctor-go.go
diff options
context:
space:
mode:
authorBrett Landau <brettlandau@google.com>2019-07-23 13:31:30 -0700
committergVisor bot <gvisor-bot@google.com>2019-07-23 13:32:43 -0700
commit57745994384ee1ff94fc7bed4f814ba75e39d48e (patch)
treeb1a4cc396b095259b66e8689e9046fd3a5e5de5e /runsc/test/runtimes/proctor-go.go
parent12c256568ba784c07cf73822359faac2971e8306 (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
Diffstat (limited to 'runsc/test/runtimes/proctor-go.go')
-rw-r--r--runsc/test/runtimes/proctor-go.go101
1 files changed, 55 insertions, 46 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)
}
}