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(pkg/config): use t.Setenv to set env vars #1821

Merged
merged 3 commits into from
Jan 30, 2023
Merged

test(pkg/config): use t.Setenv to set env vars #1821

merged 3 commits into from
Jan 30, 2023

Conversation

Juneezee
Copy link
Contributor

What is the problem I am trying to address?

Ensure no leftover environment variables between tests in pkg/config/config_test.go.

How is the fix applied?

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.

Reference: https://pkg.go.dev/testing#T.Setenv

func TestFoo(t *testing.T) {
	// before
	oldVal := os.Getenv("key")
	defer os.Setenv("key", oldVal)
	os.Setenv(key, "new value")
	
	// after
	t.Setenv(key, "new value")
}

What GitHub issue(s) does this PR fix or close?

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 January 29, 2023 04:47
@manugupt1 manugupt1 requested review from r-ashish and a team January 29, 2023 06:29
pkg/config/config_test.go Show resolved Hide resolved
@r-ashish r-ashish merged commit b3bc773 into gomods:main Jan 30, 2023
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.

2 participants