MCPcopy
hub / github.com/spf13/cobra / TestFlagNameCompletionRepeat

Function TestFlagNameCompletionRepeat

completions_test.go:697–842  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

695}
696
697func TestFlagNameCompletionRepeat(t *testing.T) {
698 rootCmd := &Command{
699 Use: "root",
700 Run: emptyRun,
701 }
702 childCmd := &Command{
703 Use: "childCmd",
704 Short: "first command",
705 Run: emptyRun,
706 }
707 rootCmd.AddCommand(childCmd)
708
709 rootCmd.Flags().IntP("first", "f", -1, "first flag")
710 firstFlag := rootCmd.Flags().Lookup("first")
711 rootCmd.Flags().BoolP("second", "s", false, "second flag")
712 secondFlag := rootCmd.Flags().Lookup("second")
713 rootCmd.Flags().StringArrayP("array", "a", nil, "array flag")
714 arrayFlag := rootCmd.Flags().Lookup("array")
715 rootCmd.Flags().IntSliceP("slice", "l", nil, "slice flag")
716 sliceFlag := rootCmd.Flags().Lookup("slice")
717 rootCmd.Flags().BoolSliceP("bslice", "b", nil, "bool slice flag")
718 bsliceFlag := rootCmd.Flags().Lookup("bslice")
719 rootCmd.Flags().VarP(&customMultiString{}, "multi", "m", "multi string flag")
720 multiFlag := rootCmd.Flags().Lookup("multi")
721
722 // Test that flag names are not repeated unless they are an array or slice
723 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--first", "1", "--")
724 if err != nil {
725 t.Errorf("Unexpected error: %v", err)
726 }
727 // Reset the flag for the next command
728 firstFlag.Changed = false
729
730 expected := strings.Join([]string{
731 "--array",
732 "--bslice",
733 "--help",
734 "--multi",
735 "--second",
736 "--slice",
737 ":4",
738 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
739
740 if output != expected {
741 t.Errorf("expected: %q, got: %q", expected, output)
742 }
743
744 // Test that flag names are not repeated unless they are an array or slice
745 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--first", "1", "--second=false", "--")
746 if err != nil {
747 t.Errorf("Unexpected error: %v", err)
748 }
749 // Reset the flag for the next command
750 firstFlag.Changed = false
751 secondFlag.Changed = false
752
753 expected = strings.Join([]string{
754 "--array",

Callers

nothing calls this directly

Calls 3

AddCommandMethod · 0.95
FlagsMethod · 0.95
executeCommandFunction · 0.85

Tested by

no test coverage detected