Skip to content
Myr

fix CompletionFunc implementation (#2234)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The new type CompletionFunc could lead to a regression. This commit make the new `CompletionFunc` type a type alias instead, in case projects using Cobra have created their own similar type. This commit also adds a test to ensure that the completion function remains backwards-compatible.

authored by ccoVeille · committed by GitHub · parent 5f9c408 · Browse files · 2 files+88 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>

Diff stats

2 files+88 added−2 removed

  • other:+88 added−2 removed

2 files+88 added−2 removed

modified:completions_test.go+87 added-1 removed
2883 unmodified lines
2884 expected = strings.Join([]string{2885 "arg1",2886 "arg2",2887 ":4",2888 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")2888 "Completion ended with directive: ShellCompDirectiveNoFileComp", "",2889 }, "\n")28902891 if output != expected {2892 t.Errorf("expected: %q, got: %q", expected, output)2893 }2894}28952896func TestCompletionFuncCompatibility(t *testing.T) {2897 t.Run("validate signature", func(t *testing.T) {2898 t.Run("format with []string", func(t *testing.T) {2899 var userComp func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)29002901 // check against new signature2902 var _ CompletionFunc = userComp29032904 // check Command accepts2905 cmd := Command{2906 ValidArgsFunction: userComp,2907 }29082909 _ = cmd.RegisterFlagCompletionFunc("foo", userComp)2910 })29112912 t.Run("format with []Completion", func(t *testing.T) {2913 var userComp func(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective)29142915 // check against new signature2916 var _ CompletionFunc = userComp29172918 // check Command accepts2919 cmd := Command{2920 ValidArgsFunction: userComp,2921 }29222923 _ = cmd.RegisterFlagCompletionFunc("foo", userComp)2924 })29252926 t.Run("format with CompletionFunc", func(t *testing.T) {2927 var userComp CompletionFunc29282929 // check helper against old signature2930 var _ func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) = userComp2931 var _ func(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective) = userComp29322933 // check Command accepts2934 cmd := Command{2935 ValidArgsFunction: userComp,2936 }29372938 _ = cmd.RegisterFlagCompletionFunc("foo", userComp)2939 })2940 })29412942 t.Run("user defined completion helper", func(t *testing.T) {2943 t.Run("type helper", func(t *testing.T) {2944 // This is a type that may have been defined by the user of the library2945 // This replicates the issue https://github.com/docker/cli/issues/58272946 // https://github.com/docker/cli/blob/b6e7eba4470ecdca460e4b63270fba8179674ad6/cli/command/completion/functions.go#L182947 type UserCompletionTypeHelper func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)29482949 var userComp UserCompletionTypeHelper29502951 // Here we are validating the existing type validates the CompletionFunc type2952 var _ CompletionFunc = userComp29532954 cmd := Command{2955 ValidArgsFunction: userComp,2956 }29572958 _ = cmd.RegisterFlagCompletionFunc("foo", userComp)2959 })29602961 t.Run("type alias helper", func(t *testing.T) {2962 // This is a type that may have been defined by the user of the library2963 // This replicates the possible fix that was tried here https://github.com/docker/cli/pull/58282964 // https://github.com/docker/cli/blob/ae3d4db9f658259dace9dee515718be7c1b1f517/cli/command/completion/functions.go#L182965 type UserCompletionTypeAliasHelper = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)29662967 var userComp UserCompletionTypeAliasHelper29682969 // Here we are validating the existing type validates the CompletionFunc type2970 var _ CompletionFunc = userComp29712972 cmd := Command{2973 ValidArgsFunction: userComp,2974 }29752976 _ = cmd.RegisterFlagCompletionFunc("foo", userComp)2977 })2978 })2979}29802981func TestFixedCompletions(t *testing.T) {2982 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}2983 choices := []string{"apple", "banana", "orange"}2984 childCmd := &Command{
1034 unmodified lines
modified:completions.go+1 added-1 removed
127 unmodified lines
128// Note: Go type alias is used to provide a more descriptive name in the documentation, but any string can be used.129type Completion = string130131// CompletionFunc is a function that provides completion results.132type CompletionFunc func(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective)132type CompletionFunc = func(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective)133134// CompletionWithDesc returns a [Completion] with a description by using the TAB delimited format.135func CompletionWithDesc(choice string, description string) Completion {136 return choice + "\t" + description
869 unmodified lines