123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- package cobra
- import (
- "strings"
- "testing"
- )
- func TestNoArgs(t *testing.T) {
- c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}
- output, err := executeCommand(c)
- if output != "" {
- t.Errorf("Unexpected string: %v", output)
- }
- if err != nil {
- t.Fatalf("Unexpected error: %v", err)
- }
- }
- func TestNoArgsWithArgs(t *testing.T) {
- c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}
- _, err := executeCommand(c, "illegal")
- if err == nil {
- t.Fatal("Expected an error")
- }
- got := err.Error()
- expected := `unknown command "illegal" for "c"`
- if got != expected {
- t.Errorf("Expected: %q, got: %q", expected, got)
- }
- }
- func TestOnlyValidArgs(t *testing.T) {
- c := &Command{
- Use: "c",
- Args: OnlyValidArgs,
- ValidArgs: []string{"one", "two"},
- Run: emptyRun,
- }
- output, err := executeCommand(c, "one", "two")
- if output != "" {
- t.Errorf("Unexpected output: %v", output)
- }
- if err != nil {
- t.Fatalf("Unexpected error: %v", err)
- }
- }
- func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {
- c := &Command{
- Use: "c",
- Args: OnlyValidArgs,
- ValidArgs: []string{"one", "two"},
- Run: emptyRun,
- }
- _, err := executeCommand(c, "three")
- if err == nil {
- t.Fatal("Expected an error")
- }
- got := err.Error()
- expected := `invalid argument "three" for "c"`
- if got != expected {
- t.Errorf("Expected: %q, got: %q", expected, got)
- }
- }
- func TestArbitraryArgs(t *testing.T) {
- c := &Command{Use: "c", Args: ArbitraryArgs, Run: emptyRun}
- output, err := executeCommand(c, "a", "b")
- if output != "" {
- t.Errorf("Unexpected output: %v", output)
- }
- if err != nil {
- t.Errorf("Unexpected error: %v", err)
- }
- }
- func TestMinimumNArgs(t *testing.T) {
- c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}
- output, err := executeCommand(c, "a", "b", "c")
- if output != "" {
- t.Errorf("Unexpected output: %v", output)
- }
- if err != nil {
- t.Errorf("Unexpected error: %v", err)
- }
- }
- func TestMinimumNArgsWithLessArgs(t *testing.T) {
- c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}
- _, err := executeCommand(c, "a")
- if err == nil {
- t.Fatal("Expected an error")
- }
- got := err.Error()
- expected := "requires at least 2 arg(s), only received 1"
- if got != expected {
- t.Fatalf("Expected %q, got %q", expected, got)
- }
- }
- func TestMaximumNArgs(t *testing.T) {
- c := &Command{Use: "c", Args: MaximumNArgs(3), Run: emptyRun}
- output, err := executeCommand(c, "a", "b")
- if output != "" {
- t.Errorf("Unexpected output: %v", output)
- }
- if err != nil {
- t.Errorf("Unexpected error: %v", err)
- }
- }
- func TestMaximumNArgsWithMoreArgs(t *testing.T) {
- c := &Command{Use: "c", Args: MaximumNArgs(2), Run: emptyRun}
- _, err := executeCommand(c, "a", "b", "c")
- if err == nil {
- t.Fatal("Expected an error")
- }
- got := err.Error()
- expected := "accepts at most 2 arg(s), received 3"
- if got != expected {
- t.Fatalf("Expected %q, got %q", expected, got)
- }
- }
- func TestExactArgs(t *testing.T) {
- c := &Command{Use: "c", Args: ExactArgs(3), Run: emptyRun}
- output, err := executeCommand(c, "a", "b", "c")
- if output != "" {
- t.Errorf("Unexpected output: %v", output)
- }
- if err != nil {
- t.Errorf("Unexpected error: %v", err)
- }
- }
- func TestExactArgsWithInvalidCount(t *testing.T) {
- c := &Command{Use: "c", Args: ExactArgs(2), Run: emptyRun}
- _, err := executeCommand(c, "a", "b", "c")
- if err == nil {
- t.Fatal("Expected an error")
- }
- got := err.Error()
- expected := "accepts 2 arg(s), received 3"
- if got != expected {
- t.Fatalf("Expected %q, got %q", expected, got)
- }
- }
- func TestRangeArgs(t *testing.T) {
- c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}
- output, err := executeCommand(c, "a", "b", "c")
- if output != "" {
- t.Errorf("Unexpected output: %v", output)
- }
- if err != nil {
- t.Errorf("Unexpected error: %v", err)
- }
- }
- func TestRangeArgsWithInvalidCount(t *testing.T) {
- c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}
- _, err := executeCommand(c, "a")
- if err == nil {
- t.Fatal("Expected an error")
- }
- got := err.Error()
- expected := "accepts between 2 and 4 arg(s), received 1"
- if got != expected {
- t.Fatalf("Expected %q, got %q", expected, got)
- }
- }
- func TestRootTakesNoArgs(t *testing.T) {
- rootCmd := &Command{Use: "root", Run: emptyRun}
- childCmd := &Command{Use: "child", Run: emptyRun}
- rootCmd.AddCommand(childCmd)
- _, err := executeCommand(rootCmd, "illegal", "args")
- if err == nil {
- t.Fatal("Expected an error")
- }
- got := err.Error()
- expected := `unknown command "illegal" for "root"`
- if !strings.Contains(got, expected) {
- t.Errorf("expected %q, got %q", expected, got)
- }
- }
- func TestRootTakesArgs(t *testing.T) {
- rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun}
- childCmd := &Command{Use: "child", Run: emptyRun}
- rootCmd.AddCommand(childCmd)
- _, err := executeCommand(rootCmd, "legal", "args")
- if err != nil {
- t.Errorf("Unexpected error: %v", err)
- }
- }
- func TestChildTakesNoArgs(t *testing.T) {
- rootCmd := &Command{Use: "root", Run: emptyRun}
- childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun}
- rootCmd.AddCommand(childCmd)
- _, err := executeCommand(rootCmd, "child", "illegal", "args")
- if err == nil {
- t.Fatal("Expected an error")
- }
- got := err.Error()
- expected := `unknown command "illegal" for "root child"`
- if !strings.Contains(got, expected) {
- t.Errorf("expected %q, got %q", expected, got)
- }
- }
- func TestChildTakesArgs(t *testing.T) {
- rootCmd := &Command{Use: "root", Run: emptyRun}
- childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun}
- rootCmd.AddCommand(childCmd)
- _, err := executeCommand(rootCmd, "child", "legal", "args")
- if err != nil {
- t.Fatalf("Unexpected error: %v", err)
- }
- }
|