@@ -96,3 +96,74 @@ func TestShortcutMount_FlagCompletionsDisabled(t *testing.T) {
9696 t .Fatal ("did not expect completion func for --format when disabled" )
9797 }
9898}
99+
100+ func TestShortcutMount_JsonFlag_AcceptedWhenHasFormat (t * testing.T ) {
101+ f , _ , _ , _ := cmdutil .TestFactory (t , nil )
102+ parent := & cobra.Command {Use : "root" }
103+ shortcut := Shortcut {
104+ Service : "test" ,
105+ Command : "+read" ,
106+ Description : "test read" ,
107+ HasFormat : true ,
108+ Execute : func (context.Context , * RuntimeContext ) error { return nil },
109+ }
110+ shortcut .Mount (parent , f )
111+
112+ cmd , _ , err := parent .Find ([]string {"+read" })
113+ if err != nil {
114+ t .Fatalf ("Find() error = %v" , err )
115+ }
116+ if flag := cmd .Flags ().Lookup ("json" ); flag == nil {
117+ t .Fatal ("expected --json flag to be registered on HasFormat shortcut" )
118+ }
119+ }
120+
121+ func TestShortcutMount_JsonFlag_SkippedWhenConflict (t * testing.T ) {
122+ f , _ , _ , _ := cmdutil .TestFactory (t , nil )
123+ parent := & cobra.Command {Use : "root" }
124+ shortcut := Shortcut {
125+ Service : "test" ,
126+ Command : "+update" ,
127+ Description : "test update" ,
128+ HasFormat : true ,
129+ Flags : []Flag {
130+ {Name : "json" , Desc : "body JSON object" , Required : true },
131+ },
132+ Execute : func (context.Context , * RuntimeContext ) error { return nil },
133+ }
134+ shortcut .Mount (parent , f )
135+
136+ cmd , _ , err := parent .Find ([]string {"+update" })
137+ if err != nil {
138+ t .Fatalf ("Find() error = %v" , err )
139+ }
140+ // --json flag exists (from custom Flags), but should be the string type, not bool.
141+ flag := cmd .Flags ().Lookup ("json" )
142+ if flag == nil {
143+ t .Fatal ("expected --json flag from custom Flags" )
144+ }
145+ if flag .DefValue != "" {
146+ t .Errorf ("expected empty default (string flag), got %q" , flag .DefValue )
147+ }
148+ }
149+
150+ func TestShortcutMount_JsonFlag_AbsentWithoutHasFormat (t * testing.T ) {
151+ f , _ , _ , _ := cmdutil .TestFactory (t , nil )
152+ parent := & cobra.Command {Use : "root" }
153+ shortcut := Shortcut {
154+ Service : "test" ,
155+ Command : "+write" ,
156+ Description : "test write" ,
157+ HasFormat : false ,
158+ Execute : func (context.Context , * RuntimeContext ) error { return nil },
159+ }
160+ shortcut .Mount (parent , f )
161+
162+ cmd , _ , err := parent .Find ([]string {"+write" })
163+ if err != nil {
164+ t .Fatalf ("Find() error = %v" , err )
165+ }
166+ if flag := cmd .Flags ().Lookup ("json" ); flag != nil {
167+ t .Fatal ("--json flag should not be registered when HasFormat is false" )
168+ }
169+ }
0 commit comments