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