@@ -32,25 +32,6 @@ func TestMain(m *testing.M) {
32
32
os .Exit (status )
33
33
}
34
34
35
- func testEnv (cmd * exec.Cmd ) * exec.Cmd {
36
- if cmd .Env != nil {
37
- panic ("environment already set" )
38
- }
39
- for _ , env := range os .Environ () {
40
- // Exclude GODEBUG from the environment to prevent its output
41
- // from breaking tests that are trying to parse other command output.
42
- if strings .HasPrefix (env , "GODEBUG=" ) {
43
- continue
44
- }
45
- // Exclude GOTRACEBACK for the same reason.
46
- if strings .HasPrefix (env , "GOTRACEBACK=" ) {
47
- continue
48
- }
49
- cmd .Env = append (cmd .Env , env )
50
- }
51
- return cmd
52
- }
53
-
54
35
var testprog struct {
55
36
sync.Mutex
56
37
dir string
@@ -70,7 +51,7 @@ func runTestProg(t *testing.T, binary, name string) string {
70
51
t .Fatal (err )
71
52
}
72
53
73
- cmd := testEnv (exec .Command (exe , name ))
54
+ cmd := testenv . CleanCmdEnv (exec .Command (exe , name ))
74
55
var b bytes.Buffer
75
56
cmd .Stdout = & b
76
57
cmd .Stderr = & b
@@ -139,7 +120,7 @@ func buildTestProg(t *testing.T, binary string, flags ...string) (string, error)
139
120
exe := filepath .Join (testprog .dir , name + ".exe" )
140
121
cmd := exec .Command (testenv .GoToolPath (t ), append ([]string {"build" , "-o" , exe }, flags ... )... )
141
122
cmd .Dir = "testdata/" + binary
142
- out , err := testEnv (cmd ).CombinedOutput ()
123
+ out , err := testenv . CleanCmdEnv (cmd ).CombinedOutput ()
143
124
if err != nil {
144
125
target .err = fmt .Errorf ("building %s %v: %v\n %s" , binary , flags , err , out )
145
126
testprog .target [name ] = target
@@ -158,14 +139,14 @@ var (
158
139
func checkStaleRuntime (t * testing.T ) {
159
140
staleRuntimeOnce .Do (func () {
160
141
// 'go run' uses the installed copy of runtime.a, which may be out of date.
161
- out , err := testEnv (exec .Command (testenv .GoToolPath (t ), "list" , "-f" , "{{.Stale}}" , "runtime" )).CombinedOutput ()
142
+ out , err := testenv . CleanCmdEnv (exec .Command (testenv .GoToolPath (t ), "list" , "-f" , "{{.Stale}}" , "runtime" )).CombinedOutput ()
162
143
if err != nil {
163
144
staleRuntimeErr = fmt .Errorf ("failed to execute 'go list': %v\n %v" , err , string (out ))
164
145
return
165
146
}
166
147
if string (out ) != "false\n " {
167
148
t .Logf ("go list -f {{.Stale}} runtime:\n %s" , out )
168
- out , err := testEnv (exec .Command (testenv .GoToolPath (t ), "list" , "-f" , "{{.StaleReason}}" , "runtime" )).CombinedOutput ()
149
+ out , err := testenv . CleanCmdEnv (exec .Command (testenv .GoToolPath (t ), "list" , "-f" , "{{.StaleReason}}" , "runtime" )).CombinedOutput ()
169
150
if err != nil {
170
151
t .Logf ("go list -f {{.StaleReason}} failed: %v" , err )
171
152
}
@@ -468,15 +449,15 @@ func TestMemPprof(t *testing.T) {
468
449
t .Fatal (err )
469
450
}
470
451
471
- got , err := testEnv (exec .Command (exe , "MemProf" )).CombinedOutput ()
452
+ got , err := testenv . CleanCmdEnv (exec .Command (exe , "MemProf" )).CombinedOutput ()
472
453
if err != nil {
473
454
t .Fatal (err )
474
455
}
475
456
fn := strings .TrimSpace (string (got ))
476
457
defer os .Remove (fn )
477
458
478
459
for try := 0 ; try < 2 ; try ++ {
479
- cmd := testEnv (exec .Command (testenv .GoToolPath (t ), "tool" , "pprof" , "-alloc_space" , "-top" ))
460
+ cmd := testenv . CleanCmdEnv (exec .Command (testenv .GoToolPath (t ), "tool" , "pprof" , "-alloc_space" , "-top" ))
480
461
// Check that pprof works both with and without explicit executable on command line.
481
462
if try == 0 {
482
463
cmd .Args = append (cmd .Args , exe , fn )
@@ -586,7 +567,7 @@ func TestPanicRace(t *testing.T) {
586
567
const tries = 10
587
568
retry:
588
569
for i := 0 ; i < tries ; i ++ {
589
- got , err := testEnv (exec .Command (exe , "PanicRace" )).CombinedOutput ()
570
+ got , err := testenv . CleanCmdEnv (exec .Command (exe , "PanicRace" )).CombinedOutput ()
590
571
if err == nil {
591
572
t .Logf ("try %d: program exited successfully, should have failed" , i + 1 )
592
573
continue
0 commit comments