tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/engine/cli/cli_test.go (about) 1 package cli 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "testing" 8 ) 9 10 // Tests TODO: 11 // top level help 12 // command help 13 // hidden commands 14 // command aliases 15 // help, examples 16 17 func TestSimpleCommand(t *testing.T) { 18 cmd := &Command{ 19 Usage: "simple", 20 Run: func(ctx *Context, args []string) { 21 fmt.Fprint(ctx, "Hello world") 22 }, 23 } 24 var buf bytes.Buffer 25 ctx := ContextWithIO(context.Background(), nil, &buf, nil) 26 if err := Execute(ctx, cmd, []string{}); err != nil { 27 t.Fatal(err) 28 } 29 if buf.String() != "Hello world" { 30 t.Fatal("unexpected output") 31 } 32 } 33 34 func TestPositionalArgs(t *testing.T) { 35 cmd := &Command{ 36 Usage: "posargs", 37 Args: ExactArgs(2), 38 Run: func(ctx *Context, args []string) {}, 39 } 40 if err := Execute(context.Background(), cmd, []string{"one", "two"}); err != nil { 41 t.Fatal(err) 42 } 43 if err := Execute(context.Background(), cmd, []string{"one", "two", "three"}); err == nil { 44 t.Fatal("expected error") 45 } 46 if err := Execute(context.Background(), cmd, []string{}); err == nil { 47 t.Fatal("expected error") 48 } 49 } 50 51 func TestSubcommands(t *testing.T) { 52 cmd := &Command{ 53 Usage: "subcmds", 54 Run: func(ctx *Context, args []string) { 55 fmt.Fprint(ctx, "root") 56 }, 57 } 58 cmd.AddCommand(&Command{ 59 Usage: "sub1", 60 Run: func(ctx *Context, args []string) { 61 fmt.Fprint(ctx, "sub1") 62 }, 63 }) 64 cmd.AddCommand(&Command{ 65 Usage: "sub2", 66 Run: func(ctx *Context, args []string) { 67 fmt.Fprint(ctx, "sub2") 68 }, 69 }) 70 71 var buf bytes.Buffer 72 ctx := ContextWithIO(context.Background(), nil, &buf, nil) 73 74 if err := Execute(ctx, cmd, []string{}); err != nil { 75 t.Fatal(err) 76 } 77 if buf.String() != "root" { 78 t.Fatal("didn't run root cmd") 79 } 80 81 buf.Reset() 82 if err := Execute(ctx, cmd, []string{"sub1"}); err != nil { 83 t.Fatal(err) 84 } 85 if buf.String() != "sub1" { 86 t.Fatal("didn't run sub1 cmd") 87 } 88 89 buf.Reset() 90 if err := Execute(ctx, cmd, []string{"sub2"}); err != nil { 91 t.Fatal(err) 92 } 93 if buf.String() != "sub2" { 94 t.Fatal("didn't run sub2 cmd") 95 } 96 } 97 98 func TestFlags(t *testing.T) { 99 var ( 100 boolFlag bool 101 stringFlag string 102 intFlag int 103 ) 104 cmd := &Command{ 105 Usage: "flags", 106 Run: func(ctx *Context, args []string) {}, 107 } 108 cmd.Flags().BoolVar(&boolFlag, "b", false, "bool value") 109 cmd.Flags().StringVar(&stringFlag, "string", "", "string value") 110 cmd.Flags().IntVar(&intFlag, "int", 0, "int value") 111 112 ctx := context.Background() 113 if err := Execute(ctx, cmd, []string{"-b=true", "-string", "STRING", "-int=100"}); err != nil { 114 t.Fatal(err) 115 } 116 if boolFlag != true || stringFlag != "STRING" || intFlag != 100 { 117 t.Fatal("unexpected flag value") 118 } 119 } 120 121 func TestVersion(t *testing.T) { 122 cmd := &Command{ 123 Version: "1.0", 124 Usage: "mytest", 125 Run: func(ctx *Context, args []string) {}, 126 } 127 var buf bytes.Buffer 128 ctx := ContextWithIO(context.Background(), nil, &buf, nil) 129 if err := Execute(ctx, cmd, []string{"-v"}); err != nil { 130 t.Fatal(err) 131 } 132 if buf.String() != "1.0\n" { 133 t.Fatal("unexpected output:", buf.String()) 134 } 135 }