os: implement and smoketest os.Unsetenv
Этот коммит содержится в:
родитель
cff4493ca0
коммит
e4f2b9c003
5 изменённых файлов: 64 добавлений и 2 удалений
|
@ -17,8 +17,12 @@ func Setenv(key, value string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Unsetenv(_ string) error {
|
func Unsetenv(key string) error {
|
||||||
return ErrNotImplemented
|
err := syscall.Unsetenv(key)
|
||||||
|
if err != nil {
|
||||||
|
return NewSyscallError("unsetenv", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LookupEnv(key string) (string, bool) {
|
func LookupEnv(key string) (string, bool) {
|
||||||
|
|
|
@ -7,6 +7,7 @@ package os_test
|
||||||
import (
|
import (
|
||||||
. "os"
|
. "os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"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) {
|
func TestLookupEnv(t *testing.T) {
|
||||||
const smallpox = "SMALLPOX" // No one has smallpox.
|
const smallpox = "SMALLPOX" // No one has smallpox.
|
||||||
value, ok := LookupEnv(smallpox) // Should not exist.
|
value, ok := LookupEnv(smallpox) // Should not exist.
|
||||||
|
|
|
@ -13,6 +13,16 @@ func syscall_setenv_c(key string, val string) {
|
||||||
return
|
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.
|
// cstring converts a Go string to a C string.
|
||||||
// borrowed from syscall
|
// borrowed from syscall
|
||||||
func cstring(s string) []byte {
|
func cstring(s string) []byte {
|
||||||
|
@ -25,3 +35,7 @@ func cstring(s string) []byte {
|
||||||
// int setenv(const char *name, const char *val, int replace);
|
// int setenv(const char *name, const char *val, int replace);
|
||||||
//export setenv
|
//export setenv
|
||||||
func libc_setenv(name *byte, val *byte, replace int32) int32
|
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
|
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) {
|
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))
|
addr := libc_mmap(nil, uintptr(length), int32(prot), int32(flags), int32(fd), uintptr(offset))
|
||||||
if addr == unsafe.Pointer(^uintptr(0)) {
|
if addr == unsafe.Pointer(^uintptr(0)) {
|
||||||
|
@ -206,6 +215,10 @@ func libc_getenv(name *byte) *byte
|
||||||
//export setenv
|
//export setenv
|
||||||
func libc_setenv(name *byte, val *byte, replace int32) int32
|
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);
|
// ssize_t read(int fd, void *buf, size_t count);
|
||||||
//export read
|
//export read
|
||||||
func libc_read(fd int32, buf *byte, count uint) int
|
func libc_read(fd int32, buf *byte, count uint) int
|
||||||
|
|
|
@ -72,6 +72,11 @@ func Setenv(key, val string) (err error) {
|
||||||
return ENOSYS
|
return ENOSYS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Unsetenv(key string) (err error) {
|
||||||
|
// stub for now
|
||||||
|
return ENOSYS
|
||||||
|
}
|
||||||
|
|
||||||
func Environ() []string {
|
func Environ() []string {
|
||||||
env := runtime_envs()
|
env := runtime_envs()
|
||||||
envCopy := make([]string, len(env))
|
envCopy := make([]string, len(env))
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче