(t *testing.T)
| 1274 | } |
| 1275 | |
| 1276 | func TestValidArgsFuncCmdContext(t *testing.T) { |
| 1277 | validArgsFunc := func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { |
| 1278 | ctx := cmd.Context() |
| 1279 | |
| 1280 | if ctx == nil { |
| 1281 | t.Error("Received nil context in completion func") |
| 1282 | } else if ctx.Value("testKey") != "123" { |
| 1283 | t.Error("Received invalid context") |
| 1284 | } |
| 1285 | |
| 1286 | return nil, ShellCompDirectiveDefault |
| 1287 | } |
| 1288 | |
| 1289 | rootCmd := &Command{ |
| 1290 | Use: "root", |
| 1291 | Run: emptyRun, |
| 1292 | } |
| 1293 | childCmd := &Command{ |
| 1294 | Use: "childCmd", |
| 1295 | ValidArgsFunction: validArgsFunc, |
| 1296 | Run: emptyRun, |
| 1297 | } |
| 1298 | rootCmd.AddCommand(childCmd) |
| 1299 | |
| 1300 | //nolint:staticcheck // We can safely use a basic type as key in tests. |
| 1301 | ctx := context.WithValue(context.Background(), "testKey", "123") |
| 1302 | |
| 1303 | // Test completing an empty string on the childCmd |
| 1304 | _, output, err := executeCommandWithContextC(ctx, rootCmd, ShellCompNoDescRequestCmd, "childCmd", "") |
| 1305 | if err != nil { |
| 1306 | t.Errorf("Unexpected error: %v", err) |
| 1307 | } |
| 1308 | |
| 1309 | expected := strings.Join([]string{ |
| 1310 | ":0", |
| 1311 | "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n") |
| 1312 | |
| 1313 | if output != expected { |
| 1314 | t.Errorf("expected: %q, got: %q", expected, output) |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | func TestValidArgsFuncSingleCmd(t *testing.T) { |
| 1319 | rootCmd := &Command{ |
nothing calls this directly
no test coverage detected