(t *testing.T)
| 581 | } |
| 582 | |
| 583 | func TestFlagNameCompletionInGoWithDesc(t *testing.T) { |
| 584 | rootCmd := &Command{ |
| 585 | Use: "root", |
| 586 | Run: emptyRun, |
| 587 | } |
| 588 | childCmd := &Command{ |
| 589 | Use: "childCmd", |
| 590 | Short: "first command", |
| 591 | Version: "1.2.3", |
| 592 | Run: emptyRun, |
| 593 | } |
| 594 | rootCmd.AddCommand(childCmd) |
| 595 | |
| 596 | rootCmd.Flags().IntP("first", "f", -1, "first flag\nlonger description for flag") |
| 597 | rootCmd.PersistentFlags().BoolP("second", "s", false, "second flag") |
| 598 | childCmd.Flags().String("subFlag", "", "sub flag") |
| 599 | |
| 600 | // Test that flag names are not shown if the user has not given the '-' prefix |
| 601 | output, err := executeCommand(rootCmd, ShellCompRequestCmd, "") |
| 602 | if err != nil { |
| 603 | t.Errorf("Unexpected error: %v", err) |
| 604 | } |
| 605 | |
| 606 | expected := strings.Join([]string{ |
| 607 | "childCmd\tfirst command", |
| 608 | "completion\tGenerate the autocompletion script for the specified shell", |
| 609 | "help\tHelp about any command", |
| 610 | ":4", |
| 611 | "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") |
| 612 | |
| 613 | if output != expected { |
| 614 | t.Errorf("expected: %q, got: %q", expected, output) |
| 615 | } |
| 616 | |
| 617 | // Test that flag names are completed |
| 618 | output, err = executeCommand(rootCmd, ShellCompRequestCmd, "-") |
| 619 | if err != nil { |
| 620 | t.Errorf("Unexpected error: %v", err) |
| 621 | } |
| 622 | |
| 623 | expected = strings.Join([]string{ |
| 624 | "--first\tfirst flag", |
| 625 | "-f\tfirst flag", |
| 626 | "--help\thelp for root", |
| 627 | "-h\thelp for root", |
| 628 | "--second\tsecond flag", |
| 629 | "-s\tsecond flag", |
| 630 | ":4", |
| 631 | "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") |
| 632 | |
| 633 | if output != expected { |
| 634 | t.Errorf("expected: %q, got: %q", expected, output) |
| 635 | } |
| 636 | |
| 637 | // Test that flag names are completed when a prefix is given |
| 638 | output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--f") |
| 639 | if err != nil { |
| 640 | t.Errorf("Unexpected error: %v", err) |
nothing calls this directly
no test coverage detected