(t *testing.T)
| 2894 | } |
| 2895 | |
| 2896 | func 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) |
| 2900 | |
| 2901 | // check against new signature |
| 2902 | var _ CompletionFunc = userComp //nolint:staticcheck // LHS type is needed for this use case |
| 2903 | |
| 2904 | // check Command accepts |
| 2905 | cmd := Command{ |
| 2906 | ValidArgsFunction: userComp, |
| 2907 | } |
| 2908 | |
| 2909 | _ = cmd.RegisterFlagCompletionFunc("foo", userComp) |
| 2910 | }) |
| 2911 | |
| 2912 | t.Run("format with []Completion", func(t *testing.T) { |
| 2913 | var userComp func(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective) |
| 2914 | |
| 2915 | // check against new signature |
| 2916 | var _ CompletionFunc = userComp //nolint:staticcheck // LHS type is needed for this use case |
| 2917 | // check Command accepts |
| 2918 | cmd := Command{ |
| 2919 | ValidArgsFunction: userComp, |
| 2920 | } |
| 2921 | |
| 2922 | _ = cmd.RegisterFlagCompletionFunc("foo", userComp) |
| 2923 | }) |
| 2924 | |
| 2925 | t.Run("format with CompletionFunc", func(t *testing.T) { |
| 2926 | var userComp CompletionFunc |
| 2927 | |
| 2928 | // check helper against old signature |
| 2929 | var _ func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) = userComp //nolint:staticcheck // LHS type is needed for this use case |
| 2930 | var _ func(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective) = userComp //nolint:staticcheck // LHS type is needed for this use case |
| 2931 | |
| 2932 | // check Command accepts |
| 2933 | cmd := Command{ |
| 2934 | ValidArgsFunction: userComp, |
| 2935 | } |
| 2936 | |
| 2937 | _ = cmd.RegisterFlagCompletionFunc("foo", userComp) |
| 2938 | }) |
| 2939 | }) |
| 2940 | |
| 2941 | t.Run("user defined completion helper", func(t *testing.T) { |
| 2942 | t.Run("type helper", func(t *testing.T) { |
| 2943 | // This is a type that may have been defined by the user of the library |
| 2944 | // This replicates the issue https://github.com/docker/cli/issues/5827 |
| 2945 | // https://github.com/docker/cli/blob/b6e7eba4470ecdca460e4b63270fba8179674ad6/cli/command/completion/functions.go#L18 |
| 2946 | type UserCompletionTypeHelper func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) |
| 2947 | |
| 2948 | var userComp UserCompletionTypeHelper |
| 2949 | |
| 2950 | // Here we are validating the existing type validates the CompletionFunc type |
| 2951 | var _ CompletionFunc = userComp |
| 2952 | |
| 2953 | cmd := Command{ |
nothing calls this directly
no test coverage detected