2015-12-09 17:15:46 +00:00
|
|
|
// Disabling building of yaml support in cases where golang is 1.0 or 1.1
|
|
|
|
// as the encoding library is not implemented or supported.
|
|
|
|
|
2016-03-19 18:06:53 +00:00
|
|
|
// +build go1.2
|
2015-12-09 17:15:46 +00:00
|
|
|
|
|
|
|
package altsrc
|
2015-12-06 23:43:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2016-05-22 22:30:07 +00:00
|
|
|
"github.com/urfave/cli"
|
2015-12-06 23:43:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommandYamlFileTest(t *testing.T) {
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
|
|
|
defer os.Remove("current.yaml")
|
|
|
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
|
|
|
set.Parse(test)
|
|
|
|
|
|
|
|
c := cli.NewContext(app, set, nil)
|
|
|
|
|
|
|
|
command := &cli.Command{
|
|
|
|
Name: "test-cmd",
|
|
|
|
Aliases: []string{"tc"},
|
|
|
|
Usage: "this is for testing",
|
|
|
|
Description: "testing",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
val := c.Int("test")
|
|
|
|
expect(t, val, 15)
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2016-05-22 19:20:52 +00:00
|
|
|
NewIntFlag(&cli.IntFlag{Name: "test"}),
|
|
|
|
&cli.StringFlag{Name: "load"}},
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
2016-04-27 13:12:34 +00:00
|
|
|
err := command.Run(c)
|
2015-12-06 23:43:18 +00:00
|
|
|
|
|
|
|
expect(t, err, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
|
|
|
defer os.Remove("current.yaml")
|
|
|
|
|
|
|
|
os.Setenv("THE_TEST", "10")
|
|
|
|
defer os.Setenv("THE_TEST", "")
|
|
|
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
|
|
|
set.Parse(test)
|
|
|
|
|
|
|
|
c := cli.NewContext(app, set, nil)
|
|
|
|
|
|
|
|
command := &cli.Command{
|
|
|
|
Name: "test-cmd",
|
|
|
|
Aliases: []string{"tc"},
|
|
|
|
Usage: "this is for testing",
|
|
|
|
Description: "testing",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
val := c.Int("test")
|
|
|
|
expect(t, val, 10)
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2016-05-22 19:20:52 +00:00
|
|
|
NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"THE_TEST"}}),
|
|
|
|
&cli.StringFlag{Name: "load"}},
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
2015-12-06 23:43:18 +00:00
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := command.Run(c)
|
2015-12-06 23:43:18 +00:00
|
|
|
|
|
|
|
expect(t, err, nil)
|
|
|
|
}
|
|
|
|
|
2016-04-29 16:33:59 +00:00
|
|
|
func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ioutil.WriteFile("current.yaml", []byte(`top:
|
|
|
|
test: 15`), 0666)
|
|
|
|
defer os.Remove("current.yaml")
|
|
|
|
|
|
|
|
os.Setenv("THE_TEST", "10")
|
|
|
|
defer os.Setenv("THE_TEST", "")
|
|
|
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
|
|
|
set.Parse(test)
|
|
|
|
|
|
|
|
c := cli.NewContext(app, set, nil)
|
|
|
|
|
|
|
|
command := &cli.Command{
|
|
|
|
Name: "test-cmd",
|
|
|
|
Aliases: []string{"tc"},
|
|
|
|
Usage: "this is for testing",
|
|
|
|
Description: "testing",
|
2016-05-01 12:36:17 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2016-04-29 16:33:59 +00:00
|
|
|
val := c.Int("top.test")
|
|
|
|
expect(t, val, 10)
|
2016-05-01 12:36:17 +00:00
|
|
|
return nil
|
2016-04-29 16:33:59 +00:00
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2016-05-22 19:20:52 +00:00
|
|
|
NewIntFlag(&cli.IntFlag{Name: "top.test", EnvVars: []string{"THE_TEST"}}),
|
|
|
|
&cli.StringFlag{Name: "load"}},
|
2016-04-29 16:33:59 +00:00
|
|
|
}
|
|
|
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
|
|
|
|
|
|
|
err := command.Run(c)
|
|
|
|
|
|
|
|
expect(t, err, nil)
|
|
|
|
}
|
|
|
|
|
2015-12-06 23:43:18 +00:00
|
|
|
func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
|
|
|
defer os.Remove("current.yaml")
|
|
|
|
|
|
|
|
test := []string{"test-cmd", "--load", "current.yaml", "--test", "7"}
|
|
|
|
set.Parse(test)
|
|
|
|
|
|
|
|
c := cli.NewContext(app, set, nil)
|
|
|
|
|
|
|
|
command := &cli.Command{
|
|
|
|
Name: "test-cmd",
|
|
|
|
Aliases: []string{"tc"},
|
|
|
|
Usage: "this is for testing",
|
|
|
|
Description: "testing",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
val := c.Int("test")
|
|
|
|
expect(t, val, 7)
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2016-05-22 19:20:52 +00:00
|
|
|
NewIntFlag(&cli.IntFlag{Name: "test"}),
|
|
|
|
&cli.StringFlag{Name: "load"}},
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
2015-12-06 23:43:18 +00:00
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := command.Run(c)
|
2015-12-06 23:43:18 +00:00
|
|
|
|
|
|
|
expect(t, err, nil)
|
|
|
|
}
|
|
|
|
|
2016-04-29 16:33:59 +00:00
|
|
|
func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ioutil.WriteFile("current.yaml", []byte(`top:
|
|
|
|
test: 15`), 0666)
|
|
|
|
defer os.Remove("current.yaml")
|
|
|
|
|
|
|
|
test := []string{"test-cmd", "--load", "current.yaml", "--top.test", "7"}
|
|
|
|
set.Parse(test)
|
|
|
|
|
|
|
|
c := cli.NewContext(app, set, nil)
|
|
|
|
|
|
|
|
command := &cli.Command{
|
|
|
|
Name: "test-cmd",
|
|
|
|
Aliases: []string{"tc"},
|
|
|
|
Usage: "this is for testing",
|
|
|
|
Description: "testing",
|
2016-05-01 12:36:17 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2016-04-29 16:33:59 +00:00
|
|
|
val := c.Int("top.test")
|
|
|
|
expect(t, val, 7)
|
2016-05-01 12:36:17 +00:00
|
|
|
return nil
|
2016-04-29 16:33:59 +00:00
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2016-05-22 19:20:52 +00:00
|
|
|
NewIntFlag(&cli.IntFlag{Name: "top.test"}),
|
|
|
|
&cli.StringFlag{Name: "load"}},
|
2016-04-29 16:33:59 +00:00
|
|
|
}
|
|
|
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
|
|
|
|
|
|
|
err := command.Run(c)
|
|
|
|
|
|
|
|
expect(t, err, nil)
|
|
|
|
}
|
|
|
|
|
2015-12-06 23:43:18 +00:00
|
|
|
func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
|
|
|
defer os.Remove("current.yaml")
|
|
|
|
|
|
|
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
|
|
|
set.Parse(test)
|
|
|
|
|
|
|
|
c := cli.NewContext(app, set, nil)
|
|
|
|
|
|
|
|
command := &cli.Command{
|
|
|
|
Name: "test-cmd",
|
|
|
|
Aliases: []string{"tc"},
|
|
|
|
Usage: "this is for testing",
|
|
|
|
Description: "testing",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
val := c.Int("test")
|
|
|
|
expect(t, val, 15)
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2016-05-22 19:20:52 +00:00
|
|
|
NewIntFlag(&cli.IntFlag{Name: "test", Value: 7}),
|
|
|
|
&cli.StringFlag{Name: "load"}},
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
2015-12-06 23:43:18 +00:00
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
err := command.Run(c)
|
2015-12-06 23:43:18 +00:00
|
|
|
|
|
|
|
expect(t, err, nil)
|
|
|
|
}
|
|
|
|
|
2016-04-29 16:33:59 +00:00
|
|
|
func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ioutil.WriteFile("current.yaml", []byte(`top:
|
|
|
|
test: 15`), 0666)
|
|
|
|
defer os.Remove("current.yaml")
|
|
|
|
|
|
|
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
|
|
|
set.Parse(test)
|
|
|
|
|
|
|
|
c := cli.NewContext(app, set, nil)
|
|
|
|
|
|
|
|
command := &cli.Command{
|
|
|
|
Name: "test-cmd",
|
|
|
|
Aliases: []string{"tc"},
|
|
|
|
Usage: "this is for testing",
|
|
|
|
Description: "testing",
|
2016-05-01 12:36:17 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2016-04-29 16:33:59 +00:00
|
|
|
val := c.Int("top.test")
|
|
|
|
expect(t, val, 15)
|
2016-05-01 12:36:17 +00:00
|
|
|
return nil
|
2016-04-29 16:33:59 +00:00
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2016-05-22 19:20:52 +00:00
|
|
|
NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7}),
|
|
|
|
&cli.StringFlag{Name: "load"}},
|
2016-04-29 16:33:59 +00:00
|
|
|
}
|
|
|
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
|
|
|
|
|
|
|
err := command.Run(c)
|
|
|
|
|
|
|
|
expect(t, err, nil)
|
|
|
|
}
|
|
|
|
|
2015-12-06 23:43:18 +00:00
|
|
|
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T) {
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
|
|
|
defer os.Remove("current.yaml")
|
|
|
|
|
|
|
|
os.Setenv("THE_TEST", "11")
|
|
|
|
defer os.Setenv("THE_TEST", "")
|
|
|
|
|
|
|
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
|
|
|
set.Parse(test)
|
|
|
|
|
|
|
|
c := cli.NewContext(app, set, nil)
|
|
|
|
|
|
|
|
command := &cli.Command{
|
|
|
|
Name: "test-cmd",
|
|
|
|
Aliases: []string{"tc"},
|
|
|
|
Usage: "this is for testing",
|
|
|
|
Description: "testing",
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
val := c.Int("test")
|
|
|
|
expect(t, val, 11)
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2016-05-22 19:20:52 +00:00
|
|
|
NewIntFlag(&cli.IntFlag{Name: "test", Value: 7, EnvVars: []string{"THE_TEST"}}),
|
|
|
|
&cli.StringFlag{Name: "load"}},
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
2016-04-27 13:12:34 +00:00
|
|
|
err := command.Run(c)
|
2015-12-06 23:43:18 +00:00
|
|
|
|
|
|
|
expect(t, err, nil)
|
|
|
|
}
|
2016-04-29 16:33:59 +00:00
|
|
|
|
|
|
|
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWinsNested(t *testing.T) {
|
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
ioutil.WriteFile("current.yaml", []byte(`top:
|
|
|
|
test: 15`), 0666)
|
|
|
|
defer os.Remove("current.yaml")
|
|
|
|
|
|
|
|
os.Setenv("THE_TEST", "11")
|
|
|
|
defer os.Setenv("THE_TEST", "")
|
|
|
|
|
|
|
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
|
|
|
set.Parse(test)
|
|
|
|
|
|
|
|
c := cli.NewContext(app, set, nil)
|
|
|
|
|
|
|
|
command := &cli.Command{
|
|
|
|
Name: "test-cmd",
|
|
|
|
Aliases: []string{"tc"},
|
|
|
|
Usage: "this is for testing",
|
|
|
|
Description: "testing",
|
2016-05-01 12:36:17 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2016-04-29 16:33:59 +00:00
|
|
|
val := c.Int("top.test")
|
|
|
|
expect(t, val, 11)
|
2016-05-01 12:36:17 +00:00
|
|
|
return nil
|
2016-04-29 16:33:59 +00:00
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2016-05-22 19:20:52 +00:00
|
|
|
NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7, EnvVars: []string{"THE_TEST"}}),
|
|
|
|
&cli.StringFlag{Name: "load"}},
|
2016-04-29 16:33:59 +00:00
|
|
|
}
|
|
|
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
|
|
|
err := command.Run(c)
|
|
|
|
|
|
|
|
expect(t, err, nil)
|
|
|
|
}
|