1
2
3
4
5
6
7
8
9
10
11 package viper
12
13 import (
14 "os"
15 "path/filepath"
16 "testing"
17
18 slog "github.com/sagikazarmark/slog-shim"
19 "github.com/stretchr/testify/assert"
20 )
21
22 func TestCopyAndInsensitiviseMap(t *testing.T) {
23 var (
24 given = map[string]any{
25 "Foo": 32,
26 "Bar": map[any]any{
27 "ABc": "A",
28 "cDE": "B",
29 },
30 }
31 expected = map[string]any{
32 "foo": 32,
33 "bar": map[string]any{
34 "abc": "A",
35 "cde": "B",
36 },
37 }
38 )
39
40 got := copyAndInsensitiviseMap(given)
41
42 assert.Equal(t, expected, got)
43 _, ok := given["foo"]
44 assert.False(t, ok)
45 _, ok = given["bar"]
46 assert.False(t, ok)
47
48 m := given["Bar"].(map[any]any)
49 _, ok = m["ABc"]
50 assert.True(t, ok)
51 }
52
53 func TestAbsPathify(t *testing.T) {
54 skipWindows(t)
55
56 home := userHomeDir()
57 homer := filepath.Join(home, "homer")
58 wd, _ := os.Getwd()
59
60 t.Setenv("HOMER_ABSOLUTE_PATH", homer)
61 t.Setenv("VAR_WITH_RELATIVE_PATH", "relative")
62
63 tests := []struct {
64 input string
65 output string
66 }{
67 {"", wd},
68 {"sub", filepath.Join(wd, "sub")},
69 {"./", wd},
70 {"./sub", filepath.Join(wd, "sub")},
71 {"$HOME", home},
72 {"$HOME/", home},
73 {"$HOME/sub", filepath.Join(home, "sub")},
74 {"$HOMER_ABSOLUTE_PATH", homer},
75 {"$HOMER_ABSOLUTE_PATH/", homer},
76 {"$HOMER_ABSOLUTE_PATH/sub", filepath.Join(homer, "sub")},
77 {"$VAR_WITH_RELATIVE_PATH", filepath.Join(wd, "relative")},
78 {"$VAR_WITH_RELATIVE_PATH/", filepath.Join(wd, "relative")},
79 {"$VAR_WITH_RELATIVE_PATH/sub", filepath.Join(wd, "relative", "sub")},
80 }
81
82 for _, test := range tests {
83 got := absPathify(slog.Default(), test.input)
84 assert.Equal(t, test.output, got)
85 }
86 }
87
View as plain text