Skip to content

Commit 8801f49

Browse files
authored
testscript: add env convenience functions (#96)
Manipulating Env.Vars is not completely trivial: you need to handle last-entry-wins, invalid entries, and key normalization. This commit adds Getenv and Setenv methods to Env.
1 parent b01f880 commit 8801f49

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

testscript/testscript.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ func (e *Env) Defer(f func()) {
6969
e.ts.Defer(f)
7070
}
7171

72+
// Getenv retrieves the value of the environment variable named by the key. It
73+
// returns the value, which will be empty if the variable is not present.
74+
func (e *Env) Getenv(key string) string {
75+
key = envvarname(key)
76+
for i := len(e.Vars) - 1; i >= 0; i-- {
77+
if pair := strings.SplitN(e.Vars[i], "=", 2); len(pair) == 2 && envvarname(pair[0]) == key {
78+
return pair[1]
79+
}
80+
}
81+
return ""
82+
}
83+
84+
// Setenv sets the value of the environment variable named by the key. It
85+
// panics if key is invalid.
86+
func (e *Env) Setenv(key, value string) {
87+
if key == "" || strings.IndexByte(key, '=') != -1 {
88+
panic("Setenv: invalid argument")
89+
}
90+
e.Vars = append(e.Vars, key+"="+value)
91+
}
92+
7293
// T returns the t argument passed to the current test by the T.Run method.
7394
// Note that if the tests were started by calling Run,
7495
// the returned value will implement testing.TB.

testscript/testscript_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,52 @@ func TestCRLFInput(t *testing.T) {
8383
})
8484
}
8585

86+
func TestEnv(t *testing.T) {
87+
e := &Env{
88+
Vars: []string{
89+
"HOME=/no-home",
90+
"PATH=/usr/bin",
91+
"PATH=/usr/bin:/usr/local/bin",
92+
"INVALID",
93+
},
94+
}
95+
96+
if got, want := e.Getenv("HOME"), "/no-home"; got != want {
97+
t.Errorf("e.Getenv(\"HOME\") == %q, want %q", got, want)
98+
}
99+
100+
e.Setenv("HOME", "/home/user")
101+
if got, want := e.Getenv("HOME"), "/home/user"; got != want {
102+
t.Errorf("e.Getenv(\"HOME\") == %q, want %q", got, want)
103+
}
104+
105+
if got, want := e.Getenv("PATH"), "/usr/bin:/usr/local/bin"; got != want {
106+
t.Errorf("e.Getenv(\"PATH\") == %q, want %q", got, want)
107+
}
108+
109+
if got, want := e.Getenv("INVALID"), ""; got != want {
110+
t.Errorf("e.Getenv(\"INVALID\") == %q, want %q", got, want)
111+
}
112+
113+
for _, key := range []string{
114+
"",
115+
"=",
116+
"key=invalid",
117+
} {
118+
value := ""
119+
var panicValue interface{}
120+
func() {
121+
defer func() {
122+
panicValue = recover()
123+
}()
124+
e.Setenv(key, value)
125+
}()
126+
if panicValue == nil {
127+
t.Errorf("e.Setenv(%q, %q) did not panic, want panic", key, value)
128+
}
129+
}
130+
}
131+
86132
func TestScripts(t *testing.T) {
87133
// TODO set temp directory.
88134
testDeferCount := 0

0 commit comments

Comments
 (0)