github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/cli/altsrc/yaml_command_test.go (about) 1 // Disabling building of yaml 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 6 package altsrc 7 8 import ( 9 "flag" 10 "io/ioutil" 11 "os" 12 "testing" 13 14 "github.com/insionng/yougam/libraries/cli" 15 ) 16 17 func TestCommandYamlFileTest(t *testing.T) { 18 app := cli.NewApp() 19 set := flag.NewFlagSet("test", 0) 20 ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) 21 defer os.Remove("current.yaml") 22 test := []string{"test-cmd", "--load", "current.yaml"} 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) { 33 val := c.Int("test") 34 expect(t, val, 15) 35 }, 36 Flags: []cli.Flag{ 37 NewIntFlag(cli.IntFlag{Name: "test"}), 38 cli.StringFlag{Name: "load"}}, 39 } 40 command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) 41 err := command.Run(c) 42 43 expect(t, err, nil) 44 } 45 46 func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) { 47 app := cli.NewApp() 48 set := flag.NewFlagSet("test", 0) 49 ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) 50 defer os.Remove("current.yaml") 51 52 os.Setenv("THE_TEST", "10") 53 defer os.Setenv("THE_TEST", "") 54 test := []string{"test-cmd", "--load", "current.yaml"} 55 set.Parse(test) 56 57 c := cli.NewContext(app, set, nil) 58 59 command := &cli.Command{ 60 Name: "test-cmd", 61 Aliases: []string{"tc"}, 62 Usage: "this is for testing", 63 Description: "testing", 64 Action: func(c *cli.Context) { 65 val := c.Int("test") 66 expect(t, val, 10) 67 }, 68 Flags: []cli.Flag{ 69 NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}), 70 cli.StringFlag{Name: "load"}}, 71 } 72 command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) 73 74 err := command.Run(c) 75 76 expect(t, err, nil) 77 } 78 79 func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) { 80 app := cli.NewApp() 81 set := flag.NewFlagSet("test", 0) 82 ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) 83 defer os.Remove("current.yaml") 84 85 test := []string{"test-cmd", "--load", "current.yaml", "--test", "7"} 86 set.Parse(test) 87 88 c := cli.NewContext(app, set, nil) 89 90 command := &cli.Command{ 91 Name: "test-cmd", 92 Aliases: []string{"tc"}, 93 Usage: "this is for testing", 94 Description: "testing", 95 Action: func(c *cli.Context) { 96 val := c.Int("test") 97 expect(t, val, 7) 98 }, 99 Flags: []cli.Flag{ 100 NewIntFlag(cli.IntFlag{Name: "test"}), 101 cli.StringFlag{Name: "load"}}, 102 } 103 command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) 104 105 err := command.Run(c) 106 107 expect(t, err, nil) 108 } 109 110 func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) { 111 app := cli.NewApp() 112 set := flag.NewFlagSet("test", 0) 113 ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) 114 defer os.Remove("current.yaml") 115 116 test := []string{"test-cmd", "--load", "current.yaml"} 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) { 127 val := c.Int("test") 128 expect(t, val, 15) 129 }, 130 Flags: []cli.Flag{ 131 NewIntFlag(cli.IntFlag{Name: "test", Value: 7}), 132 cli.StringFlag{Name: "load"}}, 133 } 134 command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) 135 136 err := command.Run(c) 137 138 expect(t, err, nil) 139 } 140 141 func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T) { 142 app := cli.NewApp() 143 set := flag.NewFlagSet("test", 0) 144 ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) 145 defer os.Remove("current.yaml") 146 147 os.Setenv("THE_TEST", "11") 148 defer os.Setenv("THE_TEST", "") 149 150 test := []string{"test-cmd", "--load", "current.yaml"} 151 set.Parse(test) 152 153 c := cli.NewContext(app, set, nil) 154 155 command := &cli.Command{ 156 Name: "test-cmd", 157 Aliases: []string{"tc"}, 158 Usage: "this is for testing", 159 Description: "testing", 160 Action: func(c *cli.Context) { 161 val := c.Int("test") 162 expect(t, val, 11) 163 }, 164 Flags: []cli.Flag{ 165 NewIntFlag(cli.IntFlag{Name: "test", Value: 7, EnvVar: "THE_TEST"}), 166 cli.StringFlag{Name: "load"}}, 167 } 168 command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) 169 err := command.Run(c) 170 171 expect(t, err, nil) 172 }