mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-06 03:53:42 +00:00
os: implement and smoketest os.Setenv
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user