1package cobra 2 3import ( 4 "strings" 5 "testing" 6) 7 8func TestNoArgs(t *testing.T) { 9 c := &Command{Use: "c", Args: NoArgs, Run: emptyRun} 10 11 output, err := executeCommand(c) 12 if output != "" { 13 t.Errorf("Unexpected string: %v", output) 14 } 15 if err != nil { 16 t.Fatalf("Unexpected error: %v", err) 17 } 18} 19 20func TestNoArgsWithArgs(t *testing.T) { 21 c := &Command{Use: "c", Args: NoArgs, Run: emptyRun} 22 23 _, err := executeCommand(c, "illegal") 24 if err == nil { 25 t.Fatal("Expected an error") 26 } 27 28 got := err.Error() 29 expected := `unknown command "illegal" for "c"` 30 if got != expected { 31 t.Errorf("Expected: %q, got: %q", expected, got) 32 } 33} 34 35func TestOnlyValidArgs(t *testing.T) { 36 c := &Command{ 37 Use: "c", 38 Args: OnlyValidArgs, 39 ValidArgs: []string{"one", "two"}, 40 Run: emptyRun, 41 } 42 43 output, err := executeCommand(c, "one", "two") 44 if output != "" { 45 t.Errorf("Unexpected output: %v", output) 46 } 47 if err != nil { 48 t.Fatalf("Unexpected error: %v", err) 49 } 50} 51 52func TestOnlyValidArgsWithInvalidArgs(t *testing.T) { 53 c := &Command{ 54 Use: "c", 55 Args: OnlyValidArgs, 56 ValidArgs: []string{"one", "two"}, 57 Run: emptyRun, 58 } 59 60 _, err := executeCommand(c, "three") 61 if err == nil { 62 t.Fatal("Expected an error") 63 } 64 65 got := err.Error() 66 expected := `invalid argument "three" for "c"` 67 if got != expected { 68 t.Errorf("Expected: %q, got: %q", expected, got) 69 } 70} 71 72func TestArbitraryArgs(t *testing.T) { 73 c := &Command{Use: "c", Args: ArbitraryArgs, Run: emptyRun} 74 output, err := executeCommand(c, "a", "b") 75 if output != "" { 76 t.Errorf("Unexpected output: %v", output) 77 } 78 if err != nil { 79 t.Errorf("Unexpected error: %v", err) 80 } 81} 82 83func TestMinimumNArgs(t *testing.T) { 84 c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun} 85 output, err := executeCommand(c, "a", "b", "c") 86 if output != "" { 87 t.Errorf("Unexpected output: %v", output) 88 } 89 if err != nil { 90 t.Errorf("Unexpected error: %v", err) 91 } 92} 93 94func TestMinimumNArgsWithLessArgs(t *testing.T) { 95 c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun} 96 _, err := executeCommand(c, "a") 97 98 if err == nil { 99 t.Fatal("Expected an error") 100 } 101 102 got := err.Error() 103 expected := "requires at least 2 arg(s), only received 1" 104 if got != expected { 105 t.Fatalf("Expected %q, got %q", expected, got) 106 } 107} 108 109func TestMaximumNArgs(t *testing.T) { 110 c := &Command{Use: "c", Args: MaximumNArgs(3), Run: emptyRun} 111 output, err := executeCommand(c, "a", "b") 112 if output != "" { 113 t.Errorf("Unexpected output: %v", output) 114 } 115 if err != nil { 116 t.Errorf("Unexpected error: %v", err) 117 } 118} 119 120func TestMaximumNArgsWithMoreArgs(t *testing.T) { 121 c := &Command{Use: "c", Args: MaximumNArgs(2), Run: emptyRun} 122 _, err := executeCommand(c, "a", "b", "c") 123 124 if err == nil { 125 t.Fatal("Expected an error") 126 } 127 128 got := err.Error() 129 expected := "accepts at most 2 arg(s), received 3" 130 if got != expected { 131 t.Fatalf("Expected %q, got %q", expected, got) 132 } 133} 134 135func TestExactArgs(t *testing.T) { 136 c := &Command{Use: "c", Args: ExactArgs(3), Run: emptyRun} 137 output, err := executeCommand(c, "a", "b", "c") 138 if output != "" { 139 t.Errorf("Unexpected output: %v", output) 140 } 141 if err != nil { 142 t.Errorf("Unexpected error: %v", err) 143 } 144} 145 146func TestExactArgsWithInvalidCount(t *testing.T) { 147 c := &Command{Use: "c", Args: ExactArgs(2), Run: emptyRun} 148 _, err := executeCommand(c, "a", "b", "c") 149 150 if err == nil { 151 t.Fatal("Expected an error") 152 } 153 154 got := err.Error() 155 expected := "accepts 2 arg(s), received 3" 156 if got != expected { 157 t.Fatalf("Expected %q, got %q", expected, got) 158 } 159} 160 161func TestExactValidArgs(t *testing.T) { 162 c := &Command{Use: "c", Args: ExactValidArgs(3), ValidArgs: []string{"a", "b", "c"}, Run: emptyRun} 163 output, err := executeCommand(c, "a", "b", "c") 164 if output != "" { 165 t.Errorf("Unexpected output: %v", output) 166 } 167 if err != nil { 168 t.Errorf("Unexpected error: %v", err) 169 } 170} 171 172func TestExactValidArgsWithInvalidCount(t *testing.T) { 173 c := &Command{Use: "c", Args: ExactValidArgs(2), Run: emptyRun} 174 _, err := executeCommand(c, "a", "b", "c") 175 176 if err == nil { 177 t.Fatal("Expected an error") 178 } 179 180 got := err.Error() 181 expected := "accepts 2 arg(s), received 3" 182 if got != expected { 183 t.Fatalf("Expected %q, got %q", expected, got) 184 } 185} 186 187func TestExactValidArgsWithInvalidArgs(t *testing.T) { 188 c := &Command{ 189 Use: "c", 190 Args: ExactValidArgs(1), 191 ValidArgs: []string{"one", "two"}, 192 Run: emptyRun, 193 } 194 195 _, err := executeCommand(c, "three") 196 if err == nil { 197 t.Fatal("Expected an error") 198 } 199 200 got := err.Error() 201 expected := `invalid argument "three" for "c"` 202 if got != expected { 203 t.Errorf("Expected: %q, got: %q", expected, got) 204 } 205} 206 207func TestRangeArgs(t *testing.T) { 208 c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun} 209 output, err := executeCommand(c, "a", "b", "c") 210 if output != "" { 211 t.Errorf("Unexpected output: %v", output) 212 } 213 if err != nil { 214 t.Errorf("Unexpected error: %v", err) 215 } 216} 217 218func TestRangeArgsWithInvalidCount(t *testing.T) { 219 c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun} 220 _, err := executeCommand(c, "a") 221 222 if err == nil { 223 t.Fatal("Expected an error") 224 } 225 226 got := err.Error() 227 expected := "accepts between 2 and 4 arg(s), received 1" 228 if got != expected { 229 t.Fatalf("Expected %q, got %q", expected, got) 230 } 231} 232 233func TestRootTakesNoArgs(t *testing.T) { 234 rootCmd := &Command{Use: "root", Run: emptyRun} 235 childCmd := &Command{Use: "child", Run: emptyRun} 236 rootCmd.AddCommand(childCmd) 237 238 _, err := executeCommand(rootCmd, "illegal", "args") 239 if err == nil { 240 t.Fatal("Expected an error") 241 } 242 243 got := err.Error() 244 expected := `unknown command "illegal" for "root"` 245 if !strings.Contains(got, expected) { 246 t.Errorf("expected %q, got %q", expected, got) 247 } 248} 249 250func TestRootTakesArgs(t *testing.T) { 251 rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun} 252 childCmd := &Command{Use: "child", Run: emptyRun} 253 rootCmd.AddCommand(childCmd) 254 255 _, err := executeCommand(rootCmd, "legal", "args") 256 if err != nil { 257 t.Errorf("Unexpected error: %v", err) 258 } 259} 260 261func TestChildTakesNoArgs(t *testing.T) { 262 rootCmd := &Command{Use: "root", Run: emptyRun} 263 childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun} 264 rootCmd.AddCommand(childCmd) 265 266 _, err := executeCommand(rootCmd, "child", "illegal", "args") 267 if err == nil { 268 t.Fatal("Expected an error") 269 } 270 271 got := err.Error() 272 expected := `unknown command "illegal" for "root child"` 273 if !strings.Contains(got, expected) { 274 t.Errorf("expected %q, got %q", expected, got) 275 } 276} 277 278func TestChildTakesArgs(t *testing.T) { 279 rootCmd := &Command{Use: "root", Run: emptyRun} 280 childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun} 281 rootCmd.AddCommand(childCmd) 282 283 _, err := executeCommand(rootCmd, "child", "legal", "args") 284 if err != nil { 285 t.Fatalf("Unexpected error: %v", err) 286 } 287} 288