Skip to content

Commit c292b55

Browse files
alexandearsagikazarmark
authored andcommitted
test: refactor asserts
1 parent 3d006fe commit c292b55

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

flags_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestBindFlagValueSet(t *testing.T) {
3939
})
4040

4141
for name, expected := range mutatedTestValues {
42-
assert.Equal(t, Get(name), expected)
42+
assert.Equal(t, expected, Get(name))
4343
}
4444
}
4545

overrides_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package viper
22

33
import (
4-
"fmt"
54
"strings"
65
"testing"
76

@@ -148,10 +147,9 @@ func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string,
148147

149148
// loop through map
150149
var m map[string]any
151-
err := false
152150
for _, k := range keys {
153151
if val == nil {
154-
assert.Fail(fmt.Sprintf("%s is not a map[string]any", ms))
152+
assert.Failf("%s is not a map[string]any", ms)
155153
return
156154
}
157155

@@ -162,13 +160,11 @@ func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string,
162160
case map[string]any:
163161
m = val
164162
default:
165-
assert.Fail(fmt.Sprintf("%s is not a map[string]any", ms))
163+
assert.Failf("%s is not a map[string]any", ms)
166164
return
167165
}
168166
ms = ms + "[\"" + k + "\"]"
169167
val = m[k]
170168
}
171-
if !err {
172-
assert.Equal(value, val)
173-
}
169+
assert.Equal(value, val)
174170
}

viper_test.go

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"path/filepath"
1717
"reflect"
1818
"runtime"
19-
"sort"
2019
"strings"
2120
"sync"
2221
"testing"
@@ -737,7 +736,7 @@ func TestEnvSubConfig(t *testing.T) {
737736
func TestAllKeys(t *testing.T) {
738737
initConfigs()
739738

740-
ks := sort.StringSlice{
739+
ks := []string{
741740
"title",
742741
"author.bio",
743742
"author.e-mail",
@@ -841,11 +840,7 @@ func TestAllKeys(t *testing.T) {
841840
"name_dotenv": "Cake",
842841
}
843842

844-
allkeys := sort.StringSlice(AllKeys())
845-
allkeys.Sort()
846-
ks.Sort()
847-
848-
assert.Equal(t, ks, allkeys)
843+
assert.ElementsMatch(t, ks, AllKeys())
849844
assert.Equal(t, all, AllSettings())
850845
}
851846

@@ -860,11 +855,7 @@ func TestAllKeysWithEnv(t *testing.T) {
860855
t.Setenv("ID", "13")
861856
t.Setenv("FOO_BAR", "baz")
862857

863-
expectedKeys := sort.StringSlice{"id", "foo.bar"}
864-
expectedKeys.Sort()
865-
keys := sort.StringSlice(v.AllKeys())
866-
keys.Sort()
867-
assert.Equal(t, expectedKeys, keys)
858+
assert.ElementsMatch(t, []string{"id", "foo.bar"}, v.AllKeys())
868859
}
869860

870861
func TestAliasesOfAliases(t *testing.T) {
@@ -1528,7 +1519,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
15281519
v.AddConfigPath(`thispathaintthere`)
15291520

15301521
err := v.ReadInConfig()
1531-
assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundError{"", ""}), reflect.TypeOf(err))
1522+
assert.IsType(t, err, ConfigFileNotFoundError{"", ""})
15321523

15331524
// Even though config did not load and the error might have
15341525
// been ignored by the client, the default still loads
@@ -1583,12 +1574,10 @@ func TestSub(t *testing.T) {
15831574
assert.Equal(t, (*Viper)(nil), subv)
15841575

15851576
subv = v.Sub("clothing")
1586-
assert.Equal(t, subv.parents[0], "clothing")
1577+
assert.Equal(t, []string{"clothing"}, subv.parents)
15871578

15881579
subv = v.Sub("clothing").Sub("pants")
1589-
assert.Equal(t, len(subv.parents), 2)
1590-
assert.Equal(t, subv.parents[0], "clothing")
1591-
assert.Equal(t, subv.parents[1], "pants")
1580+
assert.Equal(t, []string{"clothing", "pants"}, subv.parents)
15921581
}
15931582

15941583
var hclWriteExpected = []byte(`"foos" = {
@@ -2399,7 +2388,7 @@ func TestParseNested(t *testing.T) {
23992388
t.Fatalf("unable to decode into struct, %v", err)
24002389
}
24012390

2402-
assert.Equal(t, 1, len(items))
2391+
assert.Len(t, items, 1)
24032392
assert.Equal(t, 100*time.Millisecond, items[0].Delay)
24042393
assert.Equal(t, 200*time.Millisecond, items[0].Nested.Delay)
24052394
}
@@ -2421,11 +2410,11 @@ func newViperWithConfigFile(t *testing.T) (*Viper, string) {
24212410
watchDir := t.TempDir()
24222411
configFile := path.Join(watchDir, "config.yaml")
24232412
err := os.WriteFile(configFile, []byte("foo: bar\n"), 0o640)
2424-
require.Nil(t, err)
2413+
require.NoError(t, err)
24252414
v := New()
24262415
v.SetConfigFile(configFile)
24272416
err = v.ReadInConfig()
2428-
require.Nil(t, err)
2417+
require.NoError(t, err)
24292418
require.Equal(t, "bar", v.Get("foo"))
24302419
return v, configFile
24312420
}
@@ -2434,11 +2423,11 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
24342423
watchDir := t.TempDir()
24352424
dataDir1 := path.Join(watchDir, "data1")
24362425
err := os.Mkdir(dataDir1, 0o777)
2437-
require.Nil(t, err)
2426+
require.NoError(t, err)
24382427
realConfigFile := path.Join(dataDir1, "config.yaml")
24392428
t.Logf("Real config file location: %s\n", realConfigFile)
24402429
err = os.WriteFile(realConfigFile, []byte("foo: bar\n"), 0o640)
2441-
require.Nil(t, err)
2430+
require.NoError(t, err)
24422431
// now, symlink the tm `data1` dir to `data` in the baseDir
24432432
os.Symlink(dataDir1, path.Join(watchDir, "data"))
24442433
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
@@ -2449,7 +2438,7 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
24492438
v := New()
24502439
v.SetConfigFile(configFile)
24512440
err = v.ReadInConfig()
2452-
require.Nil(t, err)
2441+
require.NoError(t, err)
24532442
require.Equal(t, "bar", v.Get("foo"))
24542443
return v, watchDir, configFile
24552444
}
@@ -2480,7 +2469,7 @@ func TestWatchFile(t *testing.T) {
24802469
err = os.WriteFile(configFile, []byte("foo: baz\n"), 0o640)
24812470
wg.Wait()
24822471
// then the config value should have changed
2483-
require.Nil(t, err)
2472+
require.NoError(t, err)
24842473
assert.Equal(t, "baz", v.Get("foo"))
24852474
})
24862475

@@ -2500,16 +2489,16 @@ func TestWatchFile(t *testing.T) {
25002489
// when link to another `config.yaml` file
25012490
dataDir2 := path.Join(watchDir, "data2")
25022491
err := os.Mkdir(dataDir2, 0o777)
2503-
require.Nil(t, err)
2492+
require.NoError(t, err)
25042493
configFile2 := path.Join(dataDir2, "config.yaml")
25052494
err = os.WriteFile(configFile2, []byte("foo: baz\n"), 0o640)
2506-
require.Nil(t, err)
2495+
require.NoError(t, err)
25072496
// change the symlink using the `ln -sfn` command
25082497
err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()
2509-
require.Nil(t, err)
2498+
require.NoError(t, err)
25102499
wg.Wait()
25112500
// then
2512-
require.Nil(t, err)
2501+
require.NoError(t, err)
25132502
assert.Equal(t, "baz", v.Get("foo"))
25142503
})
25152504
}

0 commit comments

Comments
 (0)