Skip to content

Commit

Permalink
cmd/getgo: exec main from TestMain instead of running 'go build' in t…
Browse files Browse the repository at this point in the history
…ests

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 <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
Bryan C. Mills committed Mar 4, 2022
1 parent e562276 commit e155b03
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
1 change: 0 additions & 1 deletion cmd/getgo/.gitignore
@@ -1,3 +1,2 @@
build
testgetgo
getgo
45 changes: 14 additions & 31 deletions cmd/getgo/main_test.go
Expand Up @@ -13,50 +13,27 @@ import (
"io/ioutil"
"os"
"os/exec"
"runtime"
"testing"
)

const (
testbin = "testgetgo"
)

var (
exeSuffix string // ".exe" on Windows
)

func init() {
if runtime.GOOS == "windows" {
exeSuffix = ".exe"
func TestMain(m *testing.M) {
if os.Getenv("GO_GETGO_TEST_IS_GETGO") != "" {
main()
os.Exit(0)
}
}

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

args := []string{"build", "-tags", testbin, "-o", testbin + exeSuffix}
out, err := exec.Command("go", args...).CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "building %s failed: %v\n%s", testbin, err, out)
os.Exit(2)
}

// Don't let these environment variables confuse the test.
os.Unsetenv("GOBIN")
os.Unsetenv("GOPATH")
os.Unsetenv("GIT_ALLOW_PROTOCOL")
os.Unsetenv("PATH")

r := m.Run()

os.Remove(testbin + exeSuffix)

os.Exit(r)
os.Exit(m.Run())
}

func createTmpHome(t *testing.T) string {
Expand All @@ -72,12 +49,18 @@ func createTmpHome(t *testing.T) string {
// doRun runs the test getgo command, recording stdout and stderr and
// returning exit status.
func doRun(t *testing.T, args ...string) error {
exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
t.Helper()

t.Logf("running getgo %v", args)
var stdout, stderr bytes.Buffer
t.Logf("running %s %v", testbin, args)
cmd := exec.Command("./"+testbin+exeSuffix, args...)
cmd := exec.Command(exe, args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Env = os.Environ()
cmd.Env = append(os.Environ(), "GO_GETGO_TEST_IS_GETGO=1")
status := cmd.Run()
if stdout.Len() > 0 {
t.Log("standard output:")
Expand Down

0 comments on commit e155b03

Please sign in to comment.