Debian Codegraph @

value_source_test.go

raw symbols SCIP
1 package cli
2
3 import (
4 "fmt"
5 "math/rand"
6 "os"
7 "path/filepath"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 )
13
14 func TestZeroValueSourceChain(t *testing.T) {
15 var vc ValueSourceChain
16 assert.Empty(t, vc.EnvKeys())
17 assert.NotEmpty(t, vc.GoString())
18 assert.Empty(t, vc.Chain)
19 assert.Empty(t, vc.String())
20 }
21
22 func TestEnvVarValueSource(t *testing.T) {
23 t.Run("implements ValueSource", func(t *testing.T) {
24 src := EnvVar("foo")
25 require.Implements(t, (*ValueSource)(nil), src)
26
27 t.Run("not found", func(t *testing.T) {
28 t.Setenv("foo", "bar")
29
30 src := EnvVar("foo_1")
31 _, ok := src.Lookup()
32 require.False(t, ok)
33 })
34
35 t.Run("found", func(t *testing.T) {
36 t.Setenv("foo", "bar")
37
38 r := require.New(t)
39 src := EnvVar("foo")
40
41 str, ok := src.Lookup()
42 r.True(ok)
43 r.Equal(str, "bar")
44 })
45 })
46
47 t.Run("implements fmt.Stringer", func(t *testing.T) {
48 src := EnvVar("foo")
49 r := require.New(t)
50
51 r.Implements((*fmt.Stringer)(nil), src)
52 r.Equal("environment variable \"foo\"", src.String())
53 })
54
55 t.Run("implements fmt.GoStringer", func(t *testing.T) {
56 src := EnvVar("foo")
57 r := require.New(t)
58
59 r.Implements((*fmt.GoStringer)(nil), src)
60 r.Equal("&envVarValueSource{Key:\"foo\"}", src.GoString())
61 })
62 }
63
64 func TestEnvVars(t *testing.T) {
65 t.Setenv("myfoo", "mybar")
66
67 source := EnvVars("foo1", "myfoo")
68 str, src, ok := source.LookupWithSource()
69
70 r := require.New(t)
71 r.True(ok)
72 r.Equal(str, "mybar")
73 r.Contains(src.String(), "\"myfoo\"")
74 }
75
76 func TestFileValueSource(t *testing.T) {
77 t.Run("implements ValueSource", func(t *testing.T) {
78 r := require.New(t)
79
80 r.Implements((*ValueSource)(nil), &fileValueSource{})
81
82 t.Run("not found", func(t *testing.T) {
83 src := File(fmt.Sprintf("junk_file_name-%[1]v", rand.Int()))
84 _, ok := src.Lookup()
85 r.False(ok)
86 })
87
88 fileName := filepath.Join(os.TempDir(), fmt.Sprintf("urfave-cli-testing-existing_file-%[1]v", rand.Int()))
89 t.Cleanup(func() { _ = os.Remove(fileName) })
90
91 r.Nil(os.WriteFile(fileName, []byte("pita"), 0o644))
92
93 t.Run("found", func(t *testing.T) {
94 src := File(fileName)
95 str, ok := src.Lookup()
96 r.True(ok)
97 r.Equal("pita", str)
98 })
99 })
100
101 t.Run("implements fmt.Stringer", func(t *testing.T) {
102 src := File("/dev/null")
103 r := require.New(t)
104
105 r.Implements((*ValueSource)(nil), src)
106 r.Equal("file \"/dev/null\"", src.String())
107 })
108
109 t.Run("implements fmt.GoStringer", func(t *testing.T) {
110 src := File("/dev/null")
111 r := require.New(t)
112
113 r.Implements((*ValueSource)(nil), src)
114 r.Equal("&fileValueSource{Path:\"/dev/null\"}", src.GoString())
115 })
116 }
117
118 func TestFilePaths(t *testing.T) {
119 r := require.New(t)
120
121 fileName := filepath.Join(os.TempDir(), fmt.Sprintf("urfave-cli-tests-some_file_name_%[1]v", rand.Int()))
122 t.Cleanup(func() { _ = os.Remove(fileName) })
123
124 r.Nil(os.WriteFile(fileName, []byte("Hello"), 0o644))
125
126 sources := Files("junk_file_name", fileName)
127 str, src, ok := sources.LookupWithSource()
128 r.True(ok)
129 r.Equal(str, "Hello")
130 r.Contains(src.String(), fmt.Sprintf("%[1]q", fileName))
131 }
132
133 func TestValueSourceChainEnvKeys(t *testing.T) {
134 chain := NewValueSourceChain(
135 &staticValueSource{"hello"},
136 )
137 chain.Append(EnvVars("foo", "bar"))
138
139 r := require.New(t)
140 r.Equal([]string{"foo", "bar"}, chain.EnvKeys())
141 }
142
143 func TestValueSourceChain(t *testing.T) {
144 t.Run("implements ValueSource", func(t *testing.T) {
145 vsc := &ValueSourceChain{}
146 r := require.New(t)
147
148 r.Implements((*ValueSource)(nil), vsc)
149
150 _, ok := vsc.Lookup()
151 r.False(ok)
152 })
153
154 t.Run("implements fmt.GoStringer", func(t *testing.T) {
155 vsc := &ValueSourceChain{}
156 r := require.New(t)
157
158 r.Implements((*fmt.GoStringer)(nil), vsc)
159 r.Equal("&ValueSourceChain{Chain:{}}", vsc.GoString())
160
161 vsc1 := NewValueSourceChain(&staticValueSource{v: "yahtzee"},
162 &staticValueSource{v: "matzoh"},
163 )
164 r.Equal("&ValueSourceChain{Chain:{&staticValueSource{v:\"yahtzee\"},&staticValueSource{v:\"matzoh\"}}}", vsc1.GoString())
165 })
166
167 t.Run("implements fmt.Stringer", func(t *testing.T) {
168 vsc := &ValueSourceChain{}
169 r := require.New(t)
170
171 r.Implements((*fmt.Stringer)(nil), vsc)
172 r.Equal("", vsc.String())
173
174 vsc1 := NewValueSourceChain(
175 &staticValueSource{v: "soup"},
176 &staticValueSource{v: "salad"},
177 &staticValueSource{v: "pumpkins"},
178 )
179 r.Equal("soup,salad,pumpkins", vsc1.String())
180 })
181 }
182
183 type staticValueSource struct {
184 v string
185 }
186
187 func (svs *staticValueSource) GoString() string {
188 return fmt.Sprintf("&staticValueSource{v:%[1]q}", svs.v)
189 }
190 func (svs *staticValueSource) String() string { return svs.v }
191 func (svs *staticValueSource) Lookup() (string, bool) { return svs.v, true }
192
193 func TestMapValueSource(t *testing.T) {
194 tests := []struct {
195 name string
196 m map[any]any
197 key string
198 val string
199 found bool
200 }{
201 {
202 name: "No map no key",
203 },
204 {
205 name: "No map with key",
206 key: "foo",
207 },
208 {
209 name: "Empty map no key",
210 m: map[any]any{},
211 },
212 {
213 name: "Empty map with key",
214 key: "foo",
215 m: map[any]any{},
216 },
217 {
218 name: "Level 1 no key",
219 key: ".foob",
220 m: map[any]any{
221 "foo": 10,
222 },
223 },
224 {
225 name: "Level 1",
226 key: "foobar",
227 m: map[any]any{
228 "foobar": 10,
229 },
230 val: "10",
231 found: true,
232 },
233 {
234 name: "Level 2",
235 key: "foo.bar",
236 m: map[any]any{
237 "foo": map[any]any{
238 "bar": 10,
239 },
240 },
241 val: "10",
242 found: true,
243 },
244 {
245 name: "Level 2 invalid key",
246 key: "foo.bar1",
247 m: map[any]any{
248 "foo": map[any]any{
249 "bar": "10",
250 },
251 },
252 },
253 {
254 name: "Level 2 string map type",
255 key: "foo.bar1",
256 m: map[any]any{
257 "foo": map[string]any{
258 "bar": "10",
259 },
260 },
261 },
262 {
263 name: "Level 3 no entry",
264 key: "foo.bar.t",
265 m: map[any]any{
266 "foo": map[any]any{
267 "bar": "sss",
268 },
269 },
270 },
271 {
272 name: "Level 3",
273 key: "foo.bar.t",
274 m: map[any]any{
275 "foo": map[any]any{
276 "bar": map[any]any{
277 "t": "sss",
278 },
279 },
280 },
281 val: "sss",
282 found: true,
283 },
284 {
285 name: "Level 3 invalid key",
286 key: "foo.bar.t",
287 m: map[any]any{
288 "foo": map[any]any{
289 "bar": map[any]any{
290 "t1": 10,
291 },
292 },
293 },
294 },
295 {
296 name: "Level 4 no entry",
297 key: "foo.bar.t.gh",
298 m: map[any]any{
299 "foo": map[any]any{
300 "bar": map[any]any{
301 "t1": 10,
302 },
303 },
304 },
305 },
306 {
307 name: "Level 4 slice entry",
308 key: "foo.bar.t.gh",
309 m: map[any]any{
310 "foo": map[any]any{
311 "bar": map[string]any{
312 "t": map[any]any{
313 "gh": []int{10},
314 },
315 },
316 },
317 },
318 val: "[10]",
319 found: true,
320 },
321 }
322
323 for _, test := range tests {
324 t.Run(test.key, func(t *testing.T) {
325 ms := NewMapSource("test", test.m)
326 m := NewMapValueSource(test.key, ms)
327 val, b := m.Lookup()
328 if !test.found {
329 assert.False(t, b)
330 } else {
331 assert.True(t, b)
332 assert.Equal(t, val, test.val)
333 }
334 })
335 }
336 }
337
338 func TestMapValueSourceStringer(t *testing.T) {
339 m := map[any]any{
340 "foo": map[any]any{
341 "bar": 10,
342 },
343 }
344 mvs := NewMapValueSource("bar", NewMapSource("test", m))
345
346 assert.Equal(t, `&mapValueSource{key:"bar", src:&mapSource{name:"test"}}`, mvs.GoString())
347 assert.Equal(t, `key "bar" from map source "test"`, mvs.String())
348 }