@@ -492,6 +492,101 @@ func TestResourceAndPortCompletionFunc(t *testing.T) {
492492 }
493493}
494494
495+ func TestResourceTypeAndNameCompletionFuncResourceList (t * testing.T ) {
496+ // Set up a fake discovery client with some API resources
497+ dc := cmdtesting .NewFakeCachedDiscoveryClient ()
498+ dc .PreferredResources = []* metav1.APIResourceList {
499+ {
500+ GroupVersion : "v1" ,
501+ APIResources : []metav1.APIResource {
502+ {
503+ Name : "pods" ,
504+ Namespaced : true ,
505+ Kind : "Pod" ,
506+ Verbs : []string {"get" , "list" },
507+ },
508+ {
509+ Name : "services" ,
510+ Namespaced : true ,
511+ Kind : "Service" ,
512+ Verbs : []string {"get" , "list" },
513+ },
514+ {
515+ Name : "secrets" ,
516+ Namespaced : true ,
517+ Kind : "Secret" ,
518+ Verbs : []string {"get" , "list" },
519+ },
520+ },
521+ },
522+ {
523+ GroupVersion : "apps/v1" ,
524+ APIResources : []metav1.APIResource {
525+ {
526+ Name : "deployments" ,
527+ Namespaced : true ,
528+ Kind : "Deployment" ,
529+ Verbs : []string {"get" , "list" },
530+ },
531+ },
532+ },
533+ }
534+
535+ testCases := []struct {
536+ name string
537+ args []string
538+ toComplete string
539+ expectedComps []string
540+ expectedDirective cobra.ShellCompDirective
541+ }{
542+ {
543+ name : "complete resources starting with 's'" ,
544+ args : []string {},
545+ toComplete : "s" ,
546+ expectedComps : []string {"secrets" , "services" },
547+ expectedDirective : cobra .ShellCompDirectiveNoFileComp ,
548+ },
549+ {
550+ name : "complete resources starting with 'p'" ,
551+ args : []string {},
552+ toComplete : "p" ,
553+ expectedComps : []string {"pods" },
554+ expectedDirective : cobra .ShellCompDirectiveNoFileComp ,
555+ },
556+ {
557+ name : "complete resources starting with 'd'" ,
558+ args : []string {},
559+ toComplete : "d" ,
560+ expectedComps : []string {"deployments.apps" },
561+ expectedDirective : cobra .ShellCompDirectiveNoFileComp ,
562+ },
563+ {
564+ name : "complete all resources with empty string" ,
565+ args : []string {},
566+ toComplete : "" ,
567+ expectedComps : []string {"deployments.apps" , "pods" , "secrets" , "services" },
568+ expectedDirective : cobra .ShellCompDirectiveNoFileComp ,
569+ },
570+ {
571+ name : "no matches" ,
572+ args : []string {},
573+ toComplete : "xyz" ,
574+ expectedComps : []string {},
575+ expectedDirective : cobra .ShellCompDirectiveNoFileComp ,
576+ },
577+ }
578+
579+ for _ , tc := range testCases {
580+ t .Run (tc .name , func (t * testing.T ) {
581+ tf , cmd := prepareCompletionTest ()
582+ tf .WithDiscoveryClient (dc )
583+ compFunc := ResourceTypeAndNameCompletionFunc (tf )
584+ comps , directive := compFunc (cmd , tc .args , tc .toComplete )
585+ checkCompletion (t , comps , tc .expectedComps , directive , tc .expectedDirective )
586+ })
587+ }
588+ }
589+
495590func setMockFactory (config api.Config ) {
496591 clientConfig := clientcmd .NewDefaultClientConfig (config , nil )
497592 testFactory := cmdtesting .NewTestFactory ().WithClientConfig (clientConfig )
0 commit comments