Home | History | Annotate | Download | only in runtime
      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 	if runtime.GOOS == "plan9" {
     15 		t.Skipf("skipping plan9, it is inconsistent by allowing GOROOT to be updated by Setenv")
     16 	}
     17 
     18 	// Restore both the real GOROOT environment variable, and runtime's copies:
     19 	if orig, ok := syscall.Getenv("GOROOT"); ok {
     20 		defer syscall.Setenv("GOROOT", orig)
     21 	} else {
     22 		defer syscall.Unsetenv("GOROOT")
     23 	}
     24 	envs := runtime.Envs()
     25 	oldenvs := append([]string{}, envs...)
     26 	defer runtime.SetEnvs(oldenvs)
     27 
     28 	// attempt to reuse existing envs backing array.
     29 	want := runtime.GOROOT()
     30 	runtime.SetEnvs(append(envs[:0], "GOROOT="+want))
     31 
     32 	if got := runtime.GOROOT(); got != want {
     33 		t.Errorf(`initial runtime.GOROOT()=%q, want %q`, got, want)
     34 	}
     35 	if err := syscall.Setenv("GOROOT", "/os"); err != nil {
     36 		t.Fatal(err)
     37 	}
     38 	if got := runtime.GOROOT(); got != want {
     39 		t.Errorf(`after setenv runtime.GOROOT()=%q, want %q`, got, want)
     40 	}
     41 	if err := syscall.Unsetenv("GOROOT"); err != nil {
     42 		t.Fatal(err)
     43 	}
     44 	if got := runtime.GOROOT(); got != want {
     45 		t.Errorf(`after unsetenv runtime.GOROOT()=%q, want %q`, got, want)
     46 	}
     47 }
     48