mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
test: refactor asserts
This commit is contained in:
parent
3d006fe361
commit
c292b55050
3 changed files with 21 additions and 36 deletions
|
@ -39,7 +39,7 @@ func TestBindFlagValueSet(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
for name, expected := range mutatedTestValues {
|
for name, expected := range mutatedTestValues {
|
||||||
assert.Equal(t, Get(name), expected)
|
assert.Equal(t, expected, Get(name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package viper
|
package viper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -148,10 +147,9 @@ func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string,
|
||||||
|
|
||||||
// loop through map
|
// loop through map
|
||||||
var m map[string]any
|
var m map[string]any
|
||||||
err := false
|
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
if val == nil {
|
if val == nil {
|
||||||
assert.Fail(fmt.Sprintf("%s is not a map[string]any", ms))
|
assert.Failf("%s is not a map[string]any", ms)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,13 +160,11 @@ func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string,
|
||||||
case map[string]any:
|
case map[string]any:
|
||||||
m = val
|
m = val
|
||||||
default:
|
default:
|
||||||
assert.Fail(fmt.Sprintf("%s is not a map[string]any", ms))
|
assert.Failf("%s is not a map[string]any", ms)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ms = ms + "[\"" + k + "\"]"
|
ms = ms + "[\"" + k + "\"]"
|
||||||
val = m[k]
|
val = m[k]
|
||||||
}
|
}
|
||||||
if !err {
|
|
||||||
assert.Equal(value, val)
|
assert.Equal(value, val)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -16,7 +16,6 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -737,7 +736,7 @@ func TestEnvSubConfig(t *testing.T) {
|
||||||
func TestAllKeys(t *testing.T) {
|
func TestAllKeys(t *testing.T) {
|
||||||
initConfigs()
|
initConfigs()
|
||||||
|
|
||||||
ks := sort.StringSlice{
|
ks := []string{
|
||||||
"title",
|
"title",
|
||||||
"author.bio",
|
"author.bio",
|
||||||
"author.e-mail",
|
"author.e-mail",
|
||||||
|
@ -841,11 +840,7 @@ func TestAllKeys(t *testing.T) {
|
||||||
"name_dotenv": "Cake",
|
"name_dotenv": "Cake",
|
||||||
}
|
}
|
||||||
|
|
||||||
allkeys := sort.StringSlice(AllKeys())
|
assert.ElementsMatch(t, ks, AllKeys())
|
||||||
allkeys.Sort()
|
|
||||||
ks.Sort()
|
|
||||||
|
|
||||||
assert.Equal(t, ks, allkeys)
|
|
||||||
assert.Equal(t, all, AllSettings())
|
assert.Equal(t, all, AllSettings())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -860,11 +855,7 @@ func TestAllKeysWithEnv(t *testing.T) {
|
||||||
t.Setenv("ID", "13")
|
t.Setenv("ID", "13")
|
||||||
t.Setenv("FOO_BAR", "baz")
|
t.Setenv("FOO_BAR", "baz")
|
||||||
|
|
||||||
expectedKeys := sort.StringSlice{"id", "foo.bar"}
|
assert.ElementsMatch(t, []string{"id", "foo.bar"}, v.AllKeys())
|
||||||
expectedKeys.Sort()
|
|
||||||
keys := sort.StringSlice(v.AllKeys())
|
|
||||||
keys.Sort()
|
|
||||||
assert.Equal(t, expectedKeys, keys)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAliasesOfAliases(t *testing.T) {
|
func TestAliasesOfAliases(t *testing.T) {
|
||||||
|
@ -1528,7 +1519,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
|
||||||
v.AddConfigPath(`thispathaintthere`)
|
v.AddConfigPath(`thispathaintthere`)
|
||||||
|
|
||||||
err := v.ReadInConfig()
|
err := v.ReadInConfig()
|
||||||
assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundError{"", ""}), reflect.TypeOf(err))
|
assert.IsType(t, err, ConfigFileNotFoundError{"", ""})
|
||||||
|
|
||||||
// Even though config did not load and the error might have
|
// Even though config did not load and the error might have
|
||||||
// been ignored by the client, the default still loads
|
// been ignored by the client, the default still loads
|
||||||
|
@ -1583,12 +1574,10 @@ func TestSub(t *testing.T) {
|
||||||
assert.Equal(t, (*Viper)(nil), subv)
|
assert.Equal(t, (*Viper)(nil), subv)
|
||||||
|
|
||||||
subv = v.Sub("clothing")
|
subv = v.Sub("clothing")
|
||||||
assert.Equal(t, subv.parents[0], "clothing")
|
assert.Equal(t, []string{"clothing"}, subv.parents)
|
||||||
|
|
||||||
subv = v.Sub("clothing").Sub("pants")
|
subv = v.Sub("clothing").Sub("pants")
|
||||||
assert.Equal(t, len(subv.parents), 2)
|
assert.Equal(t, []string{"clothing", "pants"}, subv.parents)
|
||||||
assert.Equal(t, subv.parents[0], "clothing")
|
|
||||||
assert.Equal(t, subv.parents[1], "pants")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var hclWriteExpected = []byte(`"foos" = {
|
var hclWriteExpected = []byte(`"foos" = {
|
||||||
|
@ -2399,7 +2388,7 @@ func TestParseNested(t *testing.T) {
|
||||||
t.Fatalf("unable to decode into struct, %v", err)
|
t.Fatalf("unable to decode into struct, %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, 1, len(items))
|
assert.Len(t, items, 1)
|
||||||
assert.Equal(t, 100*time.Millisecond, items[0].Delay)
|
assert.Equal(t, 100*time.Millisecond, items[0].Delay)
|
||||||
assert.Equal(t, 200*time.Millisecond, items[0].Nested.Delay)
|
assert.Equal(t, 200*time.Millisecond, items[0].Nested.Delay)
|
||||||
}
|
}
|
||||||
|
@ -2421,11 +2410,11 @@ func newViperWithConfigFile(t *testing.T) (*Viper, string) {
|
||||||
watchDir := t.TempDir()
|
watchDir := t.TempDir()
|
||||||
configFile := path.Join(watchDir, "config.yaml")
|
configFile := path.Join(watchDir, "config.yaml")
|
||||||
err := os.WriteFile(configFile, []byte("foo: bar\n"), 0o640)
|
err := os.WriteFile(configFile, []byte("foo: bar\n"), 0o640)
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
v := New()
|
v := New()
|
||||||
v.SetConfigFile(configFile)
|
v.SetConfigFile(configFile)
|
||||||
err = v.ReadInConfig()
|
err = v.ReadInConfig()
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, "bar", v.Get("foo"))
|
require.Equal(t, "bar", v.Get("foo"))
|
||||||
return v, configFile
|
return v, configFile
|
||||||
}
|
}
|
||||||
|
@ -2434,11 +2423,11 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
|
||||||
watchDir := t.TempDir()
|
watchDir := t.TempDir()
|
||||||
dataDir1 := path.Join(watchDir, "data1")
|
dataDir1 := path.Join(watchDir, "data1")
|
||||||
err := os.Mkdir(dataDir1, 0o777)
|
err := os.Mkdir(dataDir1, 0o777)
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
realConfigFile := path.Join(dataDir1, "config.yaml")
|
realConfigFile := path.Join(dataDir1, "config.yaml")
|
||||||
t.Logf("Real config file location: %s\n", realConfigFile)
|
t.Logf("Real config file location: %s\n", realConfigFile)
|
||||||
err = os.WriteFile(realConfigFile, []byte("foo: bar\n"), 0o640)
|
err = os.WriteFile(realConfigFile, []byte("foo: bar\n"), 0o640)
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
// now, symlink the tm `data1` dir to `data` in the baseDir
|
// now, symlink the tm `data1` dir to `data` in the baseDir
|
||||||
os.Symlink(dataDir1, path.Join(watchDir, "data"))
|
os.Symlink(dataDir1, path.Join(watchDir, "data"))
|
||||||
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
|
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
|
||||||
|
@ -2449,7 +2438,7 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
|
||||||
v := New()
|
v := New()
|
||||||
v.SetConfigFile(configFile)
|
v.SetConfigFile(configFile)
|
||||||
err = v.ReadInConfig()
|
err = v.ReadInConfig()
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, "bar", v.Get("foo"))
|
require.Equal(t, "bar", v.Get("foo"))
|
||||||
return v, watchDir, configFile
|
return v, watchDir, configFile
|
||||||
}
|
}
|
||||||
|
@ -2480,7 +2469,7 @@ func TestWatchFile(t *testing.T) {
|
||||||
err = os.WriteFile(configFile, []byte("foo: baz\n"), 0o640)
|
err = os.WriteFile(configFile, []byte("foo: baz\n"), 0o640)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
// then the config value should have changed
|
// then the config value should have changed
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, "baz", v.Get("foo"))
|
assert.Equal(t, "baz", v.Get("foo"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -2500,16 +2489,16 @@ func TestWatchFile(t *testing.T) {
|
||||||
// when link to another `config.yaml` file
|
// when link to another `config.yaml` file
|
||||||
dataDir2 := path.Join(watchDir, "data2")
|
dataDir2 := path.Join(watchDir, "data2")
|
||||||
err := os.Mkdir(dataDir2, 0o777)
|
err := os.Mkdir(dataDir2, 0o777)
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
configFile2 := path.Join(dataDir2, "config.yaml")
|
configFile2 := path.Join(dataDir2, "config.yaml")
|
||||||
err = os.WriteFile(configFile2, []byte("foo: baz\n"), 0o640)
|
err = os.WriteFile(configFile2, []byte("foo: baz\n"), 0o640)
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
// change the symlink using the `ln -sfn` command
|
// change the symlink using the `ln -sfn` command
|
||||||
err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()
|
err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
// then
|
// then
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, "baz", v.Get("foo"))
|
assert.Equal(t, "baz", v.Get("foo"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue