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

Function TestFlagFileExtFilterCompletionInGo

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

Source from the content-addressed store, hash-verified

1036}
1037
1038func TestFlagFileExtFilterCompletionInGo(t *testing.T) {
1039 rootCmd := &Command{
1040 Use: "root",
1041 Run: emptyRun,
1042 }
1043
1044 // No extensions. Should be ignored.
1045 rootCmd.Flags().StringP("file", "f", "", "file flag")
1046 assertNoErr(t, rootCmd.MarkFlagFilename("file"))
1047
1048 // Single extension
1049 rootCmd.Flags().StringP("log", "l", "", "log flag")
1050 assertNoErr(t, rootCmd.MarkFlagFilename("log", "log"))
1051
1052 // Multiple extensions
1053 rootCmd.Flags().StringP("yaml", "y", "", "yaml flag")
1054 assertNoErr(t, rootCmd.MarkFlagFilename("yaml", "yaml", "yml"))
1055
1056 // Directly using annotation
1057 rootCmd.Flags().StringP("text", "t", "", "text flag")
1058 assertNoErr(t, rootCmd.Flags().SetAnnotation("text", BashCompFilenameExt, []string{"txt"}))
1059
1060 // Test that the completion logic returns the proper info for the completion
1061 // script to handle the file filtering
1062 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--file", "")
1063 if err != nil {
1064 t.Errorf("Unexpected error: %v", err)
1065 }
1066
1067 expected := strings.Join([]string{
1068 ":0",
1069 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
1070
1071 if output != expected {
1072 t.Errorf("expected: %q, got: %q", expected, output)
1073 }
1074
1075 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--log", "")
1076 if err != nil {
1077 t.Errorf("Unexpected error: %v", err)
1078 }
1079
1080 expected = strings.Join([]string{
1081 "log",
1082 ":8",
1083 "Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")
1084
1085 if output != expected {
1086 t.Errorf("expected: %q, got: %q", expected, output)
1087 }
1088
1089 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--yaml", "")
1090 if err != nil {
1091 t.Errorf("Unexpected error: %v", err)
1092 }
1093
1094 expected = strings.Join([]string{
1095 "yaml", "yml",

Callers

nothing calls this directly

Calls 4

FlagsMethod · 0.95
MarkFlagFilenameMethod · 0.95
executeCommandFunction · 0.85
assertNoErrFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…