]> Cypherpunks repositories - gostls13.git/commitdiff
misc/cgo/life: fix tests in module mode
authorBryan C. Mills <bcmills@google.com>
Fri, 22 Feb 2019 17:22:10 +0000 (12:22 -0500)
committerBryan C. Mills <bcmills@google.com>
Sun, 24 Feb 2019 00:41:11 +0000 (00:41 +0000)
Updates #30228

Change-Id: Ie972694254d2195ca9760ea7ffb6073e01c52488
Reviewed-on: https://go-review.googlesource.com/c/163422
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
misc/cgo/life/life_test.go [new file with mode: 0644]
misc/cgo/life/overlaydir_test.go [new file with mode: 0644]
misc/cgo/life/testdata/c-life.c [moved from misc/cgo/life/c-life.c with 100% similarity]
misc/cgo/life/testdata/life.go [moved from misc/cgo/life/life.go with 98% similarity]
misc/cgo/life/testdata/life.h [moved from misc/cgo/life/life.h with 100% similarity]
misc/cgo/life/testdata/main.go [moved from misc/cgo/life/main.go with 94% similarity]
misc/cgo/life/testdata/main.out [moved from misc/cgo/life/main.out with 100% similarity]
src/cmd/dist/test.go

diff --git a/misc/cgo/life/life_test.go b/misc/cgo/life/life_test.go
new file mode 100644 (file)
index 0000000..3b17ada
--- /dev/null
@@ -0,0 +1,60 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package life_test
+
+import (
+       "bytes"
+       "io/ioutil"
+       "log"
+       "os"
+       "os/exec"
+       "path/filepath"
+       "strings"
+       "testing"
+)
+
+func TestMain(m *testing.M) {
+       log.SetFlags(log.Lshortfile)
+       os.Exit(testMain(m))
+}
+
+func testMain(m *testing.M) int {
+       GOPATH, err := ioutil.TempDir("", "cgolife")
+       if err != nil {
+               log.Panic(err)
+       }
+       defer os.RemoveAll(GOPATH)
+       os.Setenv("GOPATH", GOPATH)
+
+       // Copy testdata into GOPATH/src/cgolife, along with a go.mod file
+       // declaring the same path.
+       modRoot := filepath.Join(GOPATH, "src", "cgolife")
+       if err := overlayDir(modRoot, "testdata"); err != nil {
+               log.Panic(err)
+       }
+       if err := os.Chdir(modRoot); err != nil {
+               log.Panic(err)
+       }
+       if err := ioutil.WriteFile("go.mod", []byte("module cgolife\n"), 0666); err != nil {
+               log.Panic(err)
+       }
+
+       return m.Run()
+}
+
+func TestTestRun(t *testing.T) {
+       out, err := exec.Command("go", "env", "GOROOT").Output()
+       if err != nil {
+               t.Fatal(err)
+       }
+       GOROOT := string(bytes.TrimSpace(out))
+
+       cmd := exec.Command("go", "run", filepath.Join(GOROOT, "test", "run.go"), "-", ".")
+       out, err = cmd.CombinedOutput()
+       if err != nil {
+               t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
+       }
+       t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
+}
diff --git a/misc/cgo/life/overlaydir_test.go b/misc/cgo/life/overlaydir_test.go
new file mode 100644 (file)
index 0000000..f381ea6
--- /dev/null
@@ -0,0 +1,81 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package life_test
+
+import (
+       "io"
+       "os"
+       "path/filepath"
+       "strings"
+)
+
+// overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
+//
+// TODO: Once we no longer need to support the misc module in GOPATH mode,
+// factor this function out into a package to reduce duplication.
+func overlayDir(dstRoot, srcRoot string) error {
+       dstRoot = filepath.Clean(dstRoot)
+       if err := os.MkdirAll(dstRoot, 0777); err != nil {
+               return err
+       }
+
+       symBase, err := filepath.Rel(srcRoot, dstRoot)
+       if err != nil {
+               symBase, err = filepath.Abs(srcRoot)
+               if err != nil {
+                       return err
+               }
+       }
+
+       return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
+               if err != nil || srcPath == srcRoot {
+                       return err
+               }
+
+               suffix := strings.TrimPrefix(srcPath, srcRoot)
+               for len(suffix) > 0 && suffix[0] == filepath.Separator {
+                       suffix = suffix[1:]
+               }
+               dstPath := filepath.Join(dstRoot, suffix)
+
+               perm := info.Mode() & os.ModePerm
+               if info.Mode()&os.ModeSymlink != 0 {
+                       info, err = os.Stat(srcPath)
+                       if err != nil {
+                               return err
+                       }
+                       perm = info.Mode() & os.ModePerm
+               }
+
+               // Always copy directories (don't symlink them).
+               // If we add a file in the overlay, we don't want to add it in the original.
+               if info.IsDir() {
+                       return os.Mkdir(dstPath, perm)
+               }
+
+               // If the OS supports symlinks, use them instead of copying bytes.
+               if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
+                       return nil
+               }
+
+               // Otherwise, copy the bytes.
+               src, err := os.Open(srcPath)
+               if err != nil {
+                       return err
+               }
+               defer src.Close()
+
+               dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
+               if err != nil {
+                       return err
+               }
+
+               _, err = io.Copy(dst, src)
+               if closeErr := dst.Close(); err == nil {
+                       err = closeErr
+               }
+               return err
+       })
+}
similarity index 98%
rename from misc/cgo/life/life.go
rename to misc/cgo/life/testdata/life.go
index 170a620c8788eed7e804741b21af7aa6454f4bd6..2e0af81d05f6323fa57cffe657166b148d6138bd 100644 (file)
@@ -4,7 +4,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package life
+package cgolife
 
 // #include "life.h"
 import "C"
similarity index 94%
rename from misc/cgo/life/main.go
rename to misc/cgo/life/testdata/main.go
index 145a273bdd1fd9d6e945f87e9f61caa74da6e8f8..cc2ca7c74237458cc0eae4f4bb3022aebef8caa9 100644 (file)
@@ -14,7 +14,7 @@ import (
        "flag"
        "fmt"
 
-       "."
+       "cgolife"
 )
 
 const MAXDIM = 100
@@ -34,7 +34,7 @@ func main() {
                }
        }
 
-       life.Run(*gen, *dim, *dim, a[:])
+       cgolife.Run(*gen, *dim, *dim, a[:])
 
        for i := 0; i < *dim; i++ {
                for j := 0; j < *dim; j++ {
index 1eabb85639d643ba36c0aff6fd9e88e0fc83fa33..c5cc6dcb3c88e3687d685e191f038a305a6a3bf5 100644 (file)
@@ -619,7 +619,7 @@ func (t *tester) registerTests() {
                        name:    "cgo_life",
                        heading: "../misc/cgo/life",
                        fn: func(dt *distTest) error {
-                               t.addCmd(dt, "misc/cgo/life", "go", "run", filepath.Join(os.Getenv("GOROOT"), "test/run.go"), "-", ".")
+                               t.addCmd(dt, "misc/cgo/life", t.goTest(), t.timeout(120))
                                return nil
                        },
                })