| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | func TestFishCompletion(t *testing.T) { |
| 13 | // Given |
| 14 | cmd := buildExtendedTestCommand() |
| 15 | cmd.Flags = append(cmd.Flags, |
| 16 | &StringFlag{ |
| 17 | Name: "logfile", |
| 18 | TakesFile: true, |
| 19 | }, |
| 20 | &StringSliceFlag{ |
| 21 | Name: "foofile", |
| 22 | TakesFile: true, |
| 23 | }) |
| 24 | cmd.setupCommandGraph() |
| 25 | |
| 26 | oldTemplate := FishCompletionTemplate |
| 27 | defer func() { FishCompletionTemplate = oldTemplate }() |
| 28 | FishCompletionTemplate = "{{something" |
| 29 | |
| 30 | // test error case |
| 31 | _, err1 := cmd.ToFishCompletion() |
| 32 | assert.Error(t, err1) |
| 33 | |
| 34 | // reset the template |
| 35 | FishCompletionTemplate = oldTemplate |
| 36 | // When |
| 37 | res, err := cmd.ToFishCompletion() |
| 38 | |
| 39 | // Then |
| 40 | require.NoError(t, err) |
| 41 | expectFileContent(t, "testdata/expected-fish-full.fish", res) |
| 42 | } |
| 43 | |
| 44 | func TestFishCompletionShellComplete(t *testing.T) { |
| 45 | cmd := buildExtendedTestCommand() |
| 46 | cmd.ShellComplete = func(context.Context, *Command) {} |
| 47 | |
| 48 | configCmd := cmd.Command("config") |
| 49 | configCmd.ShellComplete = func(context.Context, *Command) {} |
| 50 | |
| 51 | subConfigCmd := configCmd.Command("sub-config") |
| 52 | subConfigCmd.ShellComplete = func(context.Context, *Command) {} |
| 53 | |
| 54 | cmd.setupCommandGraph() |
| 55 | |
| 56 | res, err := cmd.ToFishCompletion() |
| 57 | require.NoError(t, err) |
| 58 | |
| 59 | assert.Contains(t, res, fmt.Sprintf("complete -c greet -n '__fish_greet_no_subcommand' -xa '(greet %s 2>/dev/null)'", completionFlag)) |
| 60 | assert.Contains(t, res, fmt.Sprintf("complete -c greet -n '__fish_seen_subcommand_from config c' -xa '(greet config %s 2>/dev/null)'", completionFlag)) |
| 61 | assert.Contains(t, res, fmt.Sprintf("complete -c greet -n '__fish_seen_subcommand_from config c; and __fish_seen_subcommand_from sub-config s ss' -xa '(greet config sub-config %s 2>/dev/null)'", completionFlag)) |
| 62 | } |