Skip to content

proposal: testing: add Unsetenv to call os.Unsetenv and then restore the value using Cleanup #52817

Closed

Description

@favonia

I wish to have t.Unsetenv(key) that unsets environment variables and automatically restores their values using Cleanup, in a way similar to t.Setenv(key, value). Here is the hypothetical usage:

func TestNoValue(t *testing.T) {
	// unset the environment variable ENV_KEY during the test
	t.Unsetenv("ENV_KEY")

	// ... more code here ...
}

A possible implementation:

func (c *common) Unsetenv(key string) {
	c.checkFuzzFn("Unsetenv")
	prevValue, prevSet := os.LookupEnv(key)

	if err := os.Unsetenv(key); err != nil {
		c.Fatalf("cannot unset environment variable: %v", err)
	}

	if prevSet {
		c.Cleanup(func() { os.Setenv(key, prevValue) })
	} else {
		c.Cleanup(func() { os.Unsetenv(key) })
	}
}

func (t *T) Unsetenv(key) {
	if t.isParallel {
		panic("testing: t.Unsetenv called after t.Parallel; cannot unset environment variables in parallel tests")
	}

	t.isEnvSet = true

	t.common.Unsetenv(key)
}

Activity

  1. added this to the Proposal milestone on May 10, 2022
  2. mvdan commented on May 10, 2022

    @mvdan
    Member

    Does t.Setenv(key, "") not work in most cases? I understand that programs can still use os.LookupEnv to differentiate an unset variable from a set-and-empty variable, but it's not very common at all. At the end of the day, these API helpers are for common needs, and I can't say that I've personally ever needed to call os.Unsetenv as opposed to os.Setenv or t.Setenv.

  3. favonia commented on May 10, 2022

    @favonia
    ContributorAuthor

    I can't say that I've personally ever needed to call os.Unsetenv as opposed to os.Setenv or t.Setenv.

    @mvdan Thanks for your comment. I can find these lines in this repository that erase environment variables during testing:

    go/src/cmd/go/go_test.go

    Lines 277 to 280 in 6fd0520

    os.Unsetenv("GOFLAGS")
    os.Unsetenv("GOBIN")
    os.Unsetenv("GOPATH")
    os.Unsetenv("GIT_ALLOW_PROTOCOL")
    // Set up the system environment variables
    for i := range undefEnvList {
    os.Unsetenv(undefEnvList[i])
    }
    os.Unsetenv("CGO_TEST_ALLOW")

    I can't really say how common it is, but Go itself is using it for testing.

  4. rsc commented on May 11, 2022

    @rsc
    Contributor

    This proposal has been added to the active column of the proposals project
    and will now be reviewed at the weekly proposal review meetings.
    — rsc for the proposal review group

  5. natefinch commented on May 11, 2022

    @natefinch
    Contributor

    Honestly, most code (aside from reading configuration at application startup) probably shouldn't be reading from the environment anyway. It's the ultimate global variable.

    I'm not sure that adding test helpers for things you shouldn't usually do is the best idea.

    That being said, if we did want something like this, I think there's a simpler solution. The proposed function is doing two things, but only really needs to do one thing. Most of the time, test authors just want to ensure they leave the environment the way it was when the test started. This would do that, much more simply, IMO:

    // CleanupEnv reverts all changes to environment variables at the end of this test.
    func (t *testing.T) CleanupEnv() {
        vals := os.Environ()
        t.Cleanup(func(){ 
            os.Clearenv()
            for _, v := range vals {
                parts := strings.SplitN(v, "=", 2)
                os.Setenv(parts[0], parts[1])
            }
        })
    }

    (with probably better error handling)

  6. rsc commented on May 11, 2022

    @rsc
    Contributor

    @natefinch you're not wrong, but given that we already have t.Setenv, adding t.CleanupEnv separately seems like having two confusingly similar ways to do something.

    I still don't think we need t.Unsetenv though.

  7. rsc commented on May 18, 2022

    @rsc
    Contributor

    Based on the discussion above, this proposal seems like a likely decline.
    — rsc for the proposal review group

  8. rogpeppe commented on May 19, 2022

    @rogpeppe
    Contributor

    I agree that this isn't common enough to be that useful, especially as it's entirely trivial to do without the functionality built in:

    t.Setenv(x, "")
    os.Unsetenv(x)
    
  9. favonia commented on May 19, 2022

    @favonia
    ContributorAuthor
    t.Setenv(x, "")
    os.Unsetenv(x)
    

    @rogpeppe Thanks for the tip. Somehow I missed this. 😃

  10. rsc commented on May 25, 2022

    @rsc
    Contributor

    No change in consensus, so declined.
    — rsc for the proposal review group

  11. moved this to Declined in Proposalson Aug 10, 2022
  12. locked and limited conversation to collaborators on May 25, 2023
  13. removed this from Proposalson May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions