os: implement and smoketest os.Unsetenv

Этот коммит содержится в:
Dan Kegel 2021-12-11 09:55:21 -08:00 коммит произвёл Ron Evans
родитель cff4493ca0
коммит e4f2b9c003
5 изменённых файлов: 64 добавлений и 2 удалений

Просмотреть файл

@ -17,8 +17,12 @@ func Setenv(key, value string) error {
return nil
}
func Unsetenv(_ string) error {
return ErrNotImplemented
func Unsetenv(key string) error {
err := syscall.Unsetenv(key)
if err != nil {
return NewSyscallError("unsetenv", err)
}
return nil
}
func LookupEnv(key string) (string, bool) {

Просмотреть файл

@ -7,6 +7,7 @@ package os_test
import (
. "os"
"reflect"
"strings"
"testing"
)
@ -20,6 +21,31 @@ func TestConsistentEnviron(t *testing.T) {
}
}
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")
}
}
func TestLookupEnv(t *testing.T) {
const smallpox = "SMALLPOX" // No one has smallpox.
value, ok := LookupEnv(smallpox) // Should not exist.

Просмотреть файл

@ -13,6 +13,16 @@ func syscall_setenv_c(key string, val string) {
return
}
// Update the C environment if cgo is loaded.
// Called from syscall.Unsetenv.
//go:linkname syscall_unsetenv_c syscall.unsetenv_c
func syscall_unsetenv_c(key string) {
keydata := cstring(key)
// ignore any errors
libc_unsetenv(&keydata[0])
return
}
// cstring converts a Go string to a C string.
// borrowed from syscall
func cstring(s string) []byte {
@ -25,3 +35,7 @@ func cstring(s string) []byte {
// int setenv(const char *name, const char *val, int replace);
//export setenv
func libc_setenv(name *byte, val *byte, replace int32) int32
// int unsetenv(const char *name);
//export unsetenv
func libc_unsetenv(name *byte) int32

Просмотреть файл

@ -142,6 +142,15 @@ func Setenv(key, val string) (err error) {
return
}
func Unsetenv(key string) (err error) {
keydata := cstring(key)
errCode := libc_unsetenv(&keydata[0])
if errCode != 0 {
err = getErrno()
}
return
}
func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
addr := libc_mmap(nil, uintptr(length), int32(prot), int32(flags), int32(fd), uintptr(offset))
if addr == unsafe.Pointer(^uintptr(0)) {
@ -206,6 +215,10 @@ func libc_getenv(name *byte) *byte
//export setenv
func libc_setenv(name *byte, val *byte, replace int32) int32
// int unsetenv(const char *name);
//export unsetenv
func libc_unsetenv(name *byte) int32
// ssize_t read(int fd, void *buf, size_t count);
//export read
func libc_read(fd int32, buf *byte, count uint) int

Просмотреть файл

@ -72,6 +72,11 @@ func Setenv(key, val string) (err error) {
return ENOSYS
}
func Unsetenv(key string) (err error) {
// stub for now
return ENOSYS
}
func Environ() []string {
env := runtime_envs()
envCopy := make([]string, len(env))