| 10 | ) |
| 11 | |
| 12 | func TestNewCmdCompletion(t *testing.T) { |
| 13 | tests := []struct { |
| 14 | name string |
| 15 | args string |
| 16 | wantOut string |
| 17 | wantErr string |
| 18 | }{ |
| 19 | { |
| 20 | name: "no arguments", |
| 21 | args: "completion", |
| 22 | wantOut: "complete -o default -F __start_gh gh", |
| 23 | }, |
| 24 | { |
| 25 | name: "zsh completion", |
| 26 | args: "completion -s zsh", |
| 27 | wantOut: "#compdef gh", |
| 28 | }, |
| 29 | { |
| 30 | name: "fish completion", |
| 31 | args: "completion -s fish", |
| 32 | wantOut: "complete -c gh ", |
| 33 | }, |
| 34 | { |
| 35 | name: "PowerShell completion", |
| 36 | args: "completion -s powershell", |
| 37 | wantOut: "Register-ArgumentCompleter", |
| 38 | }, |
| 39 | { |
| 40 | name: "unsupported shell", |
| 41 | args: "completion -s csh", |
| 42 | wantErr: "invalid argument \"csh\" for \"-s, --shell\" flag: valid values are {bash|zsh|fish|powershell}", |
| 43 | }, |
| 44 | } |
| 45 | for _, tt := range tests { |
| 46 | t.Run(tt.name, func(t *testing.T) { |
| 47 | ios, _, stdout, stderr := iostreams.Test() |
| 48 | completeCmd := NewCmdCompletion(ios) |
| 49 | rootCmd := &cobra.Command{Use: "gh"} |
| 50 | rootCmd.AddCommand(completeCmd) |
| 51 | |
| 52 | argv, err := shlex.Split(tt.args) |
| 53 | if err != nil { |
| 54 | t.Fatalf("argument splitting error: %v", err) |
| 55 | } |
| 56 | rootCmd.SetArgs(argv) |
| 57 | rootCmd.SetOut(stderr) |
| 58 | rootCmd.SetErr(stderr) |
| 59 | |
| 60 | _, err = rootCmd.ExecuteC() |
| 61 | if tt.wantErr != "" { |
| 62 | if err == nil || err.Error() != tt.wantErr { |
| 63 | t.Fatalf("expected error %q, got %q", tt.wantErr, err) |
| 64 | } |
| 65 | return |
| 66 | } |
| 67 | if err != nil { |
| 68 | t.Fatalf("error executing command: %v", err) |
| 69 | } |