Skip to content

Commit e155b03

Browse files
author
Bryan C. Mills
committed
cmd/getgo: exec main from TestMain instead of running 'go build' in tests
This was noticed from https://build.golang.org/log/da703ece9e1626eaeabf485e1a3a8180a6bde512, but I suspect not relevant to the getgo test failure observed there. Updates golang/go#28387 Change-Id: I1a156e780beabb13b4df6fd5313d4785aeb26e97 Reviewed-on: https://go-review.googlesource.com/c/tools/+/390075 Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent e562276 commit e155b03

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

cmd/getgo/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
build
2-
testgetgo
32
getgo

cmd/getgo/main_test.go

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,27 @@ import (
1313
"io/ioutil"
1414
"os"
1515
"os/exec"
16-
"runtime"
1716
"testing"
1817
)
1918

20-
const (
21-
testbin = "testgetgo"
22-
)
23-
24-
var (
25-
exeSuffix string // ".exe" on Windows
26-
)
27-
28-
func init() {
29-
if runtime.GOOS == "windows" {
30-
exeSuffix = ".exe"
19+
func TestMain(m *testing.M) {
20+
if os.Getenv("GO_GETGO_TEST_IS_GETGO") != "" {
21+
main()
22+
os.Exit(0)
3123
}
32-
}
3324

34-
// TestMain creates a getgo command for testing purposes and
35-
// deletes it after the tests have been run.
36-
func TestMain(m *testing.M) {
3725
if os.Getenv("GOGET_INTEGRATION") == "" {
3826
fmt.Fprintln(os.Stderr, "main_test: Skipping integration tests with GOGET_INTEGRATION unset")
3927
return
4028
}
4129

42-
args := []string{"build", "-tags", testbin, "-o", testbin + exeSuffix}
43-
out, err := exec.Command("go", args...).CombinedOutput()
44-
if err != nil {
45-
fmt.Fprintf(os.Stderr, "building %s failed: %v\n%s", testbin, err, out)
46-
os.Exit(2)
47-
}
48-
4930
// Don't let these environment variables confuse the test.
5031
os.Unsetenv("GOBIN")
5132
os.Unsetenv("GOPATH")
5233
os.Unsetenv("GIT_ALLOW_PROTOCOL")
5334
os.Unsetenv("PATH")
5435

55-
r := m.Run()
56-
57-
os.Remove(testbin + exeSuffix)
58-
59-
os.Exit(r)
36+
os.Exit(m.Run())
6037
}
6138

6239
func createTmpHome(t *testing.T) string {
@@ -72,12 +49,18 @@ func createTmpHome(t *testing.T) string {
7249
// doRun runs the test getgo command, recording stdout and stderr and
7350
// returning exit status.
7451
func doRun(t *testing.T, args ...string) error {
52+
exe, err := os.Executable()
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
t.Helper()
57+
58+
t.Logf("running getgo %v", args)
7559
var stdout, stderr bytes.Buffer
76-
t.Logf("running %s %v", testbin, args)
77-
cmd := exec.Command("./"+testbin+exeSuffix, args...)
60+
cmd := exec.Command(exe, args...)
7861
cmd.Stdout = &stdout
7962
cmd.Stderr = &stderr
80-
cmd.Env = os.Environ()
63+
cmd.Env = append(os.Environ(), "GO_GETGO_TEST_IS_GETGO=1")
8164
status := cmd.Run()
8265
if stdout.Len() > 0 {
8366
t.Log("standard output:")

0 commit comments

Comments
 (0)