Skip to content

Commit c6611b2

Browse files
author
Bryan C. Mills
committed
misc/cgo/stdio: fix tests in module mode
Updates #30228 Change-Id: I4d213c6fe68c47ccb877f13b55128e035f76a26b Reviewed-on: https://go-review.googlesource.com/c/163421 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent eb2d1cd commit c6611b2

File tree

12 files changed

+145
-4
lines changed

12 files changed

+145
-4
lines changed

misc/cgo/stdio/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 stdio_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+
}

misc/cgo/stdio/stdio_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 stdio_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("", "cgostdio")
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/cgostdio, along with a go.mod file
32+
// declaring the same path.
33+
modRoot := filepath.Join(GOPATH, "src", "cgostdio")
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 cgostdio\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/stdio/chain.go renamed to misc/cgo/stdio/testdata/chain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"runtime"
1515
"strconv"
1616

17-
"../stdio"
17+
"cgostdio/stdio"
1818
)
1919

2020
const N = 10
File renamed without changes.

misc/cgo/stdio/fib.go renamed to misc/cgo/stdio/testdata/fib.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"runtime"
1818
"strconv"
1919

20-
"../stdio"
20+
"cgostdio/stdio"
2121
)
2222

2323
func fibber(c, out chan int64, i int64) {
File renamed without changes.

misc/cgo/stdio/hello.go renamed to misc/cgo/stdio/testdata/hello.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
package main
1010

11-
import "../stdio"
11+
import "cgostdio/stdio"
1212

1313
func main() {
1414
stdio.Stdout.WriteString(stdio.Greeting + "\n")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/cmd/dist/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ func (t *tester) registerTests() {
611611
name: "cgo_stdio",
612612
heading: "../misc/cgo/stdio",
613613
fn: func(dt *distTest) error {
614-
t.addCmd(dt, "misc/cgo/stdio", "go", "run", filepath.Join(os.Getenv("GOROOT"), "test/run.go"), "-", ".")
614+
t.addCmd(dt, "misc/cgo/stdio", t.goTest(), t.timeout(120))
615615
return nil
616616
},
617617
})

0 commit comments

Comments
 (0)