-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind_test.go
85 lines (75 loc) · 1.19 KB
/
bind_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
package quack
import (
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
type simpleCmd struct {
NoHelp string
AnInt int `help:"I am an int"`
}
func (s *simpleCmd) Run(*cobra.Command, []string) {
}
func (s *simpleCmd) Help() string {
return "longer help message"
}
func sanatize(s string) string {
remove := []string{
"\n",
"\t",
" ",
}
for _, x := range remove {
s = strings.ReplaceAll(s, x, "")
}
return s
}
func TestBindCobra(t *testing.T) {
simple := new(simpleCmd)
tests := []struct {
name string
in any
usage string
err error
}{
{
"bad_type",
0,
"",
ErrInvalidType,
},
{
"not a command",
struct{}{},
"",
ErrNotACommand,
},
{
"simple",
simple,
`
Usage:
simple [flags]
Flags:
--an-int int I am an int
--no-help string
`,
nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cmd, err := BindCobra(test.name, test.in)
if test.err == nil {
assert.Nil(t, err)
cmd.UsageString()
assert.Equal(t,
sanatize(test.usage), sanatize(cmd.UsageString()))
return
}
assert.ErrorIs(t, err, test.err)
assert.Nil(t, cmd)
})
}
}