Skip to content

Commit 01f34cb

Browse files
author
Bryan C. Mills
committed
misc/cgo/life: fix tests in module mode
Updates #30228 Change-Id: Ie972694254d2195ca9760ea7ffb6073e01c52488 Reviewed-on: https://go-review.googlesource.com/c/163422 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent c6611b2 commit 01f34cb

File tree

8 files changed

+145
-4
lines changed

8 files changed

+145
-4
lines changed

misc/cgo/life/life_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package life_test
6+
7+
import (
8+
"bytes"
9+
"io/ioutil"
10+
"log"
11+
"os"
12+
"os/exec"
13+
"path/filepath"
14+
"strings"
15+
"testing"
16+
)
17+
18+
func TestMain(m *testing.M) {
19+
log.SetFlags(log.Lshortfile)
20+
os.Exit(testMain(m))
21+
}
22+
23+
func testMain(m *testing.M) int {
24+
GOPATH, err := ioutil.TempDir("", "cgolife")
25+
if err != nil {
26+
log.Panic(err)
27+
}
28+
defer os.RemoveAll(GOPATH)
29+
os.Setenv("GOPATH", GOPATH)
30+
31+
// Copy testdata into GOPATH/src/cgolife, along with a go.mod file
32+
// declaring the same path.
33+
modRoot := filepath.Join(GOPATH, "src", "cgolife")
34+
if err := overlayDir(modRoot, "testdata"); err != nil {
35+
log.Panic(err)
36+
}
37+
if err := os.Chdir(modRoot); err != nil {
38+
log.Panic(err)
39+
}
40+
if err := ioutil.WriteFile("go.mod", []byte("module cgolife\n"), 0666); err != nil {
41+
log.Panic(err)
42+
}
43+
44+
return m.Run()
45+
}
46+
47+
func TestTestRun(t *testing.T) {
48+
out, err := exec.Command("go", "env", "GOROOT").Output()
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
GOROOT := string(bytes.TrimSpace(out))
53+
54+
cmd := exec.Command("go", "run", filepath.Join(GOROOT, "test", "run.go"), "-", ".")
55+
out, err = cmd.CombinedOutput()
56+
if err != nil {
57+
t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
58+
}
59+
t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
60+
}

misc/cgo/life/overlaydir_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package life_test
6+
7+
import (
8+
"io"
9+
"os"
10+
"path/filepath"
11+
"strings"
12+
)
13+
14+
// overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
15+
//
16+
// TODO: Once we no longer need to support the misc module in GOPATH mode,
17+
// factor this function out into a package to reduce duplication.
18+
func overlayDir(dstRoot, srcRoot string) error {
19+
dstRoot = filepath.Clean(dstRoot)
20+
if err := os.MkdirAll(dstRoot, 0777); err != nil {
21+
return err
22+
}
23+
24+
symBase, err := filepath.Rel(srcRoot, dstRoot)
25+
if err != nil {
26+
symBase, err = filepath.Abs(srcRoot)
27+
if err != nil {
28+
return err
29+
}
30+
}
31+
32+
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
33+
if err != nil || srcPath == srcRoot {
34+
return err
35+
}
36+
37+
suffix := strings.TrimPrefix(srcPath, srcRoot)
38+
for len(suffix) > 0 && suffix[0] == filepath.Separator {
39+
suffix = suffix[1:]
40+
}
41+
dstPath := filepath.Join(dstRoot, suffix)
42+
43+
perm := info.Mode() & os.ModePerm
44+
if info.Mode()&os.ModeSymlink != 0 {
45+
info, err = os.Stat(srcPath)
46+
if err != nil {
47+
return err
48+
}
49+
perm = info.Mode() & os.ModePerm
50+
}
51+
52+
// Always copy directories (don't symlink them).
53+
// If we add a file in the overlay, we don't want to add it in the original.
54+
if info.IsDir() {
55+
return os.Mkdir(dstPath, perm)
56+
}
57+
58+
// If the OS supports symlinks, use them instead of copying bytes.
59+
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
60+
return nil
61+
}
62+
63+
// Otherwise, copy the bytes.
64+
src, err := os.Open(srcPath)
65+
if err != nil {
66+
return err
67+
}
68+
defer src.Close()
69+
70+
dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
71+
if err != nil {
72+
return err
73+
}
74+
75+
_, err = io.Copy(dst, src)
76+
if closeErr := dst.Close(); err == nil {
77+
err = closeErr
78+
}
79+
return err
80+
})
81+
}
File renamed without changes.

misc/cgo/life/life.go renamed to misc/cgo/life/testdata/life.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Use of this source code is governed by a BSD-style
55
// license that can be found in the LICENSE file.
66

7-
package life
7+
package cgolife
88

99
// #include "life.h"
1010
import "C"
File renamed without changes.

misc/cgo/life/main.go renamed to misc/cgo/life/testdata/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"flag"
1515
"fmt"
1616

17-
"."
17+
"cgolife"
1818
)
1919

2020
const MAXDIM = 100
@@ -34,7 +34,7 @@ func main() {
3434
}
3535
}
3636

37-
life.Run(*gen, *dim, *dim, a[:])
37+
cgolife.Run(*gen, *dim, *dim, a[:])
3838

3939
for i := 0; i < *dim; i++ {
4040
for j := 0; j < *dim; j++ {
File renamed without changes.

src/cmd/dist/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ func (t *tester) registerTests() {
619619
name: "cgo_life",
620620
heading: "../misc/cgo/life",
621621
fn: func(dt *distTest) error {
622-
t.addCmd(dt, "misc/cgo/life", "go", "run", filepath.Join(os.Getenv("GOROOT"), "test/run.go"), "-", ".")
622+
t.addCmd(dt, "misc/cgo/life", t.goTest(), t.timeout(120))
623623
return nil
624624
},
625625
})

0 commit comments

Comments
 (0)