terraform: for tempEnv, if the var wans't set before, unset

This was causing flaky behavior in our tests because `TF_VAR_x=""` is
actually a valid env var. For tests, we need to actually unset env vars
that haven't been set before.
This commit is contained in:
Mitchell Hashimoto 2016-10-31 11:00:19 -07:00
parent 2e639cb506
commit c21610f533
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
1 changed files with 8 additions and 2 deletions

View File

@ -59,9 +59,15 @@ func tempDir(t *testing.T) string {
// a function to defer to reset the old value.
// the old value that should be set via a defer.
func tempEnv(t *testing.T, k string, v string) func() {
old := os.Getenv(k)
old, oldOk := os.LookupEnv(k)
os.Setenv(k, v)
return func() { os.Setenv(k, old) }
return func() {
if !oldOk {
os.Unsetenv(k)
} else {
os.Setenv(k, old)
}
}
}
func testConfig(t *testing.T, name string) *config.Config {