Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use T.Setenv to set env vars in tests #739

Merged
merged 1 commit into from
Dec 5, 2022
Merged

test: use T.Setenv to set env vars in tests #739

merged 1 commit into from
Dec 5, 2022

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Dec 5, 2022

Issue #, if available:

Description of changes:
A testing cleanup.

This PR replaces os.Setenv with t.Setenv. Starting from Go 1.17, we can use t.Setenv to set environment variable in test. The environment variable is automatically restored to its original value when the test and all its subtests complete. This ensures that each test does not start with leftover environment variables from previous completed tests.

func TestFoo(t *testing.T) {
	// before
	key := "ENV"
	originalEnv := os.Getenv(key)
	
	if err := os.Setenv(key, "new value"); err != nil {
		t.Fatal(err)
	}
	defer func() {
		if err := os.Setenv(key, originalEnv); err != nil {
			t.Logf("failed to set env %s back to original value: %v", key, err)
		}
	}()
	
	// after
	t.Setenv(key, "new value")
}

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

This commit replaces `os.Setenv` with `t.Setenv` in tests. The
environment variable is automatically restored to its original value
when the test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.Setenv
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@Juneezee Juneezee requested a review from a team as a code owner December 5, 2022 15:54
Copy link
Contributor

@snay2 snay2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you for the contribution!

@snay2 snay2 merged commit b27b34c into aws:main Dec 5, 2022
@snay2 snay2 added the Pending-Release Pending an NTH or eks-charts release label Dec 5, 2022
@cjerad
Copy link
Contributor

cjerad commented Dec 14, 2022

Included in release v1.18.2

@cjerad cjerad removed the Pending-Release Pending an NTH or eks-charts release label Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants