mirror of
https://github.com/spf13/viper
synced 2024-12-22 11:37:02 +00:00
feat: use TB.Setenv on Go 1.17
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
b1fdc47b0d
commit
558a299a01
3 changed files with 40 additions and 9 deletions
|
@ -9,15 +9,6 @@ import (
|
||||||
// Licensed under the MIT license
|
// Licensed under the MIT license
|
||||||
// Copyright (c) 2017 Canonical Ltd.
|
// Copyright (c) 2017 Canonical Ltd.
|
||||||
|
|
||||||
// Setenv sets an environment variable to a temporary value for the
|
|
||||||
// duration of the test.
|
|
||||||
//
|
|
||||||
// At the end of the test (see "Deferred execution" in the package docs), the
|
|
||||||
// environment variable is returned to its original value.
|
|
||||||
func Setenv(t *testing.T, name, val string) {
|
|
||||||
setenv(t, name, val, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unsetenv unsets an environment variable for the duration of a test.
|
// Unsetenv unsets an environment variable for the duration of a test.
|
||||||
func Unsetenv(t *testing.T, name string) {
|
func Unsetenv(t *testing.T, name string) {
|
||||||
setenv(t, name, "", false)
|
setenv(t, name, "", false)
|
||||||
|
|
22
internal/testutil/env_go1_16.go
Normal file
22
internal/testutil/env_go1_16.go
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
//go:build !go1.17
|
||||||
|
// +build !go1.17
|
||||||
|
|
||||||
|
package testutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Based on https://github.com/frankban/quicktest/blob/577841610793d24f99e31cc2c0ef3a541fefd7c7/patch.go#L34-L64
|
||||||
|
// Licensed under the MIT license
|
||||||
|
// Copyright (c) 2017 Canonical Ltd.
|
||||||
|
|
||||||
|
// Setenv sets an environment variable to a temporary value for the
|
||||||
|
// duration of the test.
|
||||||
|
//
|
||||||
|
// At the end of the test (see "Deferred execution" in the package docs), the
|
||||||
|
// environment variable is returned to its original value.
|
||||||
|
func Setenv(t *testing.T, name, val string) {
|
||||||
|
setenv(t, name, val, true)
|
||||||
|
}
|
18
internal/testutil/env_go1_17.go
Normal file
18
internal/testutil/env_go1_17.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
//go:build go1.17
|
||||||
|
// +build go1.17
|
||||||
|
|
||||||
|
package testutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Setenv sets an environment variable to a temporary value for the
|
||||||
|
// duration of the test.
|
||||||
|
//
|
||||||
|
// This shim can be removed once support for Go <1.17 is dropped.
|
||||||
|
func Setenv(t *testing.T, name, val string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
t.Setenv(name, val)
|
||||||
|
}
|
Loading…
Reference in a new issue