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