package env
import "gotest.tools/v3/env"
Package env provides functions to test code that read environment variables or the current working directory.
Index ¶
- func ChangeWorkingDir(t assert.TestingT, dir string) func()
- func Patch(t assert.TestingT, key, value string) func()
- func PatchAll(t assert.TestingT, env map[string]string) func()
- func ToMap(env []string) map[string]string
Examples ¶
Functions ¶
func ChangeWorkingDir ¶
ChangeWorkingDir to the directory, and return a function which restores the previous working directory.
When used with Go 1.14+ the previous working directory will be restored automatically when the test ends, unless the TEST_NOCLEANUP env var is set to true.
func Patch ¶
Patch changes the value of an environment variable, and returns a function which will reset the the value of that variable back to the previous state.
When used with Go 1.14+ the unpatch function will be called automatically when the test ends, unless the TEST_NOCLEANUP env var is set to true.
Deprecated: use t.SetEnv
Patch an environment variable and defer to return to the previous state
Code:
Example¶
{
defer Patch(t, "PATH", "/custom/path")()
}
func PatchAll ¶
PatchAll sets the environment to env, and returns a function which will reset the environment back to the previous state.
When used with Go 1.14+ the unpatch function will be called automatically
when the test ends, unless the TEST_NOCLEANUP env var is set to true.
Patch all environment variables
Code:
Example¶
{
defer PatchAll(t, map[string]string{
"ONE": "FOO",
"TWO": "BAR",
})()
}
func ToMap ¶
ToMap takes a list of strings in the format returned by os.Environ and returns a mapping of keys to values.
Source Files ¶
env.go
- Version
- v3.5.2 (latest)
- Published
- Sep 5, 2024
- Platform
- linux/amd64
- Imports
- 4 packages
- Last checked
- 2 months ago –
Tools for package owners.