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

Function TestFlagDirFilterCompletionInGo

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

Source from the content-addressed store, hash-verified

1158}
1159
1160func TestFlagDirFilterCompletionInGo(t *testing.T) {
1161 rootCmd := &Command{
1162 Use: "root",
1163 Run: emptyRun,
1164 }
1165
1166 // Filter directories
1167 rootCmd.Flags().StringP("dir", "d", "", "dir flag")
1168 assertNoErr(t, rootCmd.MarkFlagDirname("dir"))
1169
1170 // Filter directories within a directory
1171 rootCmd.Flags().StringP("subdir", "s", "", "subdir")
1172 assertNoErr(t, rootCmd.Flags().SetAnnotation("subdir", BashCompSubdirsInDir, []string{"themes"}))
1173
1174 // Multiple directory specification get ignored
1175 rootCmd.Flags().StringP("manydir", "m", "", "manydir")
1176 assertNoErr(t, rootCmd.Flags().SetAnnotation("manydir", BashCompSubdirsInDir, []string{"themes", "colors"}))
1177
1178 // Test that the completion logic returns the proper info for the completion
1179 // script to handle the directory filtering
1180 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--dir", "")
1181 if err != nil {
1182 t.Errorf("Unexpected error: %v", err)
1183 }
1184
1185 expected := strings.Join([]string{
1186 ":16",
1187 "Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")
1188
1189 if output != expected {
1190 t.Errorf("expected: %q, got: %q", expected, output)
1191 }
1192
1193 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-d", "")
1194 if err != nil {
1195 t.Errorf("Unexpected error: %v", err)
1196 }
1197
1198 expected = strings.Join([]string{
1199 ":16",
1200 "Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")
1201
1202 if output != expected {
1203 t.Errorf("expected: %q, got: %q", expected, output)
1204 }
1205
1206 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--subdir", "")
1207 if err != nil {
1208 t.Errorf("Unexpected error: %v", err)
1209 }
1210
1211 expected = strings.Join([]string{
1212 "themes",
1213 ":16",
1214 "Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")
1215
1216 if output != expected {
1217 t.Errorf("expected: %q, got: %q", expected, output)

Callers

nothing calls this directly

Calls 4

FlagsMethod · 0.95
MarkFlagDirnameMethod · 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…