1
0
mirror of https://github.com/golang/go synced 2024-11-19 10:14:44 -07:00
go/src/runtime/env_test.go
Brad Fitzpatrick 03410f6758 runtime: fix TestFixedGOROOT to properly restore the GOROOT env var after test
Otherwise subsequent tests won't see any modified GOROOT.

With this CL I can move my GOROOT, set GOROOT to the new location, and
the runtime tests pass. Previously the crash_tests would instead look
for the GOROOT baked into the binary, instead of the env var:

--- FAIL: TestGcSys (0.01s)
        crash_test.go:92: building source: exit status 2
                go: cannot find GOROOT directory: /home/bradfitz/go
--- FAIL: TestGCFairness (0.01s)
        crash_test.go:92: building source: exit status 2
                go: cannot find GOROOT directory: /home/bradfitz/go
--- FAIL: TestGdbPython (0.07s)
        runtime-gdb_test.go:64: building source exit status 2
                go: cannot find GOROOT directory: /home/bradfitz/go
--- FAIL: TestLargeStringConcat (0.01s)
        crash_test.go:92: building source: exit status 2
                go: cannot find GOROOT directory: /home/bradfitz/go

Update #10029

Change-Id: If91be0f04d3acdcf39a9e773a4e7905a446bc477
Reviewed-on: https://go-review.googlesource.com/10685
Reviewed-by: Andrew Gerrand <adg@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2015-06-03 23:33:48 +00:00

48 lines
1.3 KiB
Go

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package runtime_test
import (
"runtime"
"syscall"
"testing"
)
func TestFixedGOROOT(t *testing.T) {
if runtime.GOOS == "plan9" {
t.Skipf("skipping plan9, it is inconsistent by allowing GOROOT to be updated by Setenv")
}
// Restore both the real GOROOT environment variable, and runtime's copies:
if orig, ok := syscall.Getenv("GOROOT"); ok {
defer syscall.Setenv("GOROOT", orig)
} else {
defer syscall.Unsetenv("GOROOT")
}
envs := runtime.Envs()
oldenvs := append([]string{}, envs...)
defer runtime.SetEnvs(oldenvs)
// attempt to reuse existing envs backing array.
want := runtime.GOROOT()
runtime.SetEnvs(append(envs[:0], "GOROOT="+want))
if got := runtime.GOROOT(); got != want {
t.Errorf(`initial runtime.GOROOT()=%q, want %q`, got, want)
}
if err := syscall.Setenv("GOROOT", "/os"); err != nil {
t.Fatal(err)
}
if got := runtime.GOROOT(); got != want {
t.Errorf(`after setenv runtime.GOROOT()=%q, want %q`, got, want)
}
if err := syscall.Unsetenv("GOROOT"); err != nil {
t.Fatal(err)
}
if got := runtime.GOROOT(); got != want {
t.Errorf(`after unsetenv runtime.GOROOT()=%q, want %q`, got, want)
}
}