Skip to content

test: use t.Setenv to set env vars #482

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

Merged
merged 2 commits into from
Oct 3, 2023
Merged

test: use t.Setenv to set env vars #482

merged 2 commits into from
Oct 3, 2023

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Mar 31, 2023

What type of PR is this?
/kind cleanup

What does this PR do / why we need it:

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")
}

Have you updated the necessary documentation?

  • Documentation update is required by this PR.
  • Documentation has been updated.

Which issue(s) this PR fixes:

Test acceptance criteria:

  • Unit Test
  • E2E Test

How to test changes / Special notes to the reviewer:

@openshift-ci
Copy link

openshift-ci bot commented Mar 31, 2023

@Juneezee: The label(s) kind/code-refactoring cannot be applied, because the repository doesn't have them.

In response to this:

What type of PR is this?
/kind code-refactoring

What does this PR do / why we need it:

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")
}

Have you updated the necessary documentation?

  • Documentation update is required by this PR.
  • Documentation has been updated.

Which issue(s) this PR fixes:

Test acceptance criteria:

  • Unit Test
  • E2E Test

How to test changes / Special notes to the reviewer:

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@openshift-ci
Copy link

openshift-ci bot commented Mar 31, 2023

Hi @Juneezee. Thanks for your PR.

I'm waiting for a redhat-developer member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@openshift-ci openshift-ci bot added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process. label Mar 31, 2023
@openshift-ci openshift-ci bot requested review from sbose78 and wtam2018 May 13, 2023 07:18
@wtam2018
Copy link
Collaborator

/ok-to-test

@wtam2018
Copy link
Collaborator

/lgtm
/approve

@wtam2018
Copy link
Collaborator

thanks @Juneezee !

@openshift-ci openshift-ci bot removed the lgtm label Jul 14, 2023
@openshift-ci openshift-ci bot requested a review from jaideepr97 July 14, 2023 03:18
@openshift-ci openshift-ci bot requested review from ciiay and jgwest September 12, 2023 15:05
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 <[email protected]>
@Juneezee
Copy link
Contributor Author

/retest

@Juneezee
Copy link
Contributor Author

Juneezee commented Oct 3, 2023

/retest

@wtam2018
Copy link
Collaborator

wtam2018 commented Oct 3, 2023

/lgtm
/approve

@openshift-ci openshift-ci bot added the lgtm label Oct 3, 2023
@openshift-ci
Copy link

openshift-ci bot commented Oct 3, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wtam2018

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot merged commit 9373a9b into redhat-developer:master Oct 3, 2023
trdoyle81 pushed a commit to trdoyle81/gitops-operator that referenced this pull request Aug 13, 2024
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 <[email protected]>
Co-authored-by: William Tam <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved kind/cleanup Categorizes issue or PR as related to cleaning up code, process. lgtm ok-to-test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants