@@ -297,6 +297,134 @@ func phaseBuilder5(name string, flags *pflag.FlagSet) Phase {
297
297
}
298
298
}
299
299
300
+ type argTest struct {
301
+ args cobra.PositionalArgs
302
+ pass []string
303
+ fail []string
304
+ }
305
+
306
+ func phaseBuilder6 (name string , args cobra.PositionalArgs , phases ... Phase ) Phase {
307
+ return Phase {
308
+ Name : name ,
309
+ Short : fmt .Sprintf ("long description for %s ..." , name ),
310
+ Phases : phases ,
311
+ ArgsValidator : args ,
312
+ }
313
+ }
314
+
315
+ // customArgs is a custom cobra.PositionArgs function
316
+ func customArgs (cmd * cobra.Command , args []string ) error {
317
+ for _ , a := range args {
318
+ if a != "qux" {
319
+ return fmt .Errorf ("arg %s does not equal qux" , a )
320
+ }
321
+ }
322
+ return nil
323
+ }
324
+
325
+ func TestBindToCommandArgRequirements (t * testing.T ) {
326
+
327
+ // because cobra.ExactArgs(1) == cobra.ExactArgs(3), it is needed
328
+ // to run test argument sets that both pass and fail to ensure the correct function was set.
329
+ var usecases = []struct {
330
+ name string
331
+ runner Runner
332
+ testCases map [string ]argTest
333
+ cmd * cobra.Command
334
+ }{
335
+ {
336
+ name : "leaf command, no defined args, follow parent" ,
337
+ runner : Runner {
338
+ Phases : []Phase {phaseBuilder ("foo" )},
339
+ },
340
+ testCases : map [string ]argTest {
341
+ "phase foo" : {
342
+ pass : []string {"one" , "two" , "three" },
343
+ fail : []string {"one" , "two" },
344
+ args : cobra .ExactArgs (3 ),
345
+ },
346
+ },
347
+ cmd : & cobra.Command {
348
+ Use : "init" ,
349
+ Args : cobra .ExactArgs (3 ),
350
+ },
351
+ },
352
+ {
353
+ name : "container cmd expect none, custom arg check for leaf" ,
354
+ runner : Runner {
355
+ Phases : []Phase {phaseBuilder6 ("foo" , cobra .NoArgs ,
356
+ phaseBuilder6 ("bar" , cobra .ExactArgs (1 )),
357
+ phaseBuilder6 ("baz" , customArgs ),
358
+ )},
359
+ },
360
+ testCases : map [string ]argTest {
361
+ "phase foo" : {
362
+ pass : []string {},
363
+ fail : []string {"one" },
364
+ args : cobra .NoArgs ,
365
+ },
366
+ "phase foo bar" : {
367
+ pass : []string {"one" },
368
+ fail : []string {"one" , "two" },
369
+ args : cobra .ExactArgs (1 ),
370
+ },
371
+ "phase foo baz" : {
372
+ pass : []string {"qux" },
373
+ fail : []string {"one" },
374
+ args : customArgs ,
375
+ },
376
+ },
377
+ cmd : & cobra.Command {
378
+ Use : "init" ,
379
+ Args : cobra .NoArgs ,
380
+ },
381
+ },
382
+ }
383
+
384
+ for _ , rt := range usecases {
385
+ t .Run (rt .name , func (t * testing.T ) {
386
+
387
+ rt .runner .BindToCommand (rt .cmd )
388
+
389
+ // Checks that cmd gets a new phase subcommand
390
+ phaseCmd := getCmd (rt .cmd , "phase" )
391
+ if phaseCmd == nil {
392
+ t .Error ("cmd didn't have phase subcommand\n " )
393
+ return
394
+ }
395
+
396
+ for c , args := range rt .testCases {
397
+
398
+ cCmd := getCmd (rt .cmd , c )
399
+ if cCmd == nil {
400
+ t .Errorf ("cmd didn't have %s subcommand\n " , c )
401
+ continue
402
+ }
403
+
404
+ // Ensure it is the expected function
405
+ if reflect .ValueOf (cCmd .Args ).Pointer () != reflect .ValueOf (args .args ).Pointer () {
406
+ t .Error ("The function poiners where not equal." )
407
+ }
408
+
409
+ // Test passing argument set
410
+ err := cCmd .Args (cCmd , args .pass )
411
+
412
+ if err != nil {
413
+ t .Errorf ("command %s should validate the args: %v\n %v" , cCmd .Name (), args .pass , err )
414
+ }
415
+
416
+ // Test failing argument set
417
+ err = cCmd .Args (cCmd , args .fail )
418
+
419
+ if err == nil {
420
+ t .Errorf ("command %s should fail to validate the args: %v\n %v" , cCmd .Name (), args .pass , err )
421
+ }
422
+ }
423
+
424
+ })
425
+ }
426
+ }
427
+
300
428
func TestBindToCommand (t * testing.T ) {
301
429
302
430
var dummy string
0 commit comments