| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | var errBothEnvFlagsAreSet = fmt.Errorf("cannot set both flags `--env` and `--no-env`") |
| 13 | |
| 14 | type boolWithInverseTestCase struct { |
| 15 | args []string |
| 16 | toBeSet bool |
| 17 | value bool |
| 18 | err error |
| 19 | envVars map[string]string |
| 20 | } |
| 21 | |
| 22 | func (tc *boolWithInverseTestCase) Run(t *testing.T, flagWithInverse *BoolWithInverseFlag) error { |
| 23 | cmd := &Command{ |
| 24 | Flags: []Flag{flagWithInverse}, |
| 25 | Action: func(context.Context, *Command) error { return nil }, |
| 26 | } |
| 27 | |
| 28 | for key, val := range tc.envVars { |
| 29 | t.Setenv(key, val) |
| 30 | } |
| 31 | |
| 32 | err := cmd.Run(buildTestContext(t), append([]string{"prog"}, tc.args...)) |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | |
| 37 | if flagWithInverse.IsSet() != tc.toBeSet { |
| 38 | return fmt.Errorf("flag should be set %t, but got %t", tc.toBeSet, flagWithInverse.IsSet()) |
| 39 | } |
| 40 | |
| 41 | if flagWithInverse.Get() != tc.value { |
| 42 | return fmt.Errorf("flag value should be %t, but got %t", tc.value, flagWithInverse.Get()) |
| 43 | } |
| 44 | |
| 45 | return nil |
| 46 | } |
| 47 | |
| 48 | func runBoolWithInverseFlagTests(t *testing.T, newFlagMethod func() *BoolWithInverseFlag, cases []*boolWithInverseTestCase) error { |
| 49 | for _, tc := range cases { |
| 50 | t.Run(strings.Join(tc.args, " ")+fmt.Sprintf("%[1]v %[2]v %[3]v", tc.value, tc.toBeSet, tc.err), func(t *testing.T) { |
| 51 | r := require.New(t) |
| 52 | |
| 53 | fl := newFlagMethod() |
| 54 | |
| 55 | err := tc.Run(t, fl) |
| 56 | if err != nil && tc.err == nil { |
| 57 | r.NoError(err) |
| 58 | } |
| 59 | |
| 60 | if err == nil && tc.err != nil { |
| 61 | r.Error(err) |
| 62 | } |
| 63 | |
| 64 | if err != nil && tc.err != nil { |
| 65 | r.ErrorContains(err, tc.err.Error()) |
| 66 | } |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | return nil |
| 71 | } |
| 72 | |
| 73 | func TestBoolWithInverseBasic(t *testing.T) { |
| 74 | flagMethod := func() *BoolWithInverseFlag { |
| 75 | return &BoolWithInverseFlag{ |
| 76 | Name: "env", |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | testCases := []*boolWithInverseTestCase{ |
| 81 | { |
| 82 | args: []string{"--no-env"}, |
| 83 | toBeSet: true, |
| 84 | value: false, |
| 85 | }, |
| 86 | { |
| 87 | args: []string{"--env"}, |
| 88 | toBeSet: true, |
| 89 | value: true, |
| 90 | }, |
| 91 | { |
| 92 | toBeSet: false, |
| 93 | value: false, |
| 94 | }, |
| 95 | { |
| 96 | args: []string{"--env", "--no-env"}, |
| 97 | err: errBothEnvFlagsAreSet, |
| 98 | }, |
| 99 | } |
| 100 | |
| 101 | err := runBoolWithInverseFlagTests(t, flagMethod, testCases) |
| 102 | if err != nil { |
| 103 | t.Error(err) |
| 104 | return |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestBoolWithInverseAction(t *testing.T) { |
| 109 | err := fmt.Errorf("action called") |
| 110 | flagMethod := func() *BoolWithInverseFlag { |
| 111 | bif := &BoolWithInverseFlag{ |
| 112 | Name: "env", |
| 113 | |
| 114 | // Setting env to the opposite to test flag Action is working as intended |
| 115 | Action: func(_ context.Context, cmd *Command, value bool) error { |
| 116 | return err |
| 117 | }, |
| 118 | } |
| 119 | return bif |
| 120 | } |
| 121 | |
| 122 | testCases := []*boolWithInverseTestCase{ |
| 123 | { |
| 124 | args: []string{"--no-env"}, |
| 125 | toBeSet: true, |
| 126 | value: false, |
| 127 | err: err, |
| 128 | }, |
| 129 | { |
| 130 | args: []string{"--env"}, |
| 131 | toBeSet: true, |
| 132 | value: true, |
| 133 | err: err, |
| 134 | }, |
| 135 | |
| 136 | // This test is not inverse because the flag action is never called |
| 137 | { |
| 138 | toBeSet: false, |
| 139 | value: false, |
| 140 | }, |
| 141 | { |
| 142 | args: []string{"--env", "--no-env"}, |
| 143 | err: errBothEnvFlagsAreSet, |
| 144 | }, |
| 145 | } |
| 146 | |
| 147 | errr := runBoolWithInverseFlagTests(t, flagMethod, testCases) |
| 148 | if errr != nil { |
| 149 | t.Error(errr) |
| 150 | return |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func TestBoolWithInverseAlias(t *testing.T) { |
| 155 | flagMethod := func() *BoolWithInverseFlag { |
| 156 | return &BoolWithInverseFlag{ |
| 157 | Name: "env", |
| 158 | Aliases: []string{"e", "do-env"}, |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | testCases := []*boolWithInverseTestCase{ |
| 163 | { |
| 164 | args: []string{"--no-e"}, |
| 165 | toBeSet: true, |
| 166 | value: false, |
| 167 | }, |
| 168 | { |
| 169 | args: []string{"--e"}, |
| 170 | toBeSet: true, |
| 171 | value: true, |
| 172 | }, |
| 173 | { |
| 174 | toBeSet: false, |
| 175 | value: false, |
| 176 | }, |
| 177 | { |
| 178 | args: []string{"--do-env", "--no-do-env"}, |
| 179 | err: errBothEnvFlagsAreSet, |
| 180 | }, |
| 181 | } |
| 182 | |
| 183 | err := runBoolWithInverseFlagTests(t, flagMethod, testCases) |
| 184 | if err != nil { |
| 185 | t.Error(err) |
| 186 | return |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func TestBoolWithInverseEnvVars(t *testing.T) { |
| 191 | flagMethod := func() *BoolWithInverseFlag { |
| 192 | return &BoolWithInverseFlag{ |
| 193 | Name: "env", |
| 194 | Sources: EnvVars("ENV", "NO-ENV"), |
| 195 | Local: true, |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | testCases := []*boolWithInverseTestCase{ |
| 200 | { |
| 201 | toBeSet: true, |
| 202 | value: false, |
| 203 | envVars: map[string]string{ |
| 204 | "NO-ENV": "false", |
| 205 | }, |
| 206 | }, |
| 207 | { |
| 208 | toBeSet: true, |
| 209 | value: true, |
| 210 | envVars: map[string]string{ |
| 211 | "ENV": "true", |
| 212 | }, |
| 213 | }, |
| 214 | { |
| 215 | toBeSet: true, |
| 216 | value: false, |
| 217 | envVars: map[string]string{ |
| 218 | "ENV": "false", |
| 219 | }, |
| 220 | }, |
| 221 | { |
| 222 | toBeSet: false, |
| 223 | value: false, |
| 224 | }, |
| 225 | // TODO |
| 226 | /*{ |
| 227 | err: errBothEnvFlagsAreSet, |
| 228 | envVars: map[string]string{ |
| 229 | "ENV": "true", |
| 230 | "NO-ENV": "true", |
| 231 | }, |
| 232 | },*/ |
| 233 | { |
| 234 | err: fmt.Errorf("could not parse \"true_env\" as bool value from environment variable \"ENV\" for flag env: parse error"), |
| 235 | envVars: map[string]string{ |
| 236 | "ENV": "true_env", |
| 237 | }, |
| 238 | }, |
| 239 | { |
| 240 | err: fmt.Errorf("could not parse \"false_env\" as bool value from environment variable \"NO-ENV\" for flag env: parse error"), |
| 241 | envVars: map[string]string{ |
| 242 | "NO-ENV": "false_env", |
| 243 | }, |
| 244 | }, |
| 245 | } |
| 246 | |
| 247 | err := runBoolWithInverseFlagTests(t, flagMethod, testCases) |
| 248 | if err != nil { |
| 249 | t.Error(err) |
| 250 | return |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | func TestBoolWithInverseWithPrefix(t *testing.T) { |
| 255 | flagMethod := func() *BoolWithInverseFlag { |
| 256 | return &BoolWithInverseFlag{ |
| 257 | Name: "env", |
| 258 | InversePrefix: "without-", |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | testCases := []*boolWithInverseTestCase{ |
| 263 | { |
| 264 | args: []string{"--without-env"}, |
| 265 | toBeSet: true, |
| 266 | value: false, |
| 267 | }, |
| 268 | { |
| 269 | args: []string{"--env"}, |
| 270 | toBeSet: true, |
| 271 | value: true, |
| 272 | }, |
| 273 | { |
| 274 | toBeSet: false, |
| 275 | value: false, |
| 276 | }, |
| 277 | { |
| 278 | args: []string{"--env", "--without-env"}, |
| 279 | err: fmt.Errorf("cannot set both flags `--env` and `--without-env`"), |
| 280 | }, |
| 281 | { |
| 282 | args: []string{"--without-env", "--env"}, |
| 283 | err: fmt.Errorf("cannot set both flags `--env` and `--without-env`"), |
| 284 | }, |
| 285 | } |
| 286 | |
| 287 | err := runBoolWithInverseFlagTests(t, flagMethod, testCases) |
| 288 | if err != nil { |
| 289 | t.Error(err) |
| 290 | return |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | func TestBoolWithInverseRequired(t *testing.T) { |
| 295 | flagMethod := func() *BoolWithInverseFlag { |
| 296 | return &BoolWithInverseFlag{ |
| 297 | Name: "env", |
| 298 | Required: true, |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | testCases := []*boolWithInverseTestCase{ |
| 303 | { |
| 304 | args: []string{"--no-env"}, |
| 305 | toBeSet: true, |
| 306 | value: false, |
| 307 | }, |
| 308 | { |
| 309 | args: []string{"--env"}, |
| 310 | toBeSet: true, |
| 311 | value: true, |
| 312 | }, |
| 313 | { |
| 314 | args: []string{"--env", "--no-env"}, |
| 315 | err: errBothEnvFlagsAreSet, |
| 316 | }, |
| 317 | } |
| 318 | |
| 319 | err := runBoolWithInverseFlagTests(t, flagMethod, testCases) |
| 320 | if err != nil { |
| 321 | t.Error(err) |
| 322 | return |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | func TestBoolWithInverseNames(t *testing.T) { |
| 327 | flag := &BoolWithInverseFlag{ |
| 328 | Name: "env", |
| 329 | Required: true, |
| 330 | } |
| 331 | names := flag.Names() |
| 332 | |
| 333 | require.Len(t, names, 2) |
| 334 | require.Equal(t, "env", names[0], "expected first name to be `env`") |
| 335 | require.Equal(t, "no-env", names[1], "expected first name to be `no-env`") |
| 336 | |
| 337 | var d DocGenerationFlag = flag |
| 338 | require.Equal(t, "bool", d.TypeName()) |
| 339 | } |
| 340 | |
| 341 | func TestBoolWithInverseString(t *testing.T) { |
| 342 | tcs := []struct { |
| 343 | testName string |
| 344 | flagName string |
| 345 | required bool |
| 346 | usage string |
| 347 | inversePrefix string |
| 348 | aliases []string |
| 349 | expected string |
| 350 | }{ |
| 351 | { |
| 352 | testName: "empty inverse prefix no flag", |
| 353 | flagName: "", |
| 354 | required: true, |
| 355 | expected: "--[no-]\t", |
| 356 | }, |
| 357 | { |
| 358 | testName: "single-char flag name", |
| 359 | flagName: "e", |
| 360 | required: true, |
| 361 | expected: "-[no-]e\t", |
| 362 | }, |
| 363 | { |
| 364 | testName: "multi-char flag name", |
| 365 | flagName: "env", |
| 366 | required: true, |
| 367 | expected: "--[no-]env\t", |
| 368 | }, |
| 369 | { |
| 370 | testName: "required with usage", |
| 371 | flagName: "env", |
| 372 | required: true, |
| 373 | usage: "env usage", |
| 374 | expected: "--[no-]env\tenv usage", |
| 375 | }, |
| 376 | { |
| 377 | testName: "required without usage", |
| 378 | flagName: "env", |
| 379 | required: true, |
| 380 | expected: "--[no-]env\t", |
| 381 | }, |
| 382 | { |
| 383 | testName: "not required with default usage", |
| 384 | flagName: "env", |
| 385 | required: false, |
| 386 | expected: "--[no-]env\t(default: false)", |
| 387 | }, |
| 388 | { |
| 389 | testName: "custom inverse prefix", |
| 390 | flagName: "env", |
| 391 | required: true, |
| 392 | inversePrefix: "nope-", |
| 393 | expected: "--[nope-]env\t", |
| 394 | }, |
| 395 | { |
| 396 | testName: "empty inverse prefix", |
| 397 | flagName: "env", |
| 398 | required: true, |
| 399 | expected: "--[no-]env\t", |
| 400 | }, |
| 401 | { |
| 402 | testName: "short alias", |
| 403 | flagName: "color", |
| 404 | required: false, |
| 405 | aliases: []string{"c"}, |
| 406 | expected: "--[no-]color, -c\t(default: false)", |
| 407 | }, |
| 408 | { |
| 409 | testName: "long alias", |
| 410 | flagName: "color", |
| 411 | required: false, |
| 412 | aliases: []string{"colour"}, |
| 413 | expected: "--[no-]color, --colour\t(default: false)", |
| 414 | }, |
| 415 | { |
| 416 | testName: "multiple aliases", |
| 417 | flagName: "color", |
| 418 | required: false, |
| 419 | aliases: []string{"c", "colour"}, |
| 420 | expected: "--[no-]color, -c, --colour\t(default: false)", |
| 421 | }, |
| 422 | } |
| 423 | |
| 424 | for _, tc := range tcs { |
| 425 | t.Run(tc.testName, func(t *testing.T) { |
| 426 | flag := &BoolWithInverseFlag{ |
| 427 | Name: tc.flagName, |
| 428 | Usage: tc.usage, |
| 429 | Required: tc.required, |
| 430 | InversePrefix: tc.inversePrefix, |
| 431 | Aliases: tc.aliases, |
| 432 | } |
| 433 | |
| 434 | require.Equal(t, tc.expected, flag.String()) |
| 435 | }) |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | func TestBoolWithInverseDestination(t *testing.T) { |
| 440 | destination := new(bool) |
| 441 | count := new(int) |
| 442 | |
| 443 | flagMethod := func() *BoolWithInverseFlag { |
| 444 | return &BoolWithInverseFlag{ |
| 445 | Name: "env", |
| 446 | Destination: destination, |
| 447 | Config: BoolConfig{ |
| 448 | Count: count, |
| 449 | }, |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | checkAndReset := func(expectedCount int, expectedValue bool) error { |
| 454 | if *count != expectedCount { |
| 455 | return fmt.Errorf("expected count to be %d, got %d", expectedCount, *count) |
| 456 | } |
| 457 | |
| 458 | if *destination != expectedValue { |
| 459 | return fmt.Errorf("expected destination to be %t, got %t", expectedValue, *destination) |
| 460 | } |
| 461 | |
| 462 | *count = 0 |
| 463 | *destination = false |
| 464 | |
| 465 | return nil |
| 466 | } |
| 467 | |
| 468 | err := (&boolWithInverseTestCase{ |
| 469 | args: []string{"--env"}, |
| 470 | toBeSet: true, |
| 471 | value: true, |
| 472 | }).Run(t, flagMethod()) |
| 473 | if err != nil { |
| 474 | t.Error(err) |
| 475 | return |
| 476 | } |
| 477 | |
| 478 | err = checkAndReset(1, true) |
| 479 | if err != nil { |
| 480 | t.Error(err) |
| 481 | return |
| 482 | } |
| 483 | |
| 484 | err = (&boolWithInverseTestCase{ |
| 485 | args: []string{"--no-env"}, |
| 486 | toBeSet: true, |
| 487 | value: false, |
| 488 | }).Run(t, flagMethod()) |
| 489 | if err != nil { |
| 490 | t.Error(err) |
| 491 | return |
| 492 | } |
| 493 | |
| 494 | err = checkAndReset(1, false) |
| 495 | if err != nil { |
| 496 | t.Error(err) |
| 497 | return |
| 498 | } |
| 499 | |
| 500 | err = (&boolWithInverseTestCase{ |
| 501 | args: []string{}, |
| 502 | toBeSet: false, |
| 503 | value: false, |
| 504 | }).Run(t, flagMethod()) |
| 505 | if err != nil { |
| 506 | t.Error(err) |
| 507 | return |
| 508 | } |
| 509 | |
| 510 | err = checkAndReset(0, false) |
| 511 | if err != nil { |
| 512 | t.Error(err) |
| 513 | return |
| 514 | } |
| 515 | |
| 516 | f := flagMethod() |
| 517 | f.Value = true |
| 518 | err = (&boolWithInverseTestCase{ |
| 519 | args: []string{}, |
| 520 | toBeSet: false, |
| 521 | value: true, |
| 522 | }).Run(t, f) |
| 523 | if err != nil { |
| 524 | t.Error(err) |
| 525 | return |
| 526 | } |
| 527 | |
| 528 | err = checkAndReset(0, true) |
| 529 | if err != nil { |
| 530 | t.Error(err) |
| 531 | return |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | func TestBoolWithInverseFlag_SatisfiesRequiredFlagInterface(t *testing.T) { |
| 536 | var f RequiredFlag = &BoolWithInverseFlag{} |
| 537 | |
| 538 | _ = f.IsRequired() |
| 539 | } |
| 540 | |
| 541 | func TestBoolWithInverseFlag_SatisfiesVisibleFlagInterface(t *testing.T) { |
| 542 | var f VisibleFlag = &BoolWithInverseFlag{} |
| 543 | |
| 544 | _ = f.IsVisible() |
| 545 | } |
| 546 | |
| 547 | // TestBoolWithInverseFlagStringNoPanicWithNoTabStringer is a regression test for |
| 548 | // https://github.com/urfave/cli/issues/2303. |
| 549 | // BoolWithInverseFlag.String() panicked with "slice bounds out of range [-1:]" |
| 550 | // when the FlagStringer returned a string without a tab character. |
| 551 | func TestBoolWithInverseFlagStringNoPanicWithNoTabStringer(t *testing.T) { |
| 552 | orig := FlagStringer |
| 553 | defer func() { FlagStringer = orig }() |
| 554 | |
| 555 | FlagStringer = func(f Flag) string { |
| 556 | return "no tab here" |
| 557 | } |
| 558 | |
| 559 | flag := &BoolWithInverseFlag{ |
| 560 | Name: "verbose", |
| 561 | } |
| 562 | |
| 563 | // Must not panic. |
| 564 | got := flag.String() |
| 565 | if !strings.Contains(got, "verbose") { |
| 566 | t.Errorf("expected String() to contain the flag name, got %q", got) |
| 567 | } |
| 568 | } |