args_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package cobra
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestNoArgs(t *testing.T) {
  7. c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}
  8. output, err := executeCommand(c)
  9. if output != "" {
  10. t.Errorf("Unexpected string: %v", output)
  11. }
  12. if err != nil {
  13. t.Fatalf("Unexpected error: %v", err)
  14. }
  15. }
  16. func TestNoArgsWithArgs(t *testing.T) {
  17. c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}
  18. _, err := executeCommand(c, "illegal")
  19. if err == nil {
  20. t.Fatal("Expected an error")
  21. }
  22. got := err.Error()
  23. expected := `unknown command "illegal" for "c"`
  24. if got != expected {
  25. t.Errorf("Expected: %q, got: %q", expected, got)
  26. }
  27. }
  28. func TestOnlyValidArgs(t *testing.T) {
  29. c := &Command{
  30. Use: "c",
  31. Args: OnlyValidArgs,
  32. ValidArgs: []string{"one", "two"},
  33. Run: emptyRun,
  34. }
  35. output, err := executeCommand(c, "one", "two")
  36. if output != "" {
  37. t.Errorf("Unexpected output: %v", output)
  38. }
  39. if err != nil {
  40. t.Fatalf("Unexpected error: %v", err)
  41. }
  42. }
  43. func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {
  44. c := &Command{
  45. Use: "c",
  46. Args: OnlyValidArgs,
  47. ValidArgs: []string{"one", "two"},
  48. Run: emptyRun,
  49. }
  50. _, err := executeCommand(c, "three")
  51. if err == nil {
  52. t.Fatal("Expected an error")
  53. }
  54. got := err.Error()
  55. expected := `invalid argument "three" for "c"`
  56. if got != expected {
  57. t.Errorf("Expected: %q, got: %q", expected, got)
  58. }
  59. }
  60. func TestArbitraryArgs(t *testing.T) {
  61. c := &Command{Use: "c", Args: ArbitraryArgs, Run: emptyRun}
  62. output, err := executeCommand(c, "a", "b")
  63. if output != "" {
  64. t.Errorf("Unexpected output: %v", output)
  65. }
  66. if err != nil {
  67. t.Errorf("Unexpected error: %v", err)
  68. }
  69. }
  70. func TestMinimumNArgs(t *testing.T) {
  71. c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}
  72. output, err := executeCommand(c, "a", "b", "c")
  73. if output != "" {
  74. t.Errorf("Unexpected output: %v", output)
  75. }
  76. if err != nil {
  77. t.Errorf("Unexpected error: %v", err)
  78. }
  79. }
  80. func TestMinimumNArgsWithLessArgs(t *testing.T) {
  81. c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}
  82. _, err := executeCommand(c, "a")
  83. if err == nil {
  84. t.Fatal("Expected an error")
  85. }
  86. got := err.Error()
  87. expected := "requires at least 2 arg(s), only received 1"
  88. if got != expected {
  89. t.Fatalf("Expected %q, got %q", expected, got)
  90. }
  91. }
  92. func TestMaximumNArgs(t *testing.T) {
  93. c := &Command{Use: "c", Args: MaximumNArgs(3), Run: emptyRun}
  94. output, err := executeCommand(c, "a", "b")
  95. if output != "" {
  96. t.Errorf("Unexpected output: %v", output)
  97. }
  98. if err != nil {
  99. t.Errorf("Unexpected error: %v", err)
  100. }
  101. }
  102. func TestMaximumNArgsWithMoreArgs(t *testing.T) {
  103. c := &Command{Use: "c", Args: MaximumNArgs(2), Run: emptyRun}
  104. _, err := executeCommand(c, "a", "b", "c")
  105. if err == nil {
  106. t.Fatal("Expected an error")
  107. }
  108. got := err.Error()
  109. expected := "accepts at most 2 arg(s), received 3"
  110. if got != expected {
  111. t.Fatalf("Expected %q, got %q", expected, got)
  112. }
  113. }
  114. func TestExactArgs(t *testing.T) {
  115. c := &Command{Use: "c", Args: ExactArgs(3), Run: emptyRun}
  116. output, err := executeCommand(c, "a", "b", "c")
  117. if output != "" {
  118. t.Errorf("Unexpected output: %v", output)
  119. }
  120. if err != nil {
  121. t.Errorf("Unexpected error: %v", err)
  122. }
  123. }
  124. func TestExactArgsWithInvalidCount(t *testing.T) {
  125. c := &Command{Use: "c", Args: ExactArgs(2), Run: emptyRun}
  126. _, err := executeCommand(c, "a", "b", "c")
  127. if err == nil {
  128. t.Fatal("Expected an error")
  129. }
  130. got := err.Error()
  131. expected := "accepts 2 arg(s), received 3"
  132. if got != expected {
  133. t.Fatalf("Expected %q, got %q", expected, got)
  134. }
  135. }
  136. func TestRangeArgs(t *testing.T) {
  137. c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}
  138. output, err := executeCommand(c, "a", "b", "c")
  139. if output != "" {
  140. t.Errorf("Unexpected output: %v", output)
  141. }
  142. if err != nil {
  143. t.Errorf("Unexpected error: %v", err)
  144. }
  145. }
  146. func TestRangeArgsWithInvalidCount(t *testing.T) {
  147. c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}
  148. _, err := executeCommand(c, "a")
  149. if err == nil {
  150. t.Fatal("Expected an error")
  151. }
  152. got := err.Error()
  153. expected := "accepts between 2 and 4 arg(s), received 1"
  154. if got != expected {
  155. t.Fatalf("Expected %q, got %q", expected, got)
  156. }
  157. }
  158. func TestRootTakesNoArgs(t *testing.T) {
  159. rootCmd := &Command{Use: "root", Run: emptyRun}
  160. childCmd := &Command{Use: "child", Run: emptyRun}
  161. rootCmd.AddCommand(childCmd)
  162. _, err := executeCommand(rootCmd, "illegal", "args")
  163. if err == nil {
  164. t.Fatal("Expected an error")
  165. }
  166. got := err.Error()
  167. expected := `unknown command "illegal" for "root"`
  168. if !strings.Contains(got, expected) {
  169. t.Errorf("expected %q, got %q", expected, got)
  170. }
  171. }
  172. func TestRootTakesArgs(t *testing.T) {
  173. rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun}
  174. childCmd := &Command{Use: "child", Run: emptyRun}
  175. rootCmd.AddCommand(childCmd)
  176. _, err := executeCommand(rootCmd, "legal", "args")
  177. if err != nil {
  178. t.Errorf("Unexpected error: %v", err)
  179. }
  180. }
  181. func TestChildTakesNoArgs(t *testing.T) {
  182. rootCmd := &Command{Use: "root", Run: emptyRun}
  183. childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun}
  184. rootCmd.AddCommand(childCmd)
  185. _, err := executeCommand(rootCmd, "child", "illegal", "args")
  186. if err == nil {
  187. t.Fatal("Expected an error")
  188. }
  189. got := err.Error()
  190. expected := `unknown command "illegal" for "root child"`
  191. if !strings.Contains(got, expected) {
  192. t.Errorf("expected %q, got %q", expected, got)
  193. }
  194. }
  195. func TestChildTakesArgs(t *testing.T) {
  196. rootCmd := &Command{Use: "root", Run: emptyRun}
  197. childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun}
  198. rootCmd.AddCommand(childCmd)
  199. _, err := executeCommand(rootCmd, "child", "legal", "args")
  200. if err != nil {
  201. t.Fatalf("Unexpected error: %v", err)
  202. }
  203. }