1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // testGetenv gives us a controlled set of variables for testing Expand.
15 func testGetenv(s
string) string {
37 var expandTests
= []struct {
41 {"$*", "all the args"},
43 {"${*}", "all the args"},
45 {"${1}", "ARGUMENT1"},
46 {"now is the time", "now is the time"},
47 {"$HOME", "/usr/gopher"},
48 {"$home_1", "/usr/foo"},
49 {"${HOME}", "/usr/gopher"},
50 {"${H}OME", "(Value of H)OME"},
51 {"A$$$#$1$H$home_1*B", "APIDNARGSARGUMENT1(Value of H)/usr/foo*B"},
54 func TestExpand(t
*testing
.T
) {
55 for _
, test
:= range expandTests
{
56 result
:= Expand(test
.in
, testGetenv
)
57 if result
!= test
.out
{
58 t
.Errorf("Expand(%q)=%q; expected %q", test
.in
, result
, test
.out
)
63 func TestConsistentEnviron(t
*testing
.T
) {
65 for i
:= 0; i
< 10; i
++ {
67 if !reflect
.DeepEqual(e0
, e1
) {
68 t
.Fatalf("environment changed")
73 func TestUnsetenv(t
*testing
.T
) {
74 const testKey
= "GO_TEST_UNSETENV"
76 prefix
:= testKey
+ "="
77 for _
, key
:= range Environ() {
78 if strings
.HasPrefix(key
, prefix
) {
84 if err
:= Setenv(testKey
, "1"); err
!= nil {
85 t
.Fatalf("Setenv: %v", err
)
88 t
.Error("Setenv didn't set TestUnsetenv")
90 if err
:= Unsetenv(testKey
); err
!= nil {
91 t
.Fatalf("Unsetenv: %v", err
)
94 t
.Fatal("Unsetenv didn't clear TestUnsetenv")