-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion_test.go
More file actions
119 lines (106 loc) · 2.69 KB
/
Copy pathcompletion_test.go
File metadata and controls
119 lines (106 loc) · 2.69 KB
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
117
118
119
package main
import (
"bytes"
"os"
"os/exec"
"strings"
"testing"
)
func TestRunCompletionBashNonEmpty(t *testing.T) {
buf := &bytes.Buffer{}
old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
os.Stdout = w
done := make(chan struct{})
go func() {
defer close(done)
_, _ = buf.ReadFrom(r)
}()
if err := runCompletion([]string{"bash"}); err != nil {
os.Stdout = old
t.Fatalf("completion bash: %v", err)
}
w.Close()
<-done
os.Stdout = old
out := buf.String()
if out == "" {
t.Fatal("bash completion is empty")
}
for _, want := range []string{"complete -F", "signup", "share", "remove", "completion"} {
if !strings.Contains(out, want) {
t.Errorf("bash completion missing %q\n%s", want, out)
}
}
}
func TestRunCompletionZshNonEmpty(t *testing.T) {
buf := &bytes.Buffer{}
old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
os.Stdout = w
done := make(chan struct{})
go func() {
defer close(done)
_, _ = buf.ReadFrom(r)
}()
if err := runCompletion([]string{"zsh"}); err != nil {
os.Stdout = old
t.Fatalf("completion zsh: %v", err)
}
w.Close()
<-done
os.Stdout = old
out := buf.String()
if out == "" {
t.Fatal("zsh completion is empty")
}
for _, want := range []string{"#compdef gander", "_gander", "signup", "share", "remove", "completion"} {
if !strings.Contains(out, want) {
t.Errorf("zsh completion missing %q\n%s", want, out)
}
}
}
func TestRunCompletionRejectsUnknownShell(t *testing.T) {
if err := runCompletion([]string{"fish"}); err == nil {
t.Fatal("expected error for unsupported shell")
} else if !strings.Contains(err.Error(), "fish") {
t.Errorf("err = %v, want mention of unsupported shell", err)
}
}
func TestRunCompletionRequiresArg(t *testing.T) {
if err := runCompletion(nil); err == nil {
t.Fatal("expected error for missing shell argument")
}
}
func TestBashCompletionSourcedWithoutError(t *testing.T) {
if _, err := exec.LookPath("bash"); err != nil {
t.Skip("bash not on PATH")
}
if err := runCompletion([]string{"bash"}); err != nil {
t.Fatalf("completion bash: %v", err)
}
cmd := exec.Command("bash", "-n", "-")
cmd.Stdin = strings.NewReader(bashCompletion)
if out, err := cmd.CombinedOutput(); err != nil {
t.Errorf("bash -n failed: %v\n%s", err, out)
}
}
func TestZshCompletionSourcedWithoutError(t *testing.T) {
if _, err := exec.LookPath("zsh"); err != nil {
t.Skip("zsh not on PATH")
}
if err := runCompletion([]string{"zsh"}); err != nil {
t.Fatalf("completion zsh: %v", err)
}
cmd := exec.Command("zsh", "-n", "-")
cmd.Stdin = strings.NewReader(zshCompletion)
if out, err := cmd.CombinedOutput(); err != nil {
t.Errorf("zsh -n failed: %v\n%s", err, out)
}
}