1package altsrc 2 3import ( 4 "flag" 5 "io/ioutil" 6 "os" 7 "testing" 8 9 "github.com/schollz/cli/v2" 10) 11 12func TestCommandTomFileTest(t *testing.T) { 13 app := &cli.App{} 14 set := flag.NewFlagSet("test", 0) 15 _ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) 16 defer os.Remove("current.toml") 17 test := []string{"test-cmd", "--load", "current.toml"} 18 _ = set.Parse(test) 19 20 c := cli.NewContext(app, set, nil) 21 22 command := &cli.Command{ 23 Name: "test-cmd", 24 Aliases: []string{"tc"}, 25 Usage: "this is for testing", 26 Description: "testing", 27 Action: func(c *cli.Context) error { 28 val := c.Int("test") 29 expect(t, val, 15) 30 return nil 31 }, 32 Flags: []cli.Flag{ 33 NewIntFlag(&cli.IntFlag{Name: "test"}), 34 &cli.StringFlag{Name: "load"}}, 35 } 36 command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load")) 37 err := command.Run(c) 38 39 expect(t, err, nil) 40} 41 42func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) { 43 app := &cli.App{} 44 set := flag.NewFlagSet("test", 0) 45 _ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) 46 defer os.Remove("current.toml") 47 48 _ = os.Setenv("THE_TEST", "10") 49 defer os.Setenv("THE_TEST", "") 50 test := []string{"test-cmd", "--load", "current.toml"} 51 _ = set.Parse(test) 52 53 c := cli.NewContext(app, set, nil) 54 55 command := &cli.Command{ 56 Name: "test-cmd", 57 Aliases: []string{"tc"}, 58 Usage: "this is for testing", 59 Description: "testing", 60 Action: func(c *cli.Context) error { 61 val := c.Int("test") 62 expect(t, val, 10) 63 return nil 64 }, 65 Flags: []cli.Flag{ 66 NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"THE_TEST"}}), 67 &cli.StringFlag{Name: "load"}}, 68 } 69 command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load")) 70 71 err := command.Run(c) 72 73 expect(t, err, nil) 74} 75 76func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) { 77 app := &cli.App{} 78 set := flag.NewFlagSet("test", 0) 79 _ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666) 80 defer os.Remove("current.toml") 81 82 _ = os.Setenv("THE_TEST", "10") 83 defer os.Setenv("THE_TEST", "") 84 test := []string{"test-cmd", "--load", "current.toml"} 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, NewTomlSourceFromFlagFunc("load")) 104 105 err := command.Run(c) 106 107 expect(t, err, nil) 108} 109 110func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) { 111 app := &cli.App{} 112 set := flag.NewFlagSet("test", 0) 113 _ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) 114 defer os.Remove("current.toml") 115 116 test := []string{"test-cmd", "--load", "current.toml", "--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, NewTomlSourceFromFlagFunc("load")) 136 137 err := command.Run(c) 138 139 expect(t, err, nil) 140} 141 142func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) { 143 app := &cli.App{} 144 set := flag.NewFlagSet("test", 0) 145 _ = ioutil.WriteFile("current.toml", []byte(`[top] 146 test = 15`), 0666) 147 defer os.Remove("current.toml") 148 149 test := []string{"test-cmd", "--load", "current.toml", "--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, NewTomlSourceFromFlagFunc("load")) 169 170 err := command.Run(c) 171 172 expect(t, err, nil) 173} 174 175func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) { 176 app := &cli.App{} 177 set := flag.NewFlagSet("test", 0) 178 _ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) 179 defer os.Remove("current.toml") 180 181 test := []string{"test-cmd", "--load", "current.toml"} 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, NewTomlSourceFromFlagFunc("load")) 201 202 err := command.Run(c) 203 204 expect(t, err, nil) 205} 206 207func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) { 208 app := &cli.App{} 209 set := flag.NewFlagSet("test", 0) 210 _ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666) 211 defer os.Remove("current.toml") 212 213 test := []string{"test-cmd", "--load", "current.toml"} 214 _ = set.Parse(test) 215 216 c := cli.NewContext(app, set, nil) 217 218 command := &cli.Command{ 219 Name: "test-cmd", 220 Aliases: []string{"tc"}, 221 Usage: "this is for testing", 222 Description: "testing", 223 Action: func(c *cli.Context) error { 224 val := c.Int("top.test") 225 expect(t, val, 15) 226 return nil 227 }, 228 Flags: []cli.Flag{ 229 NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7}), 230 &cli.StringFlag{Name: "load"}}, 231 } 232 command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load")) 233 234 err := command.Run(c) 235 236 expect(t, err, nil) 237} 238 239func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T) { 240 app := &cli.App{} 241 set := flag.NewFlagSet("test", 0) 242 _ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) 243 defer os.Remove("current.toml") 244 245 _ = os.Setenv("THE_TEST", "11") 246 defer os.Setenv("THE_TEST", "") 247 248 test := []string{"test-cmd", "--load", "current.toml"} 249 _ = set.Parse(test) 250 251 c := cli.NewContext(app, set, nil) 252 253 command := &cli.Command{ 254 Name: "test-cmd", 255 Aliases: []string{"tc"}, 256 Usage: "this is for testing", 257 Description: "testing", 258 Action: func(c *cli.Context) error { 259 val := c.Int("test") 260 expect(t, val, 11) 261 return nil 262 }, 263 Flags: []cli.Flag{ 264 NewIntFlag(&cli.IntFlag{Name: "test", Value: 7, EnvVars: []string{"THE_TEST"}}), 265 &cli.StringFlag{Name: "load"}}, 266 } 267 command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load")) 268 err := command.Run(c) 269 270 expect(t, err, nil) 271} 272 273func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWinsNested(t *testing.T) { 274 app := &cli.App{} 275 set := flag.NewFlagSet("test", 0) 276 _ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666) 277 defer os.Remove("current.toml") 278 279 _ = os.Setenv("THE_TEST", "11") 280 defer os.Setenv("THE_TEST", "") 281 282 test := []string{"test-cmd", "--load", "current.toml"} 283 _ = set.Parse(test) 284 285 c := cli.NewContext(app, set, nil) 286 287 command := &cli.Command{ 288 Name: "test-cmd", 289 Aliases: []string{"tc"}, 290 Usage: "this is for testing", 291 Description: "testing", 292 Action: func(c *cli.Context) error { 293 val := c.Int("top.test") 294 expect(t, val, 11) 295 return nil 296 }, 297 Flags: []cli.Flag{ 298 NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7, EnvVars: []string{"THE_TEST"}}), 299 &cli.StringFlag{Name: "load"}}, 300 } 301 command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load")) 302 err := command.Run(c) 303 304 expect(t, err, nil) 305} 306