]> Cypherpunks repositories - gostls13.git/commitdiff
test: clean up run.go
authorDmitri Shuralyov <dmitshur@golang.org>
Sun, 9 Feb 2020 06:40:45 +0000 (01:40 -0500)
committerDmitri Shuralyov <dmitshur@golang.org>
Mon, 24 Feb 2020 16:13:21 +0000 (16:13 +0000)
• Inline check function because it's more readable.
• Delete toolPath because it was unused.
• Use strings.TrimPrefix because it's simpler.
• Remove out variable because its value was unused.
• Rename serr to err because it's more consistent.

Change-Id: I084fb4f8b399578834d5eea29a673c386cf3a357
Reviewed-on: https://go-review.googlesource.com/c/go/+/218701
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
test/run.go

index 7129ca32284c486fc638469b7b31a77435e6ae1f..85dc51d1ddd373d4a8d2c01abad609addd8bd9c5 100644 (file)
@@ -166,14 +166,6 @@ func main() {
        }
 }
 
-func toolPath(name string) string {
-       p := filepath.Join(os.Getenv("GOROOT"), "bin", "tool", name)
-       if _, err := os.Stat(p); err != nil {
-               log.Fatalf("didn't find binary at %s", p)
-       }
-       return p
-}
-
 // goTool reports the path of the go tool to use to run the tests.
 // If possible, use the same Go used to run run.go, otherwise
 // fallback to the go version found in the PATH.
@@ -201,10 +193,14 @@ func shardMatch(name string) bool {
 
 func goFiles(dir string) []string {
        f, err := os.Open(dir)
-       check(err)
+       if err != nil {
+               log.Fatal(err)
+       }
        dirnames, err := f.Readdirnames(-1)
        f.Close()
-       check(err)
+       if err != nil {
+               log.Fatal(err)
+       }
        names := []string{}
        for _, name := range dirnames {
                if !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && shardMatch(name) {
@@ -262,12 +258,6 @@ type skipError string
 
 func (s skipError) Error() string { return string(s) }
 
-func check(err error) {
-       if err != nil {
-               log.Fatal(err)
-       }
-}
-
 // test holds the state of a test.
 type test struct {
        dir, gofile string
@@ -361,7 +351,9 @@ func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
        for _, file := range files {
                name := file.Name()
                pkgname, err := getPackageNameFromSource(filepath.Join(longdir, name))
-               check(err)
+               if err != nil {
+                       log.Fatal(err)
+               }
                i, ok := m[pkgname]
                if singlefilepkgs || !ok {
                        i = len(pkgs)
@@ -500,9 +492,7 @@ func (t *test) run() {
                // skip first line
                action = action[nl+1:]
        }
-       if strings.HasPrefix(action, "//") {
-               action = action[2:]
-       }
+       action = strings.TrimPrefix(action, "//")
 
        // Check for build constraints only up to the actual code.
        pkgPos := strings.Index(t.src, "\npackage")
@@ -601,7 +591,9 @@ func (t *test) run() {
        }
 
        err = ioutil.WriteFile(filepath.Join(t.tempDir, t.gofile), srcBytes, 0644)
-       check(err)
+       if err != nil {
+               log.Fatal(err)
+       }
 
        // A few tests (of things like the environment) require these to be set.
        if os.Getenv("GOOS") == "" {
@@ -814,8 +806,10 @@ func (t *test) run() {
                        pflags = append(pflags, flags...)
                        if setpkgpaths {
                                fp := filepath.Join(longdir, gofiles[0])
-                               pkgname, serr := getPackageNameFromSource(fp)
-                               check(serr)
+                               pkgname, err := getPackageNameFromSource(fp)
+                               if err != nil {
+                                       log.Fatal(err)
+                               }
                                pflags = append(pflags, "-p", pkgname)
                        }
                        _, err := compileInDir(runcmd, longdir, pflags, localImports, gofiles...)
@@ -967,13 +961,13 @@ func (t *test) run() {
                longdirgofile := filepath.Join(filepath.Join(cwd, t.dir), t.gofile)
                cmd = append(cmd, flags...)
                cmd = append(cmd, longdirgofile)
-               out, err := runcmd(cmd...)
+               _, err := runcmd(cmd...)
                if err != nil {
                        t.err = err
                        return
                }
                cmd = []string{"./a.exe"}
-               out, err = runcmd(append(cmd, args...)...)
+               out, err := runcmd(append(cmd, args...)...)
                if err != nil {
                        t.err = err
                        return
@@ -1129,7 +1123,9 @@ func (t *test) String() string {
 func (t *test) makeTempDir() {
        var err error
        t.tempDir, err = ioutil.TempDir("", "")
-       check(err)
+       if err != nil {
+               log.Fatal(err)
+       }
        if *keep {
                log.Printf("Temporary directory is %s", t.tempDir)
        }