github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/flags/flags_test.go (about) 1 package flags_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/flags" 5 . "github.com/onsi/ginkgo" 6 . "github.com/onsi/gomega" 7 ) 8 9 var _ = Describe("Flags", func() { 10 Describe("FlagContext", func() { 11 Describe("Parsing and retriving values", func() { 12 var ( 13 fCtx flags.FlagContext 14 cmdFlagMap map[string]flags.FlagSet 15 ) 16 17 BeforeEach(func() { 18 cmdFlagMap = make(map[string]flags.FlagSet) 19 20 cmdFlagMap["name"] = &flags.StringFlag{Name: "name", ShortName: "n", Usage: "test string flag"} 21 cmdFlagMap["skip"] = &flags.BoolFlag{Name: "skip", Usage: "test bool flag"} 22 cmdFlagMap["instance"] = &flags.IntFlag{Name: "instance", Usage: "test int flag"} 23 cmdFlagMap["float"] = &flags.Float64Flag{Name: "float", Usage: "test float64 flag"} 24 cmdFlagMap["skip2"] = &flags.BoolFlag{Name: "skip2", Usage: "test bool flag"} 25 cmdFlagMap["slice"] = &flags.StringSliceFlag{Name: "slice", Usage: "test stringSlice flag"} 26 27 fCtx = flags.NewFlagContext(cmdFlagMap) 28 }) 29 30 It("accepts flags with either single '-' or double '-' ", func() { 31 err := fCtx.Parse("--name", "blue") 32 Expect(err).NotTo(HaveOccurred()) 33 34 err = fCtx.Parse("-name", "") 35 Expect(err).NotTo(HaveOccurred()) 36 }) 37 38 It("sets a flag with it's full name", func() { 39 err := fCtx.Parse("-name", "blue") 40 Expect(err).NotTo(HaveOccurred()) 41 Expect(fCtx.IsSet("name")).To(BeTrue()) 42 Expect(fCtx.IsSet("n")).To(BeTrue()) 43 Expect(fCtx.String("name")).To(Equal("blue")) 44 Expect(fCtx.String("n")).To(Equal("blue")) 45 }) 46 47 It("sets a flag with it's short name", func() { 48 err := fCtx.Parse("-n", "red") 49 Expect(err).NotTo(HaveOccurred()) 50 Expect(fCtx.IsSet("name")).To(BeTrue()) 51 Expect(fCtx.IsSet("n")).To(BeTrue()) 52 Expect(fCtx.String("name")).To(Equal("red")) 53 Expect(fCtx.String("n")).To(Equal("red")) 54 }) 55 56 It("checks if a flag is defined in the FlagContext", func() { 57 err := fCtx.Parse("-not_defined", "") 58 Expect(err).To(HaveOccurred()) 59 60 err = fCtx.Parse("-name", "blue") 61 Expect(err).NotTo(HaveOccurred()) 62 63 err = fCtx.Parse("--skip", "") 64 Expect(err).NotTo(HaveOccurred()) 65 }) 66 67 It("sets Bool(<flag>) to return value if bool flag is provided with value true/false", func() { 68 err := fCtx.Parse("--skip=false", "-skip2", "true", "-name=johndoe") 69 Expect(err).NotTo(HaveOccurred()) 70 71 Ω(len(fCtx.Args())).To(Equal(0), "Length of Args() should be 0") 72 Ω(fCtx.Bool("skip")).To(Equal(false), "skip should be false") 73 Ω(fCtx.Bool("skip2")).To(Equal(true), "skip2 should be true") 74 Ω(fCtx.Bool("name")).To(Equal(false), "name should be false") 75 Ω(fCtx.String("name")).To(Equal("johndoe"), "name should be johndoe") 76 Expect(fCtx.Bool("non-exisit-flag")).To(Equal(false)) 77 }) 78 79 It("sets Bool(<flag>) to return true if bool flag is provided with invalid value", func() { 80 err := fCtx.Parse("--skip=Not_Valid", "skip2", "FALSE", "-name", "johndoe") 81 Expect(err).NotTo(HaveOccurred()) 82 83 Ω(fCtx.Bool("skip")).To(Equal(true), "skip should be true") 84 Ω(fCtx.Bool("skip2")).To(Equal(false), "skip2 should be false") 85 }) 86 87 It("sets Bool(<flag>) to return true when a bool flag is provided without value", func() { 88 err := fCtx.Parse("--skip", "-name", "johndoe") 89 Expect(err).NotTo(HaveOccurred()) 90 91 Ω(fCtx.Bool("skip")).To(Equal(true), "skip should be true") 92 Ω(fCtx.Bool("name")).To(Equal(false), "name should be false") 93 Expect(fCtx.Bool("non-exisit-flag")).To(Equal(false)) 94 }) 95 96 It("sets String(<flag>) to return provided value when a string flag is provided", func() { 97 err := fCtx.Parse("--skip", "-name", "doe") 98 Expect(err).NotTo(HaveOccurred()) 99 100 Expect(fCtx.String("name")).To(Equal("doe")) 101 Ω(fCtx.Bool("skip")).To(Equal(true), "skip should be true") 102 }) 103 104 It("sets StringSlice(<flag>) to return provided value when a stringSlice flag is provided", func() { 105 err := fCtx.Parse("-slice", "value1", "-slice", "value2") 106 Expect(err).NotTo(HaveOccurred()) 107 108 Ω(fCtx.StringSlice("slice")[0]).To(Equal("value1"), "slice[0] should be 'value1'") 109 Ω(fCtx.StringSlice("slice")[1]).To(Equal("value2"), "slice[1] should be 'value2'") 110 }) 111 112 It("errors when a non-boolean flag is provided without a value", func() { 113 err := fCtx.Parse("-name") 114 Expect(err).To(HaveOccurred()) 115 Expect(err.Error()).To(ContainSubstring("No value provided for flag")) 116 Expect(fCtx.String("name")).To(Equal("")) 117 }) 118 119 It("sets Int(<flag>) to return provided value when a int flag is provided", func() { 120 err := fCtx.Parse("--instance", "10") 121 Expect(err).NotTo(HaveOccurred()) 122 123 Expect(fCtx.Int("instance")).To(Equal(10)) 124 Expect(fCtx.IsSet("instance")).To(Equal(true)) 125 126 Expect(fCtx.Int("non-exist-flag")).To(Equal(0)) 127 Expect(fCtx.IsSet("non-exist-flag")).To(Equal(false)) 128 }) 129 130 It("sets Float64(<flag>) to return provided value when a float64 flag is provided", func() { 131 err := fCtx.Parse("-float", "10.5") 132 Expect(err).NotTo(HaveOccurred()) 133 134 Expect(fCtx.Float64("float")).To(Equal(10.5)) 135 Expect(fCtx.IsSet("float")).To(Equal(true)) 136 137 Expect(fCtx.Float64("non-exist-flag")).To(Equal(float64(0))) 138 Expect(fCtx.IsSet("non-exist-flag")).To(Equal(false)) 139 }) 140 141 It("returns any non-flag arguments in Args()", func() { 142 err := fCtx.Parse("Arg-1", "--instance", "10", "--skip", "Arg-2") 143 Expect(err).NotTo(HaveOccurred()) 144 145 Expect(len(fCtx.Args())).To(Equal(2)) 146 Expect(fCtx.Args()[0]).To(Equal("Arg-1")) 147 Expect(fCtx.Args()[1]).To(Equal("Arg-2")) 148 }) 149 150 It("accepts flag/value in the forms of '-flag=value' and '-flag value'", func() { 151 err := fCtx.Parse("-instance", "10", "--name=foo", "--skip", "Arg-1") 152 Expect(err).NotTo(HaveOccurred()) 153 154 Expect(fCtx.IsSet("instance")).To(Equal(true)) 155 Expect(fCtx.Int("instance")).To(Equal(10)) 156 157 Expect(fCtx.IsSet("name")).To(Equal(true)) 158 Expect(fCtx.String("name")).To(Equal("foo")) 159 160 Expect(fCtx.IsSet("skip")).To(Equal(true)) 161 162 Expect(len(fCtx.Args())).To(Equal(1)) 163 Expect(fCtx.Args()[0]).To(Equal("Arg-1")) 164 }) 165 166 Context("Default Flag Value", func() { 167 168 BeforeEach(func() { 169 cmdFlagMap = make(map[string]flags.FlagSet) 170 171 cmdFlagMap["defaultStringFlag"] = &flags.StringFlag{Name: "defaultStringFlag", Value: "Set by default"} 172 cmdFlagMap["defaultBoolFlag"] = &flags.BoolFlag{Name: "defaultBoolFlag", Value: true} 173 cmdFlagMap["defaultIntFlag"] = &flags.IntFlag{Name: "defaultIntFlag", Value: 100} 174 cmdFlagMap["defaultStringAryFlag"] = &flags.StringSliceFlag{Name: "defaultStringAryFlag", Value: []string{"abc", "def"}} 175 cmdFlagMap["defaultFloat64Flag"] = &flags.Float64Flag{Name: "defaultFloat64Flag", Value: 100.5} 176 cmdFlagMap["noDefaultStringFlag"] = &flags.StringFlag{Name: "noDefaultStringFlag"} 177 178 fCtx = flags.NewFlagContext(cmdFlagMap) 179 }) 180 181 It("sets flag with default value if 'Value' is provided", func() { 182 err := fCtx.Parse() 183 Expect(err).NotTo(HaveOccurred()) 184 185 Expect(fCtx.String("defaultStringFlag")).To(Equal("Set by default")) 186 Expect(fCtx.IsSet("defaultStringFlag")).To(BeTrue()) 187 188 Expect(fCtx.Bool("defaultBoolFlag")).To(BeTrue()) 189 Expect(fCtx.IsSet("defaultBoolFlag")).To(BeTrue()) 190 191 Expect(fCtx.Int("defaultIntFlag")).To(Equal(100)) 192 Expect(fCtx.IsSet("defaultIntFlag")).To(BeTrue()) 193 194 Expect(fCtx.Float64("defaultFloat64Flag")).To(Equal(100.5)) 195 Expect(fCtx.IsSet("defaultFloat64Flag")).To(BeTrue()) 196 197 Expect(fCtx.StringSlice("defaultStringAryFlag")).To(Equal([]string{"abc", "def"})) 198 Expect(fCtx.IsSet("defaultStringAryFlag")).To(BeTrue()) 199 200 Expect(fCtx.String("noDefaultStringFlag")).To(Equal("")) 201 Expect(fCtx.IsSet("noDefaultStringFlag")).To(BeFalse()) 202 }) 203 204 It("overrides default value if argument is provided, except StringSlice Flag", func() { 205 err := fCtx.Parse("-defaultStringFlag=foo", "-defaultBoolFlag=false", "-defaultIntFlag=200", "-defaultStringAryFlag=foo", "-defaultStringAryFlag=bar", "-noDefaultStringFlag=baz") 206 Expect(err).NotTo(HaveOccurred()) 207 208 Expect(fCtx.String("defaultStringFlag")).To(Equal("foo")) 209 Expect(fCtx.IsSet("defaultStringFlag")).To(BeTrue()) 210 211 Expect(fCtx.Bool("defaultBoolFlag")).To(BeFalse()) 212 Expect(fCtx.IsSet("defaultBoolFlag")).To(BeTrue()) 213 214 Expect(fCtx.Int("defaultIntFlag")).To(Equal(200)) 215 Expect(fCtx.IsSet("defaultIntFlag")).To(BeTrue()) 216 217 Expect(fCtx.String("noDefaultStringFlag")).To(Equal("baz")) 218 Expect(fCtx.IsSet("noDefaultStringFlag")).To(BeTrue()) 219 }) 220 221 It("appends argument value to StringSliceFlag to the default values", func() { 222 err := fCtx.Parse("-defaultStringAryFlag=foo", "-defaultStringAryFlag=bar") 223 Expect(err).NotTo(HaveOccurred()) 224 225 Expect(fCtx.StringSlice("defaultStringAryFlag")).To(Equal([]string{"abc", "def", "foo", "bar"})) 226 Expect(fCtx.IsSet("defaultStringAryFlag")).To(BeTrue()) 227 }) 228 229 }) 230 231 Context("SkipFlagParsing", func() { 232 It("skips flag parsing and treats all arguments as values", func() { 233 fCtx.SkipFlagParsing(true) 234 err := fCtx.Parse("value1", "--name", "foo") 235 Expect(err).NotTo(HaveOccurred()) 236 237 Expect(fCtx.IsSet("name")).To(Equal(false)) 238 239 Expect(len(fCtx.Args())).To(Equal(3)) 240 Expect(fCtx.Args()[0]).To(Equal("value1")) 241 Expect(fCtx.Args()[1]).To(Equal("--name")) 242 Expect(fCtx.Args()[2]).To(Equal("foo")) 243 }) 244 }) 245 246 }) 247 248 }) 249 })