os, syscall: add Unsetenv
Also address a TODO, making Clearenv pass through to cgo.
Based largely on Minux's earlier https://golang.org/cl/82040044
Fixes #6423
LGTM=iant, alex.brainman, r, rsc
R=rsc, iant, r, alex.brainman
CC=golang-codereviews
https://golang.org/cl/148370043
diff --git a/src/os/env_test.go b/src/os/env_test.go
index 991fa4d..e618067 100644
--- a/src/os/env_test.go
+++ b/src/os/env_test.go
@@ -7,6 +7,7 @@
import (
. "os"
"reflect"
+ "strings"
"testing"
)
@@ -68,3 +69,28 @@
}
}
}
+
+func TestUnsetenv(t *testing.T) {
+ const testKey = "GO_TEST_UNSETENV"
+ set := func() bool {
+ prefix := testKey + "="
+ for _, key := range Environ() {
+ if strings.HasPrefix(key, prefix) {
+ return true
+ }
+ }
+ return false
+ }
+ if err := Setenv(testKey, "1"); err != nil {
+ t.Fatalf("Setenv: %v", err)
+ }
+ if !set() {
+ t.Error("Setenv didn't set TestUnsetenv")
+ }
+ if err := Unsetenv(testKey); err != nil {
+ t.Fatalf("Unsetenv: %v", err)
+ }
+ if set() {
+ t.Fatal("Unsetenv didn't clear TestUnsetenv")
+ }
+}