@@ -125,28 +125,40 @@ var _ = Describe("CLI", func() {
125
125
Context ("with no plugins specified" , func () {
126
126
It ("should return a valid CLI" , func () {
127
127
By ("setting one plugin" )
128
- c , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (pluginAV1 ))
128
+ c , err = New (
129
+ WithDefaultPlugins (pluginAV1 ),
130
+ WithPlugins (pluginAV1 ),
131
+ )
129
132
Expect (err ).NotTo (HaveOccurred ())
130
133
Expect (c ).NotTo (BeNil ())
131
134
Expect (c .(* cli ).pluginsFromOptions ).To (Equal (makeSetByProjVer (pluginAV1 )))
132
135
Expect (c .(* cli ).resolvedPlugins ).To (Equal ([]plugin.Base {pluginAV1 }))
133
136
134
137
By ("setting two plugins with different names and versions" )
135
- c , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (pluginAV1 , pluginBV2 ))
138
+ c , err = New (
139
+ WithDefaultPlugins (pluginAV1 ),
140
+ WithPlugins (pluginAV1 , pluginBV2 ),
141
+ )
136
142
Expect (err ).NotTo (HaveOccurred ())
137
143
Expect (c ).NotTo (BeNil ())
138
144
Expect (c .(* cli ).pluginsFromOptions ).To (Equal (makeSetByProjVer (pluginAV1 , pluginBV2 )))
139
145
Expect (c .(* cli ).resolvedPlugins ).To (Equal ([]plugin.Base {pluginAV1 }))
140
146
141
147
By ("setting two plugins with the same names and different versions" )
142
- c , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (pluginAV1 , pluginAV2 ))
148
+ c , err = New (
149
+ WithDefaultPlugins (pluginAV1 ),
150
+ WithPlugins (pluginAV1 , pluginAV2 ),
151
+ )
143
152
Expect (err ).NotTo (HaveOccurred ())
144
153
Expect (c ).NotTo (BeNil ())
145
154
Expect (c .(* cli ).pluginsFromOptions ).To (Equal (makeSetByProjVer (pluginAV1 , pluginAV2 )))
146
155
Expect (c .(* cli ).resolvedPlugins ).To (Equal ([]plugin.Base {pluginAV1 }))
147
156
148
157
By ("setting two plugins with different names and the same version" )
149
- c , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (pluginAV1 , pluginBV1 ))
158
+ c , err = New (
159
+ WithDefaultPlugins (pluginAV1 ),
160
+ WithPlugins (pluginAV1 , pluginBV1 ),
161
+ )
150
162
Expect (err ).NotTo (HaveOccurred ())
151
163
Expect (c ).NotTo (BeNil ())
152
164
Expect (c .(* cli ).pluginsFromOptions ).To (Equal (makeSetByProjVer (pluginAV1 , pluginBV1 )))
@@ -159,15 +171,22 @@ var _ = Describe("CLI", func() {
159
171
Expect (err ).To (MatchError (`no plugins for project version "3-alpha"` ))
160
172
161
173
By ("not setting any plugin" )
162
- _ , err = New (WithDefaultPlugins (pluginAV1 ))
174
+ _ , err = New (
175
+ WithDefaultPlugins (pluginAV1 ),
176
+ )
163
177
Expect (err ).To (MatchError (`no plugins for project version "3-alpha"` ))
164
178
165
179
By ("not setting any default plugins" )
166
- _ , err = New (WithPlugins (pluginAV1 ))
180
+ _ , err = New (
181
+ WithPlugins (pluginAV1 ),
182
+ )
167
183
Expect (err ).To (MatchError (`no default plugins for project version "3-alpha"` ))
168
184
169
185
By ("setting two plugins of the same name and version" )
170
- _ , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (pluginAV1 , pluginAV1 ))
186
+ _ , err = New (
187
+ WithDefaultPlugins (pluginAV1 ),
188
+ WithPlugins (pluginAV1 , pluginAV1 ),
189
+ )
171
190
Expect (err ).To (MatchError (`broken pre-set plugins: two plugins have the same key: "go.example.com/v1"` ))
172
191
})
173
192
})
@@ -189,31 +208,43 @@ var _ = Describe("CLI", func() {
189
208
It ("should return a valid CLI" , func () {
190
209
By (`setting cliPluginKey to "go"` )
191
210
setPluginsFlag ("go" )
192
- c , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (pluginAV1 , pluginAV2 ))
211
+ c , err = New (
212
+ WithDefaultPlugins (pluginAV1 ),
213
+ WithPlugins (pluginAV1 , pluginAV2 ),
214
+ )
193
215
Expect (err ).NotTo (HaveOccurred ())
194
216
Expect (c ).NotTo (BeNil ())
195
217
Expect (c .(* cli ).pluginsFromOptions ).To (Equal (makeSetByProjVer (pluginAV1 , pluginAV2 )))
196
218
Expect (c .(* cli ).resolvedPlugins ).To (Equal ([]plugin.Base {pluginAV1 }))
197
219
198
220
By (`setting cliPluginKey to "go/v1"` )
199
221
setPluginsFlag ("go/v1" )
200
- c , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (pluginAV1 , pluginBV2 ))
222
+ c , err = New (
223
+ WithDefaultPlugins (pluginAV1 ),
224
+ WithPlugins (pluginAV1 , pluginBV2 ),
225
+ )
201
226
Expect (err ).NotTo (HaveOccurred ())
202
227
Expect (c ).NotTo (BeNil ())
203
228
Expect (c .(* cli ).pluginsFromOptions ).To (Equal (makeSetByProjVer (pluginAV1 , pluginBV2 )))
204
229
Expect (c .(* cli ).resolvedPlugins ).To (Equal ([]plugin.Base {pluginAV1 }))
205
230
206
231
By (`setting cliPluginKey to "go/v2"` )
207
232
setPluginsFlag ("go/v2" )
208
- c , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (pluginAV1 , pluginBV2 ))
233
+ c , err = New (
234
+ WithDefaultPlugins (pluginAV1 ),
235
+ WithPlugins (pluginAV1 , pluginBV2 ),
236
+ )
209
237
Expect (err ).NotTo (HaveOccurred ())
210
238
Expect (c ).NotTo (BeNil ())
211
239
Expect (c .(* cli ).pluginsFromOptions ).To (Equal (makeSetByProjVer (pluginAV1 , pluginBV2 )))
212
240
Expect (c .(* cli ).resolvedPlugins ).To (Equal ([]plugin.Base {pluginBV2 }))
213
241
214
242
By (`setting cliPluginKey to "go.test.com/v2"` )
215
243
setPluginsFlag ("go.test.com/v2" )
216
- c , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (allPlugins ... ))
244
+ c , err = New (
245
+ WithDefaultPlugins (pluginAV1 ),
246
+ WithPlugins (allPlugins ... ),
247
+ )
217
248
Expect (err ).NotTo (HaveOccurred ())
218
249
Expect (c ).NotTo (BeNil ())
219
250
Expect (c .(* cli ).pluginsFromOptions ).To (Equal (makeSetByProjVer (allPlugins ... )))
@@ -223,21 +254,98 @@ var _ = Describe("CLI", func() {
223
254
It ("should return an error" , func () {
224
255
By (`setting cliPluginKey to an non-existent key "foo"` )
225
256
setPluginsFlag ("foo" )
226
- _ , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (pluginAV1 , pluginAV2 ))
257
+ _ , err = New (
258
+ WithDefaultPlugins (pluginAV1 ),
259
+ WithPlugins (pluginAV1 , pluginAV2 ),
260
+ )
227
261
Expect (err ).To (MatchError (errAmbiguousPlugin {
228
262
key : "foo" ,
229
263
msg : `no names match, possible plugins: ["go.example.com/v1" "go.example.com/v2"]` ,
230
264
}))
231
265
})
232
266
})
233
267
234
- Context ("with extra commands set" , func () {
268
+ Context ("WithCommandName" , func () {
269
+ It ("should use the provided command name" , func () {
270
+ commandName := "other-command"
271
+ c , err = New (
272
+ WithCommandName (commandName ),
273
+ WithDefaultPlugins (pluginAV1 ),
274
+ WithPlugins (allPlugins ... ),
275
+ )
276
+ Expect (err ).NotTo (HaveOccurred ())
277
+ Expect (c ).NotTo (BeNil ())
278
+ Expect (c .(* cli ).commandName ).To (Equal (commandName ))
279
+ })
280
+ })
281
+
282
+ Context ("WithDefaultProjectVersion" , func () {
283
+ var defaultProjectVersion string
284
+
285
+ It ("should use the provided default project version" , func () {
286
+ By (`using version "2"` )
287
+ defaultProjectVersion = "2"
288
+ c , err = New (
289
+ WithDefaultProjectVersion (defaultProjectVersion ),
290
+ WithDefaultPlugins (pluginAV1 ),
291
+ WithPlugins (allPlugins ... ),
292
+ )
293
+ Expect (err ).NotTo (HaveOccurred ())
294
+ Expect (c ).NotTo (BeNil ())
295
+ Expect (c .(* cli ).defaultProjectVersion ).To (Equal (defaultProjectVersion ))
296
+
297
+ By (`using version "3-alpha"` )
298
+ defaultProjectVersion = "3-alpha"
299
+ c , err = New (
300
+ WithDefaultProjectVersion (defaultProjectVersion ),
301
+ WithDefaultPlugins (pluginAV1 ),
302
+ WithPlugins (allPlugins ... ),
303
+ )
304
+ Expect (err ).NotTo (HaveOccurred ())
305
+ Expect (c ).NotTo (BeNil ())
306
+ Expect (c .(* cli ).defaultProjectVersion ).To (Equal (defaultProjectVersion ))
307
+ })
308
+
309
+ It ("should fail for invalid project versions" , func () {
310
+ By (`using version "0"` )
311
+ defaultProjectVersion = "0"
312
+ c , err = New (
313
+ WithDefaultProjectVersion (defaultProjectVersion ),
314
+ WithDefaultPlugins (pluginAV1 ),
315
+ WithPlugins (allPlugins ... ),
316
+ )
317
+ Expect (err ).To (HaveOccurred ())
318
+
319
+ By (`using version "1-gamma"` )
320
+ defaultProjectVersion = "1-gamma"
321
+ c , err = New (
322
+ WithDefaultProjectVersion (defaultProjectVersion ),
323
+ WithDefaultPlugins (pluginAV1 ),
324
+ WithPlugins (allPlugins ... ),
325
+ )
326
+ Expect (err ).To (HaveOccurred ())
327
+
328
+ By (`using version "1alpha"` )
329
+ defaultProjectVersion = "1alpha"
330
+ c , err = New (
331
+ WithDefaultProjectVersion (defaultProjectVersion ),
332
+ WithDefaultPlugins (pluginAV1 ),
333
+ WithPlugins (allPlugins ... ),
334
+ )
335
+ Expect (err ).To (HaveOccurred ())
336
+ })
337
+ })
338
+
339
+ Context ("WithExtraCommands" , func () {
235
340
It ("should work successfully with extra commands" , func () {
236
- setPluginsFlag ("go.test.com/v2" )
237
341
commandTest := & cobra.Command {
238
342
Use : "example" ,
239
343
}
240
- c , err = New (WithDefaultPlugins (pluginAV1 ), WithPlugins (allPlugins ... ), WithExtraCommands (commandTest ))
344
+ c , err = New (
345
+ WithExtraCommands (commandTest ),
346
+ WithDefaultPlugins (pluginAV1 ),
347
+ WithPlugins (allPlugins ... ),
348
+ )
241
349
Expect (err ).NotTo (HaveOccurred ())
242
350
Expect (c ).NotTo (BeNil ())
243
351
Expect (c .(* cli ).extraCommands [0 ]).NotTo (BeNil ())
0 commit comments