1
0
mirror of https://github.com/golang/go synced 2024-11-06 13:36:12 -07:00
go/src/runtime/env_test.go
Richard Miller af09ff1981 runtime, syscall: use local cache for Setenv/Getenv in Plan 9
In os.Getenv and os.Setenv, instead of directly reading and writing the
Plan 9 environment device (which may be shared with other processes),
use a local copy of environment variables cached at the start of
execution. This gives the same semantics for Getenv and Setenv as on
other operating systems which don't share the environment, making it
more likely that Go programs (for example the build tests) will be
portable to Plan 9.

This doesn't preclude writing non-portable Plan 9 Go programs which make
use of the shared environment semantics (for example to have a command
which exports variable definitions to the parent shell). To do this, use
  ioutil.ReadFile("/env/"+key) and
  ioutil.WriteFile("/env/"+key, value, 0666)
in place of os.Getenv(key) and os.Setenv(key, value) respectively.

Note that CL 5599054 previously added env cacheing, citing efficiency
as the reason. However it made the cache write-through, with Setenv
changing the shared environment as well as the cache (so not consistent
with Posix semantics), and Clearenv breaking the sharing of the
environment between the calling thread and other threads (leading to
unpredictable behaviour). Because of these inconsistencies (#8849),
CL 158970045 removed the cacheing again.

This CL restores cacheing but without write-through. The local cache is
initialised at start of execution, manipulated by the standard functions
in syscall/env_unix.go to ensure the same semantics, and exported only
when exec'ing a new program.

Fixes #34971
Fixes #25234
Fixes #19388
Updates #38772

Change-Id: I2dd15516d27414afaf99ea382f0e00be37a570c3
Reviewed-on: https://go-review.googlesource.com/c/go/+/236520
Run-TryBot: David du Colombier <0intro@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Fazlul Shahriar <fshahriar@gmail.com>
Reviewed-by: David du Colombier <0intro@gmail.com>
2020-06-19 11:28:19 +00:00

44 lines
1.2 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) {
// 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)
}
}