Skip to content

Commit

Permalink
Use t.Setenv for setting/unsetting test-specific env vars (#20946)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhendrixMSFT authored Jun 5, 2023
1 parent a182cd6 commit 5917509
Showing 1 changed file with 2 additions and 27 deletions.
29 changes: 2 additions & 27 deletions sdk/azidentity/azidentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,36 +161,11 @@ func validateX5C(t *testing.T, certs []*x509.Certificate) mock.ResponsePredicate
}

// Set environment variables for the duration of a test. Restore their prior values
// after the test completes. Obviated by 1.17's T.Setenv
// after the test completes. uses t.Setenv on the key/value pairs in vars.
func setEnvironmentVariables(t *testing.T, vars map[string]string) {
unsetSentinel := "variables having no initial value must be unset after the test"
priorValues := make(map[string]string, len(vars))
for k, v := range vars {
priorValue, ok := os.LookupEnv(k)
if ok {
priorValues[k] = priorValue
} else {
priorValues[k] = unsetSentinel
}
err := os.Setenv(k, v)
if err != nil {
t.Fatalf("Unexpected error setting %s: %v", k, err)
}
t.Setenv(k, v)
}

t.Cleanup(func() {
for k, v := range priorValues {
var err error
if v == unsetSentinel {
err = os.Unsetenv(k)
} else {
err = os.Setenv(k, v)
}
if err != nil {
t.Fatalf("Unexpected error resetting %s: %v", k, err)
}
}
})
}

func Test_WellKnownHosts(t *testing.T) {
Expand Down

0 comments on commit 5917509

Please sign in to comment.