@@ -16,7 +16,6 @@ import (
16
16
"path/filepath"
17
17
"reflect"
18
18
"runtime"
19
- "sort"
20
19
"strings"
21
20
"sync"
22
21
"testing"
@@ -737,7 +736,7 @@ func TestEnvSubConfig(t *testing.T) {
737
736
func TestAllKeys (t * testing.T ) {
738
737
initConfigs ()
739
738
740
- ks := sort. StringSlice {
739
+ ks := [] string {
741
740
"title" ,
742
741
"author.bio" ,
743
742
"author.e-mail" ,
@@ -841,11 +840,7 @@ func TestAllKeys(t *testing.T) {
841
840
"name_dotenv" : "Cake" ,
842
841
}
843
842
844
- allkeys := sort .StringSlice (AllKeys ())
845
- allkeys .Sort ()
846
- ks .Sort ()
847
-
848
- assert .Equal (t , ks , allkeys )
843
+ assert .ElementsMatch (t , ks , AllKeys ())
849
844
assert .Equal (t , all , AllSettings ())
850
845
}
851
846
@@ -860,11 +855,7 @@ func TestAllKeysWithEnv(t *testing.T) {
860
855
t .Setenv ("ID" , "13" )
861
856
t .Setenv ("FOO_BAR" , "baz" )
862
857
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 ())
868
859
}
869
860
870
861
func TestAliasesOfAliases (t * testing.T ) {
@@ -1528,7 +1519,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
1528
1519
v .AddConfigPath (`thispathaintthere` )
1529
1520
1530
1521
err := v .ReadInConfig ()
1531
- assert .Equal (t , reflect . TypeOf ( ConfigFileNotFoundError {"" , "" }), reflect . TypeOf ( err ) )
1522
+ assert .IsType (t , err , ConfigFileNotFoundError {"" , "" })
1532
1523
1533
1524
// Even though config did not load and the error might have
1534
1525
// been ignored by the client, the default still loads
@@ -1583,12 +1574,10 @@ func TestSub(t *testing.T) {
1583
1574
assert .Equal (t , (* Viper )(nil ), subv )
1584
1575
1585
1576
subv = v .Sub ("clothing" )
1586
- assert .Equal (t , subv . parents [ 0 ], "clothing" )
1577
+ assert .Equal (t , [] string { "clothing" }, subv . parents )
1587
1578
1588
1579
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 )
1592
1581
}
1593
1582
1594
1583
var hclWriteExpected = []byte (`"foos" = {
@@ -2399,7 +2388,7 @@ func TestParseNested(t *testing.T) {
2399
2388
t .Fatalf ("unable to decode into struct, %v" , err )
2400
2389
}
2401
2390
2402
- assert .Equal (t , 1 , len ( items ) )
2391
+ assert .Len (t , items , 1 )
2403
2392
assert .Equal (t , 100 * time .Millisecond , items [0 ].Delay )
2404
2393
assert .Equal (t , 200 * time .Millisecond , items [0 ].Nested .Delay )
2405
2394
}
@@ -2421,11 +2410,11 @@ func newViperWithConfigFile(t *testing.T) (*Viper, string) {
2421
2410
watchDir := t .TempDir ()
2422
2411
configFile := path .Join (watchDir , "config.yaml" )
2423
2412
err := os .WriteFile (configFile , []byte ("foo: bar\n " ), 0o640 )
2424
- require .Nil (t , err )
2413
+ require .NoError (t , err )
2425
2414
v := New ()
2426
2415
v .SetConfigFile (configFile )
2427
2416
err = v .ReadInConfig ()
2428
- require .Nil (t , err )
2417
+ require .NoError (t , err )
2429
2418
require .Equal (t , "bar" , v .Get ("foo" ))
2430
2419
return v , configFile
2431
2420
}
@@ -2434,11 +2423,11 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
2434
2423
watchDir := t .TempDir ()
2435
2424
dataDir1 := path .Join (watchDir , "data1" )
2436
2425
err := os .Mkdir (dataDir1 , 0o777 )
2437
- require .Nil (t , err )
2426
+ require .NoError (t , err )
2438
2427
realConfigFile := path .Join (dataDir1 , "config.yaml" )
2439
2428
t .Logf ("Real config file location: %s\n " , realConfigFile )
2440
2429
err = os .WriteFile (realConfigFile , []byte ("foo: bar\n " ), 0o640 )
2441
- require .Nil (t , err )
2430
+ require .NoError (t , err )
2442
2431
// now, symlink the tm `data1` dir to `data` in the baseDir
2443
2432
os .Symlink (dataDir1 , path .Join (watchDir , "data" ))
2444
2433
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
@@ -2449,7 +2438,7 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
2449
2438
v := New ()
2450
2439
v .SetConfigFile (configFile )
2451
2440
err = v .ReadInConfig ()
2452
- require .Nil (t , err )
2441
+ require .NoError (t , err )
2453
2442
require .Equal (t , "bar" , v .Get ("foo" ))
2454
2443
return v , watchDir , configFile
2455
2444
}
@@ -2480,7 +2469,7 @@ func TestWatchFile(t *testing.T) {
2480
2469
err = os .WriteFile (configFile , []byte ("foo: baz\n " ), 0o640 )
2481
2470
wg .Wait ()
2482
2471
// then the config value should have changed
2483
- require .Nil (t , err )
2472
+ require .NoError (t , err )
2484
2473
assert .Equal (t , "baz" , v .Get ("foo" ))
2485
2474
})
2486
2475
@@ -2500,16 +2489,16 @@ func TestWatchFile(t *testing.T) {
2500
2489
// when link to another `config.yaml` file
2501
2490
dataDir2 := path .Join (watchDir , "data2" )
2502
2491
err := os .Mkdir (dataDir2 , 0o777 )
2503
- require .Nil (t , err )
2492
+ require .NoError (t , err )
2504
2493
configFile2 := path .Join (dataDir2 , "config.yaml" )
2505
2494
err = os .WriteFile (configFile2 , []byte ("foo: baz\n " ), 0o640 )
2506
- require .Nil (t , err )
2495
+ require .NoError (t , err )
2507
2496
// change the symlink using the `ln -sfn` command
2508
2497
err = exec .Command ("ln" , "-sfn" , dataDir2 , path .Join (watchDir , "data" )).Run ()
2509
- require .Nil (t , err )
2498
+ require .NoError (t , err )
2510
2499
wg .Wait ()
2511
2500
// then
2512
- require .Nil (t , err )
2501
+ require .NoError (t , err )
2513
2502
assert .Equal (t , "baz" , v .Get ("foo" ))
2514
2503
})
2515
2504
}
0 commit comments