-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfish_completions_test.go
112 lines (91 loc) · 3.32 KB
/
fish_completions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package zulu_test
import (
"bytes"
"errors"
"os"
"path/filepath"
"testing"
"github.com/zulucmd/zulu/v2"
"github.com/zulucmd/zulu/v2/internal/testutil"
)
func TestCompleteNoDesCmdInFishScript(t *testing.T) {
rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: noopRun}
child := &zulu.Command{
Use: "child",
ValidArgsFunction: validArgsFunc,
RunE: noopRun,
}
rootCmd.AddCommand(child)
buf := new(bytes.Buffer)
testutil.AssertNil(t, rootCmd.GenFishCompletion(buf, false))
output := buf.String()
testutil.AssertContains(t, output, zulu.ShellCompNoDescRequestCmd)
}
func TestCompleteCmdInFishScript(t *testing.T) {
rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: noopRun}
child := &zulu.Command{
Use: "child",
ValidArgsFunction: validArgsFunc,
RunE: noopRun,
}
rootCmd.AddCommand(child)
buf := new(bytes.Buffer)
testutil.AssertNil(t, rootCmd.GenFishCompletion(buf, true))
output := buf.String()
testutil.AssertContains(t, output, zulu.ShellCompRequestCmd+" ")
testutil.AssertNotContains(t, output, zulu.ShellCompNoDescRequestCmd)
}
func TestProgWithDash(t *testing.T) {
rootCmd := &zulu.Command{Use: "root-dash", Args: zulu.NoArgs, RunE: noopRun}
buf := new(bytes.Buffer)
testutil.AssertNil(t, rootCmd.GenFishCompletion(buf, false))
output := buf.String()
// Functions name should have replaced the '-'
testutil.AssertContains(t, output, "__root_dash_perform_completion")
testutil.AssertNotContains(t, output, "__root-dash_perform_completion")
// The command name should not have replaced the '-'
testutil.AssertContains(t, output, "-c root-dash")
testutil.AssertNotContains(t, output, "-c root_dash")
}
func TestProgWithColon(t *testing.T) {
rootCmd := &zulu.Command{Use: "root:colon", Args: zulu.NoArgs, RunE: noopRun}
buf := new(bytes.Buffer)
testutil.AssertNil(t, rootCmd.GenFishCompletion(buf, false))
output := buf.String()
// Functions name should have replaced the ':'
testutil.AssertContains(t, output, "__root_colon_perform_completion")
testutil.AssertNotContains(t, output, "__root:colon_perform_completion")
// The command name should not have replaced the ':'
testutil.AssertContains(t, output, "-c root:colon")
testutil.AssertNotContains(t, output, "-c root_colon")
}
func TestGenFishCompletionFile(t *testing.T) {
tmpFile, err := os.CreateTemp(t.TempDir(), "cobra-test")
if err != nil {
t.Fatal(err.Error())
}
defer os.RemoveAll(tmpFile.Name())
rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: noopRun}
child := &zulu.Command{
Use: "child",
ValidArgsFunction: validArgsFunc,
RunE: noopRun,
}
rootCmd.AddCommand(child)
testutil.AssertNil(t, rootCmd.GenFishCompletionFile(tmpFile.Name(), false))
}
func TestFailGenFishCompletionFile(t *testing.T) {
tmpDir := t.TempDir()
f, _ := os.OpenFile(filepath.Join(tmpDir, "test"), os.O_CREATE, 0400)
defer f.Close()
rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: noopRun}
child := &zulu.Command{
Use: "child",
ValidArgsFunction: validArgsFunc,
RunE: noopRun,
}
rootCmd.AddCommand(child)
got := rootCmd.GenFishCompletionFile(f.Name(), false)
testutil.AssertNotNilf(t, got, "should raise permission denied error")
testutil.AssertEqual(t, true, errors.Is(got, os.ErrPermission))
}