-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_completions_test.go
116 lines (93 loc) · 3.35 KB
/
bash_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
113
114
115
116
package zulu_test
import (
"bytes"
"os"
"testing"
"github.com/zulucmd/zulu/v2"
"github.com/zulucmd/zulu/v2/internal/testutil"
)
func TestCompleteNoDesCmdInBashScript(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.GenBashCompletion(buf, false))
output := buf.String()
testutil.AssertContains(t, output, zulu.ShellCompNoDescRequestCmd)
}
func TestCompleteCmdInBashScript(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.GenBashCompletion(buf, true))
output := buf.String()
testutil.AssertContains(t, output, zulu.ShellCompRequestCmd+"$")
testutil.AssertNotContains(t, output, zulu.ShellCompNoDescRequestCmd)
}
func TestBashProgWithDash(t *testing.T) {
rootCmd := &zulu.Command{Use: "root-dash", Args: zulu.NoArgs, RunE: noopRun}
buf := new(bytes.Buffer)
testutil.AssertNil(t, rootCmd.GenBashCompletion(buf, false))
output := buf.String()
// Functions name should have replace the '-'
testutil.AssertContains(t, output, "__root_dash_init_completion")
testutil.AssertNotContains(t, output, "__root-dash_init_completion")
// The command name should not have replaced the '-'
testutil.AssertContains(t, output, "__start_root_dash root-dash")
testutil.AssertNotContains(t, output, "dash root_dash")
}
func TestBashProgWithColon(t *testing.T) {
rootCmd := &zulu.Command{Use: "root:colon", Args: zulu.NoArgs, RunE: noopRun}
buf := new(bytes.Buffer)
testutil.AssertNil(t, rootCmd.GenBashCompletion(buf, false))
output := buf.String()
// Functions name should have replace the ':'
testutil.AssertContains(t, output, "__root_colon_init_completion")
testutil.AssertNotContains(t, output, "__root:colon_init_completion")
// The command name should not have replaced the ':'
testutil.AssertContains(t, output, "__start_root_colon root:colon")
testutil.AssertNotContains(t, output, "colon root_colon")
}
func TestGenBashCompletionFile(t *testing.T) {
err := os.Mkdir("./tmp", 0755)
if err != nil {
t.Fatal(err.Error())
}
defer os.RemoveAll("./tmp")
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.GenBashCompletionFile("./tmp/test", false))
}
func TestFailGenBashCompletionFile(t *testing.T) {
err := os.Mkdir("./tmp", 0755)
if err != nil {
t.Fatal(err.Error())
}
defer os.RemoveAll("./tmp")
f, _ := os.OpenFile("./tmp/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)
err = rootCmd.GenBashCompletionFile("./tmp/test", false)
testutil.AssertErrf(t, err, "should raise permission denied error")
testutil.AssertEqual(t, expectedPermissionError, err.Error())
}