| 1 |
// Copyright 2015 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package runtime_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"runtime" |
| 9 |
"syscall" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestFixedGOROOT(t *testing.T) { |
| 14 |
// Restore both the real GOROOT environment variable, and runtime's copies: |
| 15 |
if orig, ok := syscall.Getenv("GOROOT"); ok { |
| 16 |
defer syscall.Setenv("GOROOT", orig) |
| 17 |
} else { |
| 18 |
defer syscall.Unsetenv("GOROOT") |
| 19 |
} |
| 20 |
envs := runtime.Envs() |
| 21 |
oldenvs := append([]string{}, envs...) |
| 22 |
defer runtime.SetEnvs(oldenvs) |
| 23 |
|
| 24 |
// attempt to reuse existing envs backing array. |
| 25 |
want := runtime.GOROOT() |
| 26 |
runtime.SetEnvs(append(envs[:0], "GOROOT="+want)) |
| 27 |
|
| 28 |
if got := runtime.GOROOT(); got != want { |
| 29 |
t.Errorf(`initial runtime.GOROOT()=%q, want %q`, got, want) |
| 30 |
} |
| 31 |
if err := syscall.Setenv("GOROOT", "/os"); err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
if got := runtime.GOROOT(); got != want { |
| 35 |
t.Errorf(`after setenv runtime.GOROOT()=%q, want %q`, got, want) |
| 36 |
} |
| 37 |
if err := syscall.Unsetenv("GOROOT"); err != nil { |
| 38 |
t.Fatal(err) |
| 39 |
} |
| 40 |
if got := runtime.GOROOT(); got != want { |
| 41 |
t.Errorf(`after unsetenv runtime.GOROOT()=%q, want %q`, got, want) |
| 42 |
} |
| 43 |
} |