@@ -23,7 +23,6 @@ import (
2323
2424 "github.com/google/go-cmp/cmp"
2525 "github.com/google/go-cmp/cmp/cmpopts"
26- "github.com/googleapis/librarian/internal/config"
2726)
2827
2928func TestParseAndSetFlags (t * testing.T ) {
@@ -42,7 +41,7 @@ func TestParseAndSetFlags(t *testing.T) {
4241 cmd .Flags .IntVar (& intFlag , "count" , 0 , "count flag" )
4342
4443 args := []string {"-name=foo" , "-count=5" }
45- if err := cmd .Parse (args ); err != nil {
44+ if err := cmd .Flags . Parse (args ); err != nil {
4645 t .Fatalf ("Parse() failed: %v" , err )
4746 }
4847
@@ -54,22 +53,21 @@ func TestParseAndSetFlags(t *testing.T) {
5453 }
5554}
5655
57- func TestRun (t * testing.T ) {
56+ func TestAction (t * testing.T ) {
5857 executed := false
5958 cmd := & Command {
6059 Short : "run runs the command" ,
61- Run : func (ctx context.Context , cfg * config. Config ) error {
60+ Action : func (ctx context.Context , cmd * Command ) error {
6261 executed = true
6362 return nil
6463 },
6564 }
6665
67- cfg := & config.Config {}
68- if err := cmd .Run (t .Context (), cfg ); err != nil {
66+ if err := cmd .Action (t .Context (), cmd ); err != nil {
6967 t .Fatal (err )
7068 }
7169 if ! executed {
72- t .Errorf ("cmd.Run was not executed" )
70+ t .Errorf ("cmd.Action was not executed" )
7371 }
7472}
7573
@@ -278,7 +276,7 @@ func TestLookupCommand(t *testing.T) {
278276 },
279277 } {
280278 t .Run (test .name , func (t * testing.T ) {
281- gotCmd , gotArgs , err := LookupCommand (test .cmd , test .args )
279+ gotCmd , gotArgs , err := lookupCommand (test .cmd , test .args )
282280 if (err != nil ) != test .wantErr {
283281 t .Fatalf ("error = %v, wantErr %v" , err , test .wantErr )
284282 }
@@ -298,3 +296,77 @@ func TestLookupCommand(t *testing.T) {
298296 })
299297 }
300298}
299+
300+ func TestRun (t * testing.T ) {
301+ actionExecuted := false
302+ subcmd := & Command {
303+ Short : "bar is a subcommand" ,
304+ Long : "bar is a subcommand." ,
305+ UsageLine : "bar" ,
306+ Action : func (ctx context.Context , cmd * Command ) error {
307+ actionExecuted = true
308+ return nil
309+ },
310+ }
311+ subcmd .Init ()
312+
313+ root := & Command {
314+ Short : "foo is the root command" ,
315+ Long : "foo is the root command." ,
316+ UsageLine : "foo" ,
317+ Commands : []* Command {subcmd },
318+ }
319+ root .Init ()
320+
321+ noaction := & Command {
322+ Short : "noaction has no action" ,
323+ Long : "noaction has no action." ,
324+ UsageLine : "noaction" ,
325+ }
326+ noaction .Init ()
327+
328+ for _ , test := range []struct {
329+ name string
330+ cmd * Command
331+ args []string
332+ wantErr bool
333+ actionExecuted bool
334+ }{
335+ {
336+ name : "execute foo with subcommand bar" ,
337+ cmd : root ,
338+ args : []string {"bar" },
339+ actionExecuted : true ,
340+ },
341+ {
342+ name : "unknown subcommand" ,
343+ cmd : root ,
344+ args : []string {"unknown" },
345+ wantErr : true ,
346+ },
347+ {
348+ name : "flag parse error" ,
349+ cmd : subcmd ,
350+ args : []string {"-unknown" },
351+ wantErr : true ,
352+ },
353+ {
354+ name : "no action defined on command with no subcommands" ,
355+ cmd : noaction ,
356+ wantErr : true ,
357+ },
358+ } {
359+ t .Run (test .name , func (t * testing.T ) {
360+ actionExecuted = false
361+ err := test .cmd .Run (t .Context (), test .args )
362+ if err != nil {
363+ if ! test .wantErr {
364+ t .Errorf ("error = %v, wantErr %v" , err , test .wantErr )
365+ }
366+ }
367+ if actionExecuted != test .actionExecuted {
368+ t .Errorf ("actionExecuted = %v, want %v" , actionExecuted , test .actionExecuted )
369+ }
370+ })
371+ }
372+ }
0 commit comments