-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcompleter_test.go
More file actions
88 lines (71 loc) · 2.08 KB
/
Copy pathcompleter_test.go
File metadata and controls
88 lines (71 loc) · 2.08 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
package console
import (
"strings"
"testing"
"github.com/reeflective/readline"
"github.com/spf13/cobra"
)
func TestCompleteHidesCarapaceCommand(t *testing.T) {
c := New("test")
root := &cobra.Command{Use: "root"}
internal := &cobra.Command{Use: "_carapace"}
root.AddCommand(internal, &cobra.Command{Use: "visible"})
c.activeMenu().Command = root
comps := c.complete(nil, 0)
if !internal.Hidden {
t.Fatal("_carapace command was not hidden")
}
for _, value := range completionValues(comps) {
if strings.TrimSpace(value) == "_carapace" {
t.Fatalf("completion values include internal command: %v", completionValues(comps))
}
}
}
func TestCompleteResetsFlagDefaults(t *testing.T) {
c := New("test")
root := &cobra.Command{Use: "root"}
cmd := &cobra.Command{Use: "serve"}
cmd.Flags().Bool("verbose", false, "")
root.AddCommand(cmd)
c.activeMenu().Command = root
if err := cmd.Flags().Set("verbose", "true"); err != nil {
t.Fatal(err)
}
_ = c.complete([]rune("serve "), len("serve "))
flag := cmd.Flags().Lookup("verbose")
if flag == nil {
t.Fatal("missing verbose flag")
}
if flag.Changed {
t.Fatal("completion did not clear flag Changed state")
}
if flag.Value.String() != "false" {
t.Fatalf("flag value = %q, want false", flag.Value.String())
}
}
func TestCompleteResetsArgsLenAtDash(t *testing.T) {
c := New("test")
root := &cobra.Command{Use: "root"}
cmd := &cobra.Command{Use: "serve"}
cmd.Flags().Bool("verbose", false, "")
root.AddCommand(cmd)
c.activeMenu().Command = root
if err := cmd.Flags().Parse([]string{"--", "positional"}); err != nil {
t.Fatal(err)
}
if got := cmd.Flags().ArgsLenAtDash(); got < 0 {
t.Fatalf("test setup did not set ArgsLenAtDash: %d", got)
}
_ = c.complete([]rune("serve "), len("serve "))
if got := cmd.Flags().ArgsLenAtDash(); got != -1 {
t.Fatalf("ArgsLenAtDash = %d, want -1", got)
}
}
func completionValues(comps readline.Completions) []string {
var values []string
comps.EachValue(func(comp readline.Completion) readline.Completion {
values = append(values, comp.Value)
return comp
})
return values
}