-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerdeterCommandinternal_test.go
156 lines (144 loc) · 4.84 KB
/
VerdeterCommandinternal_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package verdeter
import (
"errors"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
// This file is intended to hold the tests that need to be run in the verdeter namespace
func TestNewVerdeterCommandAddSubCommand(t *testing.T) {
verdeterCmd := NewVerdeterCommand(
"test",
"test short description",
"test long description",
func(verdeterCmd *VerdeterCommand, args []string) error {
return nil
},
)
assert.Empty(t, verdeterCmd.subCmds)
verdeterSubCmd := NewVerdeterCommand(
"subtest",
"", "",
func(verdeterCmd *VerdeterCommand, args []string) error {
return nil
},
)
verdeterCmd.AddSubCommand(verdeterSubCmd)
assert.NotEmpty(t, verdeterCmd.subCmds)
}
func TestVerdeterCommandgetRootCommand(t *testing.T) {
verdeterCmd := NewVerdeterCommand(
"test",
"test short description",
"test long description",
func(verdeterCmd *VerdeterCommand, args []string) error {
return nil
},
)
assert.Empty(t, verdeterCmd.subCmds)
verdeterSubCmd := NewVerdeterCommand(
"subtest",
"", "",
func(verdeterCmd *VerdeterCommand, args []string) error {
return nil
},
)
verdeterSubCmd.parentCmd = verdeterCmd
verdeterSubSubCmd := NewVerdeterCommand(
"subsubtest",
"", "",
func(verdeterCmd *VerdeterCommand, args []string) error {
return nil
},
)
verdeterSubSubCmd.parentCmd = verdeterSubCmd
assert.Equal(t, verdeterCmd, verdeterSubSubCmd.getRootCommand())
assert.Equal(t, verdeterCmd, verdeterSubCmd.getRootCommand())
}
func TestBuildVerdeterCommands(t *testing.T) {
// just some data to fill the structure
config := VerdeterConfig{
Use: "eee",
Aliases: []string{"1", "2"},
SuggestFor: []string{"4", "3"},
Short: "short",
Long: "long",
Example: "exemple",
ValidArgs: []string{"valid arg1", "valid arg2"},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"valid arg"}, cobra.ShellCompDirectiveFilterDirs
},
Args: func(cmd *cobra.Command, args []string) error {
return errors.New("err args")
},
ArgAliases: []string{"alias1", "alias2"},
BashCompletionFunction: "BashCompletionFunction",
Deprecated: "Deprecated",
Annotations: map[string]string{"super": "annotation"},
Version: "Version",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return nil
},
PreRun: func(cmd *cobra.Command, args []string) {
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return nil
},
Run: func(cmd *cobra.Command, args []string) {
},
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
PostRun: func(cmd *cobra.Command, args []string) {
},
PostRunE: func(cmd *cobra.Command, args []string) error {
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
verdeterCmd := BuildVerdeterCommand(config)
assert.Equal(t, config.Use, verdeterCmd.cmd.Use)
assert.Equal(t, config.Aliases, verdeterCmd.cmd.Aliases)
assert.Equal(t, config.SuggestFor, verdeterCmd.cmd.SuggestFor)
assert.Equal(t, config.Short, verdeterCmd.cmd.Short)
assert.Equal(t, config.Long, verdeterCmd.cmd.Long)
assert.Equal(t, config.Example, verdeterCmd.cmd.Example)
assert.Equal(t, config.ValidArgs, verdeterCmd.cmd.ValidArgs)
assert.NotNil(t, config.ValidArgsFunction, verdeterCmd.cmd.ValidArgsFunction)
assert.NotNil(t, config.Args, verdeterCmd.cmd.Args)
assert.Equal(t, config.ArgAliases, verdeterCmd.cmd.ArgAliases)
assert.Equal(t, config.BashCompletionFunction, verdeterCmd.cmd.BashCompletionFunction)
assert.Equal(t, config.Deprecated, verdeterCmd.cmd.Deprecated)
assert.Equal(t, config.Annotations, verdeterCmd.cmd.Annotations)
assert.Equal(t, config.Version, verdeterCmd.cmd.Version)
assert.NotNil(t, verdeterCmd.cmd.PersistentPreRun)
assert.NotNil(t, verdeterCmd.cmd.PersistentPreRunE)
assert.NotNil(t, verdeterCmd.cmd.PreRun)
assert.NotNil(t, verdeterCmd.cmd.PreRunE)
assert.NotNil(t, verdeterCmd.cmd.Run)
assert.NotNil(t, verdeterCmd.cmd.RunE)
assert.NotNil(t, verdeterCmd.cmd.PostRun)
assert.NotNil(t, verdeterCmd.cmd.PostRunE)
assert.NotNil(t, verdeterCmd.cmd.PersistentPostRun)
assert.NotNil(t, verdeterCmd.cmd.PersistentPostRunE)
assert.NotNil(t, verdeterCmd.subCmds)
assert.NotNil(t, verdeterCmd.globalConfigKeys)
assert.NotNil(t, verdeterCmd.localConfigKeys)
assert.NotNil(t, verdeterCmd.constraints)
}
func TestBuildVerdeterCommandsNoPreRunE(t *testing.T) {
// just some data to fill the structure
config := VerdeterConfig{
PreRun: func(cmd *cobra.Command, args []string) {
},
}
verdeterCmd := BuildVerdeterCommand(config)
assert.NotNil(t, verdeterCmd.cmd.PreRun)
assert.Nil(t, verdeterCmd.cmd.PreRunE)
}