1 package altsrc
2
3 import (
4 "flag"
5 "os"
6 "testing"
7
8 "github.com/urfave/cli/v2"
9 )
10
11 const (
12 fileName = "current.json"
13 simpleJSON = `{"test": 15, "testb": false}`
14 nestedJSON = `{"top": {"test": 15}}`
15 )
16
17 func TestCommandJSONFileTest(t *testing.T) {
18 cleanup := writeTempFile(t, fileName, simpleJSON)
19 defer cleanup()
20
21 app := &cli.App{}
22 set := flag.NewFlagSet("test", 0)
23 test := []string{"test-cmd", "--load", fileName}
24 _ = set.Parse(test)
25
26 c := cli.NewContext(app, set, nil)
27
28 command := &cli.Command{
29 Name: "test-cmd",
30 Aliases: []string{"tc"},
31 Usage: "this is for testing",
32 Description: "testing",
33 Action: func(c *cli.Context) error {
34 val := c.Int("test")
35 expect(t, val, 15)
36
37 valb := c.Bool("testb")
38 expect(t, valb, false)
39 return nil
40 },
41 Flags: []cli.Flag{
42 NewIntFlag(&cli.IntFlag{Name: "test"}),
43 &cli.StringFlag{Name: "load"},
44 NewBoolFlag(&cli.BoolFlag{Name: "testb", Value: true}),
45 },
46 }
47 command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
48 err := command.Run(c, test...)
49
50 expect(t, err, nil)
51 }
52
53 func TestCommandJSONFileTestGlobalEnvVarWins(t *testing.T) {
54 cleanup := writeTempFile(t, fileName, simpleJSON)
55 defer cleanup()
56
57 app := &cli.App{}
58 set := flag.NewFlagSet("test", 0)
59 _ = os.Setenv("THE_TEST", "10")
60 defer os.Setenv("THE_TEST", "")
61
62 test := []string{"test-cmd", "--load", fileName}
63 _ = set.Parse(test)
64
65 c := cli.NewContext(app, set, nil)
66
67 command := &cli.Command{
68 Name: "test-cmd",
69 Aliases: []string{"tc"},
70 Usage: "this is for testing",
71 Description: "testing",
72 Action: func(c *cli.Context) error {
73 val := c.Int("test")
74 expect(t, val, 10)
75 return nil
76 },
77 Flags: []cli.Flag{
78 NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"THE_TEST"}}),
79 &cli.StringFlag{Name: "load"}},
80 }
81 command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
82
83 err := command.Run(c, test...)
84
85 expect(t, err, nil)
86 }
87
88 func TestCommandJSONFileTestGlobalEnvVarWinsNested(t *testing.T) {
89 cleanup := writeTempFile(t, fileName, nestedJSON)
90 defer cleanup()
91
92 app := &cli.App{}
93 set := flag.NewFlagSet("test", 0)
94 _ = os.Setenv("THE_TEST", "10")
95 defer os.Setenv("THE_TEST", "")
96
97 test := []string{"test-cmd", "--load", fileName}
98 _ = set.Parse(test)
99
100 c := cli.NewContext(app, set, nil)
101
102 command := &cli.Command{
103 Name: "test-cmd",
104 Aliases: []string{"tc"},
105 Usage: "this is for testing",
106 Description: "testing",
107 Action: func(c *cli.Context) error {
108 val := c.Int("top.test")
109 expect(t, val, 10)
110 return nil
111 },
112 Flags: []cli.Flag{
113 NewIntFlag(&cli.IntFlag{Name: "top.test", EnvVars: []string{"THE_TEST"}}),
114 &cli.StringFlag{Name: "load"}},
115 }
116 command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
117
118 err := command.Run(c, test...)
119
120 expect(t, err, nil)
121 }
122
123 func TestCommandJSONFileTestSpecifiedFlagWins(t *testing.T) {
124 cleanup := writeTempFile(t, fileName, simpleJSON)
125 defer cleanup()
126
127 app := &cli.App{}
128 set := flag.NewFlagSet("test", 0)
129 test := []string{"test-cmd", "--load", fileName, "--test", "7"}
130 _ = set.Parse(test)
131
132 c := cli.NewContext(app, set, nil)
133
134 command := &cli.Command{
135 Name: "test-cmd",
136 Aliases: []string{"tc"},
137 Usage: "this is for testing",
138 Description: "testing",
139 Action: func(c *cli.Context) error {
140 val := c.Int("test")
141 expect(t, val, 7)
142 return nil
143 },
144 Flags: []cli.Flag{
145 NewIntFlag(&cli.IntFlag{Name: "test"}),
146 &cli.StringFlag{Name: "load"}},
147 }
148 command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
149
150 err := command.Run(c, test...)
151
152 expect(t, err, nil)
153 }
154
155 func TestCommandJSONFileTestSpecifiedFlagWinsNested(t *testing.T) {
156 cleanup := writeTempFile(t, fileName, nestedJSON)
157 defer cleanup()
158
159 app := &cli.App{}
160 set := flag.NewFlagSet("test", 0)
161 test := []string{"test-cmd", "--load", fileName, "--top.test", "7"}
162 _ = set.Parse(test)
163
164 c := cli.NewContext(app, set, nil)
165
166 command := &cli.Command{
167 Name: "test-cmd",
168 Aliases: []string{"tc"},
169 Usage: "this is for testing",
170 Description: "testing",
171 Action: func(c *cli.Context) error {
172 val := c.Int("top.test")
173 expect(t, val, 7)
174 return nil
175 },
176 Flags: []cli.Flag{
177 NewIntFlag(&cli.IntFlag{Name: "top.test"}),
178 &cli.StringFlag{Name: "load"}},
179 }
180 command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
181
182 err := command.Run(c, test...)
183
184 expect(t, err, nil)
185 }
186
187 func TestCommandJSONFileTestDefaultValueFileWins(t *testing.T) {
188 cleanup := writeTempFile(t, fileName, simpleJSON)
189 defer cleanup()
190
191 app := &cli.App{}
192 set := flag.NewFlagSet("test", 0)
193 test := []string{"test-cmd", "--load", fileName}
194 _ = set.Parse(test)
195
196 c := cli.NewContext(app, set, nil)
197
198 command := &cli.Command{
199 Name: "test-cmd",
200 Aliases: []string{"tc"},
201 Usage: "this is for testing",
202 Description: "testing",
203 Action: func(c *cli.Context) error {
204 val := c.Int("test")
205 expect(t, val, 15)
206 return nil
207 },
208 Flags: []cli.Flag{
209 NewIntFlag(&cli.IntFlag{Name: "test", Value: 7}),
210 &cli.StringFlag{Name: "load"}},
211 }
212 command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
213
214 err := command.Run(c, test...)
215
216 expect(t, err, nil)
217 }
218
219 func TestCommandJSONFileTestDefaultValueFileWinsNested(t *testing.T) {
220 cleanup := writeTempFile(t, fileName, nestedJSON)
221 defer cleanup()
222
223 app := &cli.App{}
224 set := flag.NewFlagSet("test", 0)
225 test := []string{"test-cmd", "--load", fileName}
226 _ = set.Parse(test)
227
228 c := cli.NewContext(app, set, nil)
229
230 command := &cli.Command{
231 Name: "test-cmd",
232 Aliases: []string{"tc"},
233 Usage: "this is for testing",
234 Description: "testing",
235 Action: func(c *cli.Context) error {
236 val := c.Int("top.test")
237 expect(t, val, 15)
238 return nil
239 },
240 Flags: []cli.Flag{
241 NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7}),
242 &cli.StringFlag{Name: "load"}},
243 }
244 command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
245
246 err := command.Run(c, test...)
247
248 expect(t, err, nil)
249 }
250
251 func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWins(t *testing.T) {
252 cleanup := writeTempFile(t, fileName, simpleJSON)
253 defer cleanup()
254
255 app := &cli.App{}
256 set := flag.NewFlagSet("test", 0)
257 _ = os.Setenv("THE_TEST", "11")
258 defer os.Setenv("THE_TEST", "")
259
260 test := []string{"test-cmd", "--load", fileName}
261 _ = set.Parse(test)
262
263 c := cli.NewContext(app, set, nil)
264
265 command := &cli.Command{
266 Name: "test-cmd",
267 Aliases: []string{"tc"},
268 Usage: "this is for testing",
269 Description: "testing",
270 Action: func(c *cli.Context) error {
271 val := c.Int("test")
272 expect(t, val, 11)
273 return nil
274 },
275 Flags: []cli.Flag{
276 NewIntFlag(&cli.IntFlag{Name: "test", Value: 7, EnvVars: []string{"THE_TEST"}}),
277 &cli.StringFlag{Name: "load"}},
278 }
279 command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
280 err := command.Run(c, test...)
281
282 expect(t, err, nil)
283 }
284
285 func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWinsNested(t *testing.T) {
286 cleanup := writeTempFile(t, fileName, nestedJSON)
287 defer cleanup()
288
289 app := &cli.App{}
290 set := flag.NewFlagSet("test", 0)
291 _ = os.Setenv("THE_TEST", "11")
292 defer os.Setenv("THE_TEST", "")
293
294 test := []string{"test-cmd", "--load", fileName}
295 _ = set.Parse(test)
296
297 c := cli.NewContext(app, set, nil)
298
299 command := &cli.Command{
300 Name: "test-cmd",
301 Aliases: []string{"tc"},
302 Usage: "this is for testing",
303 Description: "testing",
304 Action: func(c *cli.Context) error {
305 val := c.Int("top.test")
306 expect(t, val, 11)
307 return nil
308 },
309 Flags: []cli.Flag{
310 NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7, EnvVars: []string{"THE_TEST"}}),
311 &cli.StringFlag{Name: "load"}},
312 }
313 command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
314 err := command.Run(c, test...)
315
316 expect(t, err, nil)
317 }
318
319 func writeTempFile(t *testing.T, name string, content string) func() {
320 if err := os.WriteFile(name, []byte(content), 0666); err != nil {
321 t.Fatalf("cannot write %q: %v", name, err)
322 }
323 return func() {
324 if err := os.Remove(name); err != nil {
325 t.Errorf("cannot remove %q: %v", name, err)
326 }
327 }
328 }
329
View as plain text