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