| 1 |
// Licensed under the MIT license, see LICENSE file for details. |
| 2 |
|
| 3 |
//go:build !go1.17 |
| 4 |
// +build !go1.17 |
| 5 |
|
| 6 |
package quicktest |
| 7 |
|
| 8 |
import "os" |
| 9 |
|
| 10 |
// Setenv sets an environment variable to a temporary value for the |
| 11 |
// duration of the test. |
| 12 |
// |
| 13 |
// At the end of the test (see "Deferred execution" in the package docs), the |
| 14 |
// environment variable is returned to its original value. |
| 15 |
// |
| 16 |
// This is the equivalent of testing.T.Setenv introduced in Go 1.17. |
| 17 |
func (c *C) Setenv(name, val string) { |
| 18 |
oldVal, oldOK := os.LookupEnv(name) |
| 19 |
os.Setenv(name, val) |
| 20 |
c.cleanup(func() { |
| 21 |
if oldOK { |
| 22 |
os.Setenv(name, oldVal) |
| 23 |
} else { |
| 24 |
os.Unsetenv(name) |
| 25 |
} |
| 26 |
}) |
| 27 |
} |