mirror of
https://github.com/spf13/viper
synced 2024-12-22 11:37:02 +00:00
test: remove testutil.Setenv as not needed from Go 1.17
This commit is contained in:
parent
a0696bbc97
commit
13dbfcafff
4 changed files with 23 additions and 83 deletions
|
@ -1,40 +0,0 @@
|
||||||
//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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// setenv sets or unsets an environment variable to a temporary value for the
|
|
||||||
// duration of the test
|
|
||||||
func setenv(t *testing.T, name, val string, valOK bool) {
|
|
||||||
oldVal, oldOK := os.LookupEnv(name)
|
|
||||||
if valOK {
|
|
||||||
os.Setenv(name, val)
|
|
||||||
} else {
|
|
||||||
os.Unsetenv(name)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
if oldOK {
|
|
||||||
os.Setenv(name, oldVal)
|
|
||||||
} else {
|
|
||||||
os.Unsetenv(name)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
|
@ -1,18 +0,0 @@
|
||||||
//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)
|
|
||||||
}
|
|
|
@ -15,8 +15,6 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/viper/internal/testutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCopyAndInsensitiviseMap(t *testing.T) {
|
func TestCopyAndInsensitiviseMap(t *testing.T) {
|
||||||
|
@ -64,8 +62,8 @@ func TestAbsPathify(t *testing.T) {
|
||||||
homer := filepath.Join(home, "homer")
|
homer := filepath.Join(home, "homer")
|
||||||
wd, _ := os.Getwd()
|
wd, _ := os.Getwd()
|
||||||
|
|
||||||
testutil.Setenv(t, "HOMER_ABSOLUTE_PATH", homer)
|
t.Setenv("HOMER_ABSOLUTE_PATH", homer)
|
||||||
testutil.Setenv(t, "VAR_WITH_RELATIVE_PATH", "relative")
|
t.Setenv("VAR_WITH_RELATIVE_PATH", "relative")
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input string
|
input string
|
||||||
|
|
|
@ -620,10 +620,10 @@ func TestEnv(t *testing.T) {
|
||||||
BindEnv("id")
|
BindEnv("id")
|
||||||
BindEnv("f", "FOOD", "OLD_FOOD")
|
BindEnv("f", "FOOD", "OLD_FOOD")
|
||||||
|
|
||||||
testutil.Setenv(t, "ID", "13")
|
t.Setenv("ID", "13")
|
||||||
testutil.Setenv(t, "FOOD", "apple")
|
t.Setenv("FOOD", "apple")
|
||||||
testutil.Setenv(t, "OLD_FOOD", "banana")
|
t.Setenv("OLD_FOOD", "banana")
|
||||||
testutil.Setenv(t, "NAME", "crunk")
|
t.Setenv("NAME", "crunk")
|
||||||
|
|
||||||
assert.Equal(t, "13", Get("id"))
|
assert.Equal(t, "13", Get("id"))
|
||||||
assert.Equal(t, "apple", Get("f"))
|
assert.Equal(t, "apple", Get("f"))
|
||||||
|
@ -639,7 +639,7 @@ func TestMultipleEnv(t *testing.T) {
|
||||||
|
|
||||||
BindEnv("f", "FOOD", "OLD_FOOD")
|
BindEnv("f", "FOOD", "OLD_FOOD")
|
||||||
|
|
||||||
testutil.Setenv(t, "OLD_FOOD", "banana")
|
t.Setenv("OLD_FOOD", "banana")
|
||||||
|
|
||||||
assert.Equal(t, "banana", Get("f"))
|
assert.Equal(t, "banana", Get("f"))
|
||||||
}
|
}
|
||||||
|
@ -650,7 +650,7 @@ func TestEmptyEnv(t *testing.T) {
|
||||||
BindEnv("type") // Empty environment variable
|
BindEnv("type") // Empty environment variable
|
||||||
BindEnv("name") // Bound, but not set environment variable
|
BindEnv("name") // Bound, but not set environment variable
|
||||||
|
|
||||||
testutil.Setenv(t, "TYPE", "")
|
t.Setenv("TYPE", "")
|
||||||
|
|
||||||
assert.Equal(t, "donut", Get("type"))
|
assert.Equal(t, "donut", Get("type"))
|
||||||
assert.Equal(t, "Cake", Get("name"))
|
assert.Equal(t, "Cake", Get("name"))
|
||||||
|
@ -664,7 +664,7 @@ func TestEmptyEnv_Allowed(t *testing.T) {
|
||||||
BindEnv("type") // Empty environment variable
|
BindEnv("type") // Empty environment variable
|
||||||
BindEnv("name") // Bound, but not set environment variable
|
BindEnv("name") // Bound, but not set environment variable
|
||||||
|
|
||||||
testutil.Setenv(t, "TYPE", "")
|
t.Setenv("TYPE", "")
|
||||||
|
|
||||||
assert.Equal(t, "", Get("type"))
|
assert.Equal(t, "", Get("type"))
|
||||||
assert.Equal(t, "Cake", Get("name"))
|
assert.Equal(t, "Cake", Get("name"))
|
||||||
|
@ -677,9 +677,9 @@ func TestEnvPrefix(t *testing.T) {
|
||||||
BindEnv("id")
|
BindEnv("id")
|
||||||
BindEnv("f", "FOOD") // not using prefix
|
BindEnv("f", "FOOD") // not using prefix
|
||||||
|
|
||||||
testutil.Setenv(t, "FOO_ID", "13")
|
t.Setenv("FOO_ID", "13")
|
||||||
testutil.Setenv(t, "FOOD", "apple")
|
t.Setenv("FOOD", "apple")
|
||||||
testutil.Setenv(t, "FOO_NAME", "crunk")
|
t.Setenv("FOO_NAME", "crunk")
|
||||||
|
|
||||||
assert.Equal(t, "13", Get("id"))
|
assert.Equal(t, "13", Get("id"))
|
||||||
assert.Equal(t, "apple", Get("f"))
|
assert.Equal(t, "apple", Get("f"))
|
||||||
|
@ -695,7 +695,7 @@ func TestAutoEnv(t *testing.T) {
|
||||||
|
|
||||||
AutomaticEnv()
|
AutomaticEnv()
|
||||||
|
|
||||||
testutil.Setenv(t, "FOO_BAR", "13")
|
t.Setenv("FOO_BAR", "13")
|
||||||
|
|
||||||
assert.Equal(t, "13", Get("foo_bar"))
|
assert.Equal(t, "13", Get("foo_bar"))
|
||||||
}
|
}
|
||||||
|
@ -706,7 +706,7 @@ func TestAutoEnvWithPrefix(t *testing.T) {
|
||||||
AutomaticEnv()
|
AutomaticEnv()
|
||||||
SetEnvPrefix("Baz")
|
SetEnvPrefix("Baz")
|
||||||
|
|
||||||
testutil.Setenv(t, "BAZ_BAR", "13")
|
t.Setenv("BAZ_BAR", "13")
|
||||||
|
|
||||||
assert.Equal(t, "13", Get("bar"))
|
assert.Equal(t, "13", Get("bar"))
|
||||||
}
|
}
|
||||||
|
@ -716,7 +716,7 @@ func TestSetEnvKeyReplacer(t *testing.T) {
|
||||||
|
|
||||||
AutomaticEnv()
|
AutomaticEnv()
|
||||||
|
|
||||||
testutil.Setenv(t, "REFRESH_INTERVAL", "30s")
|
t.Setenv("REFRESH_INTERVAL", "30s")
|
||||||
|
|
||||||
replacer := strings.NewReplacer("-", "_")
|
replacer := strings.NewReplacer("-", "_")
|
||||||
SetEnvKeyReplacer(replacer)
|
SetEnvKeyReplacer(replacer)
|
||||||
|
@ -729,7 +729,7 @@ func TestEnvKeyReplacer(t *testing.T) {
|
||||||
|
|
||||||
v.AutomaticEnv()
|
v.AutomaticEnv()
|
||||||
|
|
||||||
testutil.Setenv(t, "REFRESH_INTERVAL", "30s")
|
t.Setenv("REFRESH_INTERVAL", "30s")
|
||||||
|
|
||||||
assert.Equal(t, "30s", v.Get("refresh-interval"))
|
assert.Equal(t, "30s", v.Get("refresh-interval"))
|
||||||
}
|
}
|
||||||
|
@ -741,14 +741,14 @@ func TestEnvSubConfig(t *testing.T) {
|
||||||
|
|
||||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
|
|
||||||
testutil.Setenv(t, "CLOTHING_PANTS_SIZE", "small")
|
t.Setenv("CLOTHING_PANTS_SIZE", "small")
|
||||||
subv := v.Sub("clothing").Sub("pants")
|
subv := v.Sub("clothing").Sub("pants")
|
||||||
assert.Equal(t, "small", subv.Get("size"))
|
assert.Equal(t, "small", subv.Get("size"))
|
||||||
|
|
||||||
// again with EnvPrefix
|
// again with EnvPrefix
|
||||||
v.SetEnvPrefix("foo") // will be uppercased automatically
|
v.SetEnvPrefix("foo") // will be uppercased automatically
|
||||||
subWithPrefix := v.Sub("clothing").Sub("pants")
|
subWithPrefix := v.Sub("clothing").Sub("pants")
|
||||||
testutil.Setenv(t, "FOO_CLOTHING_PANTS_SIZE", "large")
|
t.Setenv("FOO_CLOTHING_PANTS_SIZE", "large")
|
||||||
assert.Equal(t, "large", subWithPrefix.Get("size"))
|
assert.Equal(t, "large", subWithPrefix.Get("size"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -875,8 +875,8 @@ func TestAllKeysWithEnv(t *testing.T) {
|
||||||
v.BindEnv("foo.bar")
|
v.BindEnv("foo.bar")
|
||||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
|
|
||||||
testutil.Setenv(t, "ID", "13")
|
t.Setenv("ID", "13")
|
||||||
testutil.Setenv(t, "FOO_BAR", "baz")
|
t.Setenv("FOO_BAR", "baz")
|
||||||
|
|
||||||
expectedKeys := sort.StringSlice{"id", "foo.bar"}
|
expectedKeys := sort.StringSlice{"id", "foo.bar"}
|
||||||
expectedKeys.Sort()
|
expectedKeys.Sort()
|
||||||
|
@ -1308,7 +1308,7 @@ func TestBoundCaseSensitivity(t *testing.T) {
|
||||||
|
|
||||||
BindEnv("eYEs", "TURTLE_EYES")
|
BindEnv("eYEs", "TURTLE_EYES")
|
||||||
|
|
||||||
testutil.Setenv(t, "TURTLE_EYES", "blue")
|
t.Setenv("TURTLE_EYES", "blue")
|
||||||
|
|
||||||
assert.Equal(t, "blue", Get("eyes"))
|
assert.Equal(t, "blue", Get("eyes"))
|
||||||
|
|
||||||
|
@ -1492,8 +1492,8 @@ func TestIsSet(t *testing.T) {
|
||||||
v.BindEnv("clothing.hat")
|
v.BindEnv("clothing.hat")
|
||||||
v.BindEnv("clothing.hats")
|
v.BindEnv("clothing.hats")
|
||||||
|
|
||||||
testutil.Setenv(t, "FOO", "bar")
|
t.Setenv("FOO", "bar")
|
||||||
testutil.Setenv(t, "CLOTHING_HAT", "bowler")
|
t.Setenv("CLOTHING_HAT", "bowler")
|
||||||
|
|
||||||
assert.True(t, v.IsSet("eyes")) // in the config file
|
assert.True(t, v.IsSet("eyes")) // in the config file
|
||||||
assert.True(t, v.IsSet("foo")) // in the environment
|
assert.True(t, v.IsSet("foo")) // in the environment
|
||||||
|
|
Loading…
Reference in a new issue