Skip to content

Commit 58035ec

Browse files
committed
cmd/go: run test binaries in original environment
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 <[email protected]>
1 parent 28fb0d8 commit 58035ec

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

src/cmd/go/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,7 @@ func (b *builder) runOut(dir string, desc string, env []string, cmdargs ...inter
18801880
cmd.Stdout = &buf
18811881
cmd.Stderr = &buf
18821882
cmd.Dir = dir
1883-
cmd.Env = mergeEnvLists(env, envForDir(cmd.Dir))
1883+
cmd.Env = mergeEnvLists(env, envForDir(cmd.Dir, os.Environ()))
18841884
err := cmd.Run()
18851885

18861886
// cmd.Run will fail on Unix if some other process has the binary

src/cmd/go/go_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,21 @@ func TestIssue11709(t *testing.T) {
22942294
tg.run("run", tg.path("run.go"))
22952295
}
22962296

2297+
func TestIssue12096(t *testing.T) {
2298+
tg := testgo(t)
2299+
defer tg.cleanup()
2300+
tg.tempFile("test_test.go", `
2301+
package main
2302+
import ("os"; "testing")
2303+
func TestEnv(t *testing.T) {
2304+
if os.Getenv("TERM") != "" {
2305+
t.Fatal("TERM is set")
2306+
}
2307+
}`)
2308+
tg.unsetenv("TERM")
2309+
tg.run("test", tg.path("test_test.go"))
2310+
}
2311+
22972312
func TestGoBuildOutput(t *testing.T) {
22982313
tg := testgo(t)
22992314
defer tg.cleanup()

src/cmd/go/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,10 @@ func runOut(dir string, cmdargs ...interface{}) []byte {
446446
// The environment is the current process's environment
447447
// but with an updated $PWD, so that an os.Getwd in the
448448
// child will be faster.
449-
func envForDir(dir string) []string {
450-
env := os.Environ()
449+
func envForDir(dir string, base []string) []string {
451450
// Internally we only use rooted paths, so dir is rooted.
452451
// Even if dir is not rooted, no harm done.
453-
return mergeEnvLists([]string{"PWD=" + dir}, env)
452+
return mergeEnvLists([]string{"PWD=" + dir}, base)
454453
}
455454

456455
// mergeEnvLists merges the two environment lists such that

src/cmd/go/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ func (b *builder) runTest(a *action) error {
10271027

10281028
cmd := exec.Command(args[0], args[1:]...)
10291029
cmd.Dir = a.p.Dir
1030-
cmd.Env = envForDir(cmd.Dir)
1030+
cmd.Env = envForDir(cmd.Dir, origEnv)
10311031
var buf bytes.Buffer
10321032
if testStreamOutput {
10331033
cmd.Stdout = os.Stdout

src/cmd/go/vcs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool)
344344

345345
cmd := exec.Command(v.cmd, args...)
346346
cmd.Dir = dir
347-
cmd.Env = envForDir(cmd.Dir)
347+
cmd.Env = envForDir(cmd.Dir, os.Environ())
348348
if buildX {
349349
fmt.Printf("cd %s\n", dir)
350350
fmt.Printf("%s %s\n", v.cmd, strings.Join(args, " "))

0 commit comments

Comments
 (0)