|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | +) |
| 9 | + |
| 10 | +func TestWrapArgsWithNames_LeavesNilArgsAlone(t *testing.T) { |
| 11 | + root := &cobra.Command{Use: "root"} |
| 12 | + bare := &cobra.Command{Use: "bare"} |
| 13 | + root.AddCommand(bare) |
| 14 | + |
| 15 | + wrapArgsWithNames(root) |
| 16 | + |
| 17 | + if bare.Args != nil { |
| 18 | + t.Fatal("bare command should not be wrapped") |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestWrapArgsWithNames_PassesThroughOnSuccess(t *testing.T) { |
| 23 | + cmd := &cobra.Command{Use: "login <name>", Args: cobra.ExactArgs(1)} |
| 24 | + wrapArgsWithNames(cmd) |
| 25 | + |
| 26 | + if err := cmd.Args(cmd, []string{"x"}); err != nil { |
| 27 | + t.Fatalf("expected nil, got %v", err) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestWrapArgsWithNames_NamesMissingArg(t *testing.T) { |
| 32 | + cmd := &cobra.Command{Use: "login <name>", Args: cobra.ExactArgs(1)} |
| 33 | + wrapArgsWithNames(cmd) |
| 34 | + |
| 35 | + err := cmd.Args(cmd, nil) |
| 36 | + if err == nil { |
| 37 | + t.Fatal("expected error") |
| 38 | + } |
| 39 | + if got, want := err.Error(), `required argument(s) "name" not set`; got != want { |
| 40 | + t.Fatalf("error = %q, want %q", got, want) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestWrapArgsWithNames_NamesMultipleMissingArgs(t *testing.T) { |
| 45 | + cmd := &cobra.Command{Use: "thing <a> <b>", Args: cobra.ExactArgs(2)} |
| 46 | + wrapArgsWithNames(cmd) |
| 47 | + |
| 48 | + err := cmd.Args(cmd, nil) |
| 49 | + if err == nil { |
| 50 | + t.Fatal("expected error") |
| 51 | + } |
| 52 | + if got, want := err.Error(), `required argument(s) "a", "b" not set`; got != want { |
| 53 | + t.Fatalf("error = %q, want %q", got, want) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestWrapArgsWithNames_NamesOnlyTrailingMissingArgs(t *testing.T) { |
| 58 | + cmd := &cobra.Command{Use: "thing <a> <b>", Args: cobra.ExactArgs(2)} |
| 59 | + wrapArgsWithNames(cmd) |
| 60 | + |
| 61 | + err := cmd.Args(cmd, []string{"first"}) |
| 62 | + if err == nil { |
| 63 | + t.Fatal("expected error") |
| 64 | + } |
| 65 | + if got, want := err.Error(), `required argument(s) "b" not set`; got != want { |
| 66 | + t.Fatalf("error = %q, want %q", got, want) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestWrapArgsWithNames_FallsBackOnTooManyArgs(t *testing.T) { |
| 71 | + cmd := &cobra.Command{Use: "login <name>", Args: cobra.ExactArgs(1)} |
| 72 | + wrapArgsWithNames(cmd) |
| 73 | + |
| 74 | + err := cmd.Args(cmd, []string{"a", "b"}) |
| 75 | + if err == nil { |
| 76 | + t.Fatal("expected error") |
| 77 | + } |
| 78 | + if got, want := err.Error(), "accepts 1 arg(s), received 2"; got != want { |
| 79 | + t.Fatalf("error = %q, want %q", got, want) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestWrapArgsWithNames_RecursesIntoChildren(t *testing.T) { |
| 84 | + root := &cobra.Command{Use: "root"} |
| 85 | + parent := &cobra.Command{Use: "parent"} |
| 86 | + grandchild := &cobra.Command{Use: "grandchild <event>", Args: cobra.ExactArgs(1)} |
| 87 | + parent.AddCommand(grandchild) |
| 88 | + root.AddCommand(parent) |
| 89 | + |
| 90 | + wrapArgsWithNames(root) |
| 91 | + |
| 92 | + err := grandchild.Args(grandchild, nil) |
| 93 | + if err == nil { |
| 94 | + t.Fatal("expected error") |
| 95 | + } |
| 96 | + if got, want := err.Error(), `required argument(s) "event" not set`; got != want { |
| 97 | + t.Fatalf("error = %q, want %q", got, want) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestRequiredArgNames(t *testing.T) { |
| 102 | + cases := []struct { |
| 103 | + use string |
| 104 | + want []string |
| 105 | + }{ |
| 106 | + {"login <name>", []string{"name"}}, |
| 107 | + {"thing <a> <b>", []string{"a", "b"}}, |
| 108 | + {"use [name]", nil}, |
| 109 | + {"mixed <id> [extra]", []string{"id"}}, |
| 110 | + {"bare", nil}, |
| 111 | + } |
| 112 | + for _, tc := range cases { |
| 113 | + got := requiredArgNames(tc.use) |
| 114 | + if !reflect.DeepEqual(got, tc.want) && !(len(got) == 0 && len(tc.want) == 0) { |
| 115 | + t.Errorf("requiredArgNames(%q) = %v, want %v", tc.use, got, tc.want) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments