Skip to content

Commit ed7bb9d

Browse files
authored
Add unit test for fish completion (#1515)
1 parent f464d6c commit ed7bb9d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

fish_completions_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cobra
33
import (
44
"bytes"
55
"fmt"
6+
"log"
7+
"os"
68
"testing"
79
)
810

@@ -80,3 +82,57 @@ func TestFishCompletionNoActiveHelp(t *testing.T) {
8082
activeHelpVar := activeHelpEnvVar(c.Name())
8183
check(t, output, fmt.Sprintf("%s=0", activeHelpVar))
8284
}
85+
86+
func TestGenFishCompletionFile(t *testing.T) {
87+
err := os.Mkdir("./tmp", 0755)
88+
if err != nil {
89+
log.Fatal(err.Error())
90+
}
91+
92+
defer os.RemoveAll("./tmp")
93+
94+
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
95+
child := &Command{
96+
Use: "child",
97+
ValidArgsFunction: validArgsFunc,
98+
Run: emptyRun,
99+
}
100+
rootCmd.AddCommand(child)
101+
102+
assertNoErr(t, rootCmd.GenFishCompletionFile("./tmp/test", false))
103+
}
104+
105+
func TestFailGenFishCompletionFile(t *testing.T) {
106+
err := os.Mkdir("./tmp", 0755)
107+
if err != nil {
108+
log.Fatal(err.Error())
109+
}
110+
111+
defer os.RemoveAll("./tmp")
112+
113+
f, _ := os.OpenFile("./tmp/test", os.O_CREATE, 0400)
114+
defer f.Close()
115+
116+
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
117+
child := &Command{
118+
Use: "child",
119+
ValidArgsFunction: validArgsFunc,
120+
Run: emptyRun,
121+
}
122+
rootCmd.AddCommand(child)
123+
124+
got := rootCmd.GenFishCompletionFile("./tmp/test", false)
125+
if got == nil {
126+
t.Error("should raise permission denied error")
127+
}
128+
129+
if os.Getenv("MSYSTEM") == "MINGW64" {
130+
if got.Error() != "open ./tmp/test: Access is denied." {
131+
t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: Access is denied.")
132+
}
133+
} else {
134+
if got.Error() != "open ./tmp/test: permission denied" {
135+
t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: permission denied")
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)