Skip to content

Commit

Permalink
cmd/go: run test binaries in original environment
Browse files Browse the repository at this point in the history
Fixes #12096.
Followup to CL 12483, which fixed #11709 and #11449.

Change-Id: I9031ea36cc60685f4d6f65c39f770c89b3e3395a
Reviewed-on: https://go-review.googlesource.com/13449
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
rsc committed Aug 11, 2015
1 parent 28fb0d8 commit 58035ec
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/cmd/go/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ func (b *builder) runOut(dir string, desc string, env []string, cmdargs ...inter
cmd.Stdout = &buf
cmd.Stderr = &buf
cmd.Dir = dir
cmd.Env = mergeEnvLists(env, envForDir(cmd.Dir))
cmd.Env = mergeEnvLists(env, envForDir(cmd.Dir, os.Environ()))
err := cmd.Run()

// cmd.Run will fail on Unix if some other process has the binary
Expand Down
15 changes: 15 additions & 0 deletions src/cmd/go/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,21 @@ func TestIssue11709(t *testing.T) {
tg.run("run", tg.path("run.go"))
}

func TestIssue12096(t *testing.T) {
tg := testgo(t)
defer tg.cleanup()
tg.tempFile("test_test.go", `
package main
import ("os"; "testing")
func TestEnv(t *testing.T) {
if os.Getenv("TERM") != "" {
t.Fatal("TERM is set")
}
}`)
tg.unsetenv("TERM")
tg.run("test", tg.path("test_test.go"))
}

func TestGoBuildOutput(t *testing.T) {
tg := testgo(t)
defer tg.cleanup()
Expand Down
5 changes: 2 additions & 3 deletions src/cmd/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,10 @@ func runOut(dir string, cmdargs ...interface{}) []byte {
// The environment is the current process's environment
// but with an updated $PWD, so that an os.Getwd in the
// child will be faster.
func envForDir(dir string) []string {
env := os.Environ()
func envForDir(dir string, base []string) []string {
// Internally we only use rooted paths, so dir is rooted.
// Even if dir is not rooted, no harm done.
return mergeEnvLists([]string{"PWD=" + dir}, env)
return mergeEnvLists([]string{"PWD=" + dir}, base)
}

// mergeEnvLists merges the two environment lists such that
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ func (b *builder) runTest(a *action) error {

cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = a.p.Dir
cmd.Env = envForDir(cmd.Dir)
cmd.Env = envForDir(cmd.Dir, origEnv)
var buf bytes.Buffer
if testStreamOutput {
cmd.Stdout = os.Stdout
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool)

cmd := exec.Command(v.cmd, args...)
cmd.Dir = dir
cmd.Env = envForDir(cmd.Dir)
cmd.Env = envForDir(cmd.Dir, os.Environ())
if buildX {
fmt.Printf("cd %s\n", dir)
fmt.Printf("%s %s\n", v.cmd, strings.Join(args, " "))
Expand Down

0 comments on commit 58035ec

Please sign in to comment.