Skip to content

Commit fdf702a

Browse files
authored
testscript: minor updates to Setenv logic (#98)
- include the key in the panic message - cosmetic improvements to tests
1 parent 8801f49 commit fdf702a

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

testscript/testscript.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (e *Env) Getenv(key string) string {
8585
// panics if key is invalid.
8686
func (e *Env) Setenv(key, value string) {
8787
if key == "" || strings.IndexByte(key, '=') != -1 {
88-
panic("Setenv: invalid argument")
88+
panic(fmt.Errorf("invalid environment variable key %q", key))
8989
}
9090
e.Vars = append(e.Vars, key+"="+value)
9191
}

testscript/testscript_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,32 +99,31 @@ func TestEnv(t *testing.T) {
9999

100100
e.Setenv("HOME", "/home/user")
101101
if got, want := e.Getenv("HOME"), "/home/user"; got != want {
102-
t.Errorf("e.Getenv(\"HOME\") == %q, want %q", got, want)
102+
t.Errorf(`e.Getenv("HOME") == %q, want %q`, got, want)
103103
}
104104

105105
if got, want := e.Getenv("PATH"), "/usr/bin:/usr/local/bin"; got != want {
106-
t.Errorf("e.Getenv(\"PATH\") == %q, want %q", got, want)
106+
t.Errorf(`e.Getenv("PATH") == %q, want %q`, got, want)
107107
}
108108

109109
if got, want := e.Getenv("INVALID"), ""; got != want {
110-
t.Errorf("e.Getenv(\"INVALID\") == %q, want %q", got, want)
110+
t.Errorf(`e.Getenv("INVALID") == %q, want %q`, got, want)
111111
}
112112

113113
for _, key := range []string{
114114
"",
115115
"=",
116116
"key=invalid",
117117
} {
118-
value := ""
119118
var panicValue interface{}
120119
func() {
121120
defer func() {
122121
panicValue = recover()
123122
}()
124-
e.Setenv(key, value)
123+
e.Setenv(key, "")
125124
}()
126125
if panicValue == nil {
127-
t.Errorf("e.Setenv(%q, %q) did not panic, want panic", key, value)
126+
t.Errorf("e.Setenv(%q) did not panic, want panic", key)
128127
}
129128
}
130129
}

0 commit comments

Comments
 (0)