-
Notifications
You must be signed in to change notification settings - Fork 0
/
compsh_test.go
114 lines (100 loc) · 2.55 KB
/
compsh_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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 The Prime Citizens
package cli
import (
"strings"
"testing"
"github.com/primecitizens/cli/internal/assert"
)
func TestCompCmdShells(t *testing.T) {
var cc CompCmdShells
root := &Cmd{
Pattern: "foo",
Children: []*Cmd{
cc.Setup("", -1, true),
{
Pattern: "dirs",
BriefUsage: "complete dirs",
Completion: CompActionDirs{},
},
{
Pattern: "files-and-dirs",
Completion: CompActionFiles{},
},
{
Pattern: "none",
BriefUsage: "complete nothing",
Completion: CompActionDisable{},
},
},
}
for _, shell := range []string{
"zsh",
"bash",
"pwsh",
} {
cc.opComp.At.State_ = 0 // reset
t.Run("PrintScript", func(t *testing.T) {
var sb strings.Builder
err := root.Exec(&CmdOptions{Stdout: &sb},
"completion", shell,
)
assert.NoError(t, err)
assert.False(t, strings.Contains(sb.String(), placeholderName))
assert.False(t, strings.Contains(sb.String(), placeholderNameForIdent))
assert.False(t, strings.Contains(sb.String(), placeholderCompletionCmdName))
assert.True(t, sb.Len() > 100)
})
t.Run("PrintUsage", func(t *testing.T) {
var sb strings.Builder
err := root.Exec(&CmdOptions{Stdout: &sb},
"completion", shell, "--help",
)
assert.ErrorIs(t, ErrHelpHandled{}, err)
assert.False(t, strings.Contains(sb.String(), placeholderName))
assert.False(t, strings.Contains(sb.String(), placeholderNameForIdent))
assert.False(t, strings.Contains(sb.String(), placeholderCompletionCmdName))
assert.True(t, sb.Len() > 100)
})
t.Run("BadRequest", func(t *testing.T) {
var sb strings.Builder
err := root.Exec(&CmdOptions{Stderr: &sb},
"completion", shell, "complete",
)
assert.ErrorIs(t, &FlagViolation{
Key: "at",
Reason: ViolationCodeEmptyAllOf,
}, err)
})
t.Run("GoodRequest", func(t *testing.T) {
var sb strings.Builder
err := root.Exec(
&CmdOptions{
Stdout: &sb,
},
"completion", shell, "complete", "--at", "5",
"--",
"arg0", "completion", shell, "complete", "--debug-file", "",
)
assert.NoError(t, err)
})
}
}
func TestCompCmdOpComplete(t *testing.T) {
var cmd CompCmdOpComplete
cmd.Setup(0)
flags := []string{"debug-file", "at", "timeout"}
i := 0
for ; i < len(flags); i++ {
_, ok := cmd.NthFlag(i)
assert.True(t, ok)
}
_, ok := cmd.NthFlag(i)
assert.False(t, ok)
for _, flag := range flags {
f, ok := cmd.FindFlag(flag)
assertFlagTrue(t, f, ok)
}
f, ok := cmd.FindFlag("non-existing")
assertNoflagFalse(t, f, ok)
}