-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
197 lines (185 loc) · 4.4 KB
/
Copy pathcommand_test.go
File metadata and controls
197 lines (185 loc) · 4.4 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package naistrix_test
import (
"bytes"
"strings"
"testing"
"github.com/nais/naistrix"
)
func TestCommandValidation(t *testing.T) {
tests := []struct {
name string
cmd *naistrix.Command
errorContains string
}{
{
name: "command with no name",
cmd: &naistrix.Command{
Title: "Test command",
RunFunc: noop,
},
errorContains: "cannot be empty",
},
{
name: "command with space in name",
cmd: &naistrix.Command{
Name: "test command",
Title: "Test command",
RunFunc: noop,
},
errorContains: "contain spaces",
},
{
name: "command with no title",
cmd: &naistrix.Command{
Name: "cmd",
RunFunc: noop,
},
errorContains: "missing a title",
},
{
name: "command with newline in title",
cmd: &naistrix.Command{
Name: "test",
Title: "Test command\nwith newline",
RunFunc: noop,
},
errorContains: "contains newline",
},
{
name: "missing RunFunc and SubCommands",
cmd: &naistrix.Command{
Name: "test",
Title: "Some title",
},
errorContains: "either RunFunc, SubCommands or Deprecated must be set",
},
{
name: "has both RunFunc and SubCommands",
cmd: &naistrix.Command{
Name: "test",
Title: "Some title",
RunFunc: noop,
SubCommands: []*naistrix.Command{
{
Name: "sub",
Title: "Some title",
RunFunc: noop,
},
},
},
errorContains: "cannot have both RunFunc and SubCommands",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app, _, err := naistrix.NewApplication("app", "title", "v0.0.0")
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
err = app.AddCommand(tt.cmd)
if err == nil {
t.Fatalf("expected error, got nil")
} else if !strings.Contains(err.Error(), tt.errorContains) {
t.Fatalf("expected error message to contain %q, got: %q", tt.errorContains, err.Error())
}
})
}
}
func TestArgumentUseString(t *testing.T) {
tests := []struct {
name string
expectedArgsString string
args []naistrix.Argument
}{
{
name: "no arguments",
expectedArgsString: "",
},
{
name: "argument",
expectedArgsString: "ARG",
args: []naistrix.Argument{
{Name: "arg"},
},
},
{
name: "repeatable argument",
expectedArgsString: "ARG [ARG...]",
args: []naistrix.Argument{
{Name: "arg", Repeatable: true},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
app, _, err := naistrix.NewApplication(
"app",
"title",
"v0.0.0",
naistrix.ApplicationWithWriter(buf),
)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
err = app.AddCommand(&naistrix.Command{
Name: "test",
Title: "Test command",
Args: tt.args,
RunFunc: noop,
})
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if err := app.Run(naistrix.RunWithArgs([]string{"test", "-h"})); err != nil {
t.Fatalf("expected no error, got %v", err)
}
expectedUsage := strings.TrimSpace("Usage:\n app test "+tt.expectedArgsString) + " [flags]\n"
if helpText := buf.String(); !strings.Contains(helpText, expectedUsage) {
t.Fatalf("expected help text to contain %q, got %q", expectedUsage, helpText)
}
})
}
}
func TestCommandArgumentValidation(t *testing.T) {
tests := []struct {
name string
args []naistrix.Argument
errorContains string
}{
{
name: "missing argument name",
args: []naistrix.Argument{
{Repeatable: true},
},
errorContains: "cannot be empty",
},
{
name: "repeatable argument must be last",
args: []naistrix.Argument{
{Name: "arg1", Repeatable: true},
{Name: "arg2"},
},
errorContains: "must be the last argument",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app, _, err := naistrix.NewApplication("app", "title", "v0.0.0")
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
err = app.AddCommand(&naistrix.Command{
Name: "test",
Title: "Test command",
RunFunc: noop,
Args: tt.args,
})
if err == nil {
t.Fatalf("expected error, got nil")
} else if !strings.Contains(err.Error(), tt.errorContains) {
t.Fatalf("expected error message to contain %q, got: %q", tt.errorContains, err.Error())
}
})
}
}