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 #2437

Merged
merged 3 commits into from
Jan 25, 2024
Merged

test: use t.Setenv to set env vars in tests #2437

merged 3 commits into from
Jan 25, 2024

Conversation

Juneezee
Copy link
Contributor

What does this PR do?

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
	os.Setenv(key, "new value")
	defer os.Unsetenv(key)
	
	// after
	t.Setenv(key, "new value")
}

Motivation

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • System-Tests covering this feature have been added and enabled with the va.b.c-dev version tag.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.

For Datadog employees:

  • If this PR touches code that handles credentials of any kind, such as Datadog API keys, I've requested a review from @DataDog/security-design-and-guidance.
  • This PR doesn't touch any of that.

Unsure? Have a question? Request a review!

@Juneezee Juneezee requested a review from a team December 14, 2023 18:10
@dianashevchenko
Copy link
Contributor

IIRC, we have attempted to do that before and it increased flakiness in the tests, that is Unsetenv was actually not called properly in sub tests. Will verify if it's still the case

@Juneezee Juneezee requested a review from a team as a code owner January 8, 2024 13:48
@katiehockman
Copy link
Contributor

IIRC, we have attempted to do that before and it increased flakiness in the tests, that is Unsetenv was actually not called properly in sub tests. Will verify if it's still the case

I was playing around with this locally, and it appears to be working as we would expect now (i.e. t.Setenv sets the environment at the beginning of the subtest, and then unsets it at the end). I also recall it being broken previously, @dianashevchenko, so maybe the fixed it?

Looking at the code, it's unset via an os.Unsetenv function within a c.Cleanup call which runs in runCleanup, which runs after the test is finished. Each t.Run execution triggers a tRunner which is where it calls that cleanup, so the code also looks like it should be doing what we expect.

ddtrace/tracer/context_test.go Outdated Show resolved Hide resolved
@Juneezee
Copy link
Contributor Author

@katiehockman Hi 👋 . If the changes look good to you, I would appreciate it if you could approve the workflow run and consider merging the PR. I'm happy to answer any questions or address any concerns you may have.

Thanks for your time and consideration!

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>
Reference: #2437 (review)
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
Reference: #2437 (comment)
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@katiehockman katiehockman enabled auto-merge (squash) January 25, 2024 14:56
@katiehockman katiehockman merged commit c03c56b into DataDog:main Jan 25, 2024
100 checks passed
darccio pushed a commit that referenced this pull request Feb 5, 2024
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.

None yet

4 participants