Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomwright/tmpenv
Set temporary environment variables and program arguments.
https://github.com/tomwright/tmpenv
arguments environment environment-variables test test-driven-development testing testing-tools tests
Last synced: 19 days ago
JSON representation
Set temporary environment variables and program arguments.
- Host: GitHub
- URL: https://github.com/tomwright/tmpenv
- Owner: TomWright
- License: mit
- Created: 2019-04-25T12:12:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-04-25T12:38:29.000Z (over 5 years ago)
- Last Synced: 2024-10-04T02:41:19.415Z (about 1 month ago)
- Topics: arguments, environment, environment-variables, test, test-driven-development, testing, testing-tools, tests
- Language: Go
- Size: 2.93 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# tmpenv
[![Build Status](https://travis-ci.org/TomWright/tmpenv.svg?branch=master)](https://travis-ci.org/TomWright/tmpenv)
[![codecov](https://codecov.io/gh/TomWright/tmpenv/branch/master/graph/badge.svg)](https://codecov.io/gh/TomWright/tmpenv)
[![Documentation](https://godoc.org/github.com/TomWright/tmpenv?status.svg)](https://godoc.org/github.com/TomWright/tmpenv)Easily set temporary environment variables and commandline flags, usually within a testing environment.
## Quick start
`SetEnvVar`, `SetEnvVars` and `AppendOSArgs` all return a single `func`, which when executed will reset the given variables to their previous state.
*Important note - Things may not work as expected if you run your tests in parallel!*
### Setting environment variables
```
func TestSomething(t *testing.T) {
log.Println(os.Getenv("A")) // ""
t.Run("test one", func(t *testing.T) {
resetEnvVars := tmpenv.SetEnvVars(map[string]string{
"A": "123",
})
defer resetEnvVars()log.Println(os.Getenv("A")) // "123"
})
log.Println(os.Getenv("A")) // ""
t.Run("test two", func(t *testing.T) {
resetEnvVars := tmpenv.SetEnvVars(map[string]string{
"A": "456",
})
defer resetEnvVars()log.Println(os.Getenv("A")) // "456"
})log.Println(os.Getenv("A")) // ""
}
```