os: implement and smoketest os.Setenv

This commit is contained in:
Dan Kegel
2021-12-05 13:24:00 -08:00
committed by Ron Evans
parent 93ac7cec0d
commit cff4493ca0
5 changed files with 96 additions and 0 deletions
+12
View File
@@ -9,6 +9,18 @@ func Getenv(key string) string {
return v
}
func Setenv(key, value string) error {
err := syscall.Setenv(key, value)
if err != nil {
return NewSyscallError("setenv", err)
}
return nil
}
func Unsetenv(_ string) error {
return ErrNotImplemented
}
func LookupEnv(key string) (string, bool) {
return syscall.Getenv(key)
}
+38
View File
@@ -0,0 +1,38 @@
// Copyright 2010 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 os_test
import (
. "os"
"reflect"
"testing"
)
func TestConsistentEnviron(t *testing.T) {
e0 := Environ()
for i := 0; i < 10; i++ {
e1 := Environ()
if !reflect.DeepEqual(e0, e1) {
t.Fatalf("environment changed")
}
}
}
func TestLookupEnv(t *testing.T) {
const smallpox = "SMALLPOX" // No one has smallpox.
value, ok := LookupEnv(smallpox) // Should not exist.
if ok || value != "" {
t.Fatalf("%s=%q", smallpox, value)
}
defer Unsetenv(smallpox)
err := Setenv(smallpox, "virus")
if err != nil {
t.Fatalf("failed to release smallpox virus")
}
_, ok = LookupEnv(smallpox)
if !ok {
t.Errorf("smallpox release failed; world remains safe but LookupEnv is broken")
}
}