args_test.go
This file
Path
args_test.go
323 lines · 8.0 KiB · Go
Last changed by
Add backwards-compatibility tests for legacyArgs() (#1547)Marc Khouzambefore adoption94e552d
History of this fileTake it
1package cobra23import (4 "fmt"5 "strings"6 "testing"7)89func getCommand(args PositionalArgs, withValid bool) *Command {10 c := &Command{11 Use: "c",12 Args: args,13 Run: emptyRun,14 }15 if withValid {16 c.ValidArgs = []string{"one", "two", "three"}17 }18 return c19}2021func expectSuccess(output string, err error, t *testing.T) {22 if output != "" {23 t.Errorf("Unexpected output: %v", output)24 }25 if err != nil {26 t.Fatalf("Unexpected error: %v", err)27 }28}2930func validWithInvalidArgs(err error, t *testing.T) {31 if err == nil {32 t.Fatal("Expected an error")33 }34 got := err.Error()35 expected := `invalid argument "a" for "c"`36 if got != expected {37 t.Errorf("Expected: %q, got: %q", expected, got)38 }39}4041func noArgsWithArgs(err error, t *testing.T) {42 if err == nil {43 t.Fatal("Expected an error")44 }45 got := err.Error()46 expected := `unknown command "illegal" for "c"`47 if got != expected {48 t.Errorf("Expected: %q, got: %q", expected, got)49 }50}5152func minimumNArgsWithLessArgs(err error, t *testing.T) {53 if err == nil {54 t.Fatal("Expected an error")55 }56 got := err.Error()57 expected := "requires at least 2 arg(s), only received 1"58 if got != expected {59 t.Fatalf("Expected %q, got %q", expected, got)60 }61}6263func maximumNArgsWithMoreArgs(err error, t *testing.T) {64 if err == nil {65 t.Fatal("Expected an error")66 }67 got := err.Error()68 expected := "accepts at most 2 arg(s), received 3"69 if got != expected {70 t.Fatalf("Expected %q, got %q", expected, got)71 }72}7374func exactArgsWithInvalidCount(err error, t *testing.T) {75 if err == nil {76 t.Fatal("Expected an error")77 }78 got := err.Error()79 expected := "accepts 2 arg(s), received 3"80 if got != expected {81 t.Fatalf("Expected %q, got %q", expected, got)82 }83}8485func rangeArgsWithInvalidCount(err error, t *testing.T) {86 if err == nil {87 t.Fatal("Expected an error")88 }89 got := err.Error()90 expected := "accepts between 2 and 4 arg(s), received 1"91 if got != expected {92 t.Fatalf("Expected %q, got %q", expected, got)93 }94}9596func TestNoArgs(t *testing.T) {97 c := getCommand(NoArgs, false)98 output, err := executeCommand(c)99 expectSuccess(output, err, t)100}101102func TestNoArgsWithArgs(t *testing.T) {103 c := getCommand(NoArgs, false)104 _, err := executeCommand(c, "illegal")105 noArgsWithArgs(err, t)106}107108func TestOnlyValidArgs(t *testing.T) {109 c := getCommand(OnlyValidArgs, true)110 output, err := executeCommand(c, "one", "two")111 expectSuccess(output, err, t)112}113114func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {115 c := getCommand(OnlyValidArgs, true)116 _, err := executeCommand(c, "a")117 validWithInvalidArgs(err, t)118}119120func TestArbitraryArgs(t *testing.T) {121 c := getCommand(ArbitraryArgs, false)122 output, err := executeCommand(c, "a", "b")123 expectSuccess(output, err, t)124}125126func TestMinimumNArgs(t *testing.T) {127 c := getCommand(MinimumNArgs(2), false)128 output, err := executeCommand(c, "a", "b", "c")129 expectSuccess(output, err, t)130}131132func TestMinimumNArgsWithLessArgs(t *testing.T) {133 c := getCommand(MinimumNArgs(2), false)134 _, err := executeCommand(c, "a")135 minimumNArgsWithLessArgs(err, t)136}137138func TestMaximumNArgs(t *testing.T) {139 c := getCommand(MaximumNArgs(3), false)140 output, err := executeCommand(c, "a", "b")141 expectSuccess(output, err, t)142}143144func TestMaximumNArgsWithMoreArgs(t *testing.T) {145 c := getCommand(MaximumNArgs(2), false)146 _, err := executeCommand(c, "a", "b", "c")147 maximumNArgsWithMoreArgs(err, t)148}149150func TestExactArgs(t *testing.T) {151 c := getCommand(ExactArgs(3), false)152 output, err := executeCommand(c, "a", "b", "c")153 expectSuccess(output, err, t)154}155156func TestExactArgsWithInvalidCount(t *testing.T) {157 c := getCommand(ExactArgs(2), false)158 _, err := executeCommand(c, "a", "b", "c")159 exactArgsWithInvalidCount(err, t)160}161162func TestExactValidArgs(t *testing.T) {163 c := getCommand(ExactValidArgs(3), true)164 output, err := executeCommand(c, "three", "one", "two")165 expectSuccess(output, err, t)166}167168func TestExactValidArgsWithInvalidCount(t *testing.T) {169 c := getCommand(ExactValidArgs(2), false)170 _, err := executeCommand(c, "three", "one", "two")171 exactArgsWithInvalidCount(err, t)172}173174func TestExactValidArgsWithInvalidArgs(t *testing.T) {175 c := getCommand(ExactValidArgs(3), true)176 _, err := executeCommand(c, "three", "a", "two")177 validWithInvalidArgs(err, t)178}179180func TestRangeArgs(t *testing.T) {181 c := getCommand(RangeArgs(2, 4), false)182 output, err := executeCommand(c, "a", "b", "c")183 expectSuccess(output, err, t)184}185186func TestRangeArgsWithInvalidCount(t *testing.T) {187 c := getCommand(RangeArgs(2, 4), false)188 _, err := executeCommand(c, "a")189 rangeArgsWithInvalidCount(err, t)190}191192func TestRootTakesNoArgs(t *testing.T) {193 rootCmd := &Command{Use: "root", Run: emptyRun}194 childCmd := &Command{Use: "child", Run: emptyRun}195 rootCmd.AddCommand(childCmd)196197 _, err := executeCommand(rootCmd, "illegal", "args")198 if err == nil {199 t.Fatal("Expected an error")200 }201202 got := err.Error()203 expected := `unknown command "illegal" for "root"`204 if !strings.Contains(got, expected) {205 t.Errorf("expected %q, got %q", expected, got)206 }207}208209func TestRootTakesArgs(t *testing.T) {210 rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun}211 childCmd := &Command{Use: "child", Run: emptyRun}212 rootCmd.AddCommand(childCmd)213214 _, err := executeCommand(rootCmd, "legal", "args")215 if err != nil {216 t.Errorf("Unexpected error: %v", err)217 }218}219220func TestChildTakesNoArgs(t *testing.T) {221 rootCmd := &Command{Use: "root", Run: emptyRun}222 childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun}223 rootCmd.AddCommand(childCmd)224225 _, err := executeCommand(rootCmd, "child", "illegal", "args")226 if err == nil {227 t.Fatal("Expected an error")228 }229230 got := err.Error()231 expected := `unknown command "illegal" for "root child"`232 if !strings.Contains(got, expected) {233 t.Errorf("expected %q, got %q", expected, got)234 }235}236237func TestChildTakesArgs(t *testing.T) {238 rootCmd := &Command{Use: "root", Run: emptyRun}239 childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun}240 rootCmd.AddCommand(childCmd)241242 _, err := executeCommand(rootCmd, "child", "legal", "args")243 if err != nil {244 t.Fatalf("Unexpected error: %v", err)245 }246}247248func TestMatchAll(t *testing.T) {249 // Somewhat contrived example check that ensures there are exactly 3250 // arguments, and each argument is exactly 2 bytes long.251 pargs := MatchAll(252 ExactArgs(3),253 func(cmd *Command, args []string) error {254 for _, arg := range args {255 if len([]byte(arg)) != 2 {256 return fmt.Errorf("expected to be exactly 2 bytes long")257 }258 }259 return nil260 },261 )262263 testCases := map[string]struct {264 args []string265 fail bool266 }{267 "happy path": {268 []string{"aa", "bb", "cc"},269 false,270 },271 "incorrect number of args": {272 []string{"aa", "bb", "cc", "dd"},273 true,274 },275 "incorrect number of bytes in one arg": {276 []string{"aa", "bb", "abc"},277 true,278 },279 }280281 rootCmd := &Command{Use: "root", Args: pargs, Run: emptyRun}282283 for name, tc := range testCases {284 t.Run(name, func(t *testing.T) {285 _, err := executeCommand(rootCmd, tc.args...)286 if err != nil && !tc.fail {287 t.Errorf("unexpected: %v\n", err)288 }289 if err == nil && tc.fail {290 t.Errorf("expected error")291 }292 })293 }294}295296// This test make sure we keep backwards-compatibility with respect297// to the legacyArgs() function.298// It makes sure the root command accepts arguments if it does not have299// sub-commands.300func TestLegacyArgsRootAcceptsArgs(t *testing.T) {301 rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}302303 _, err := executeCommand(rootCmd, "somearg")304 if err != nil {305 t.Fatalf("Unexpected error: %v", err)306 }307}308309// This test make sure we keep backwards-compatibility with respect310// to the legacyArgs() function.311// It makes sure a sub-command accepts arguments and further sub-commands312func TestLegacyArgsSubcmdAcceptsArgs(t *testing.T) {313 rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}314 childCmd := &Command{Use: "child", Args: nil, Run: emptyRun}315 grandchildCmd := &Command{Use: "grandchild", Args: nil, Run: emptyRun}316 rootCmd.AddCommand(childCmd)317 childCmd.AddCommand(grandchildCmd)318319 _, err := executeCommand(rootCmd, "child", "somearg")320 if err != nil {321 t.Fatalf("Unexpected error: %v", err)322 }323}