2017-10-31 11:57:17 -07:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-11-16 15:20:18 -07:00
|
|
|
func getCommand(args PositionalArgs, withValid bool) *Command {
|
|
|
|
c := &Command{
|
|
|
|
Use: "c",
|
|
|
|
Args: args,
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
if withValid {
|
|
|
|
c.ValidArgs = []string{"one", "two", "three"}
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2017-10-31 11:57:17 -07:00
|
|
|
|
2021-11-16 15:20:18 -07:00
|
|
|
func expectSuccess(output string, err error, t *testing.T) {
|
2017-10-31 11:57:17 -07:00
|
|
|
if output != "" {
|
2021-11-16 15:20:18 -07:00
|
|
|
t.Errorf("Unexpected output: %v", output)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:20:18 -07:00
|
|
|
func validWithInvalidArgs(err error, t *testing.T) {
|
2017-10-31 11:57:17 -07:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
|
|
|
}
|
2021-11-16 15:20:18 -07:00
|
|
|
got := err.Error()
|
|
|
|
expected := `invalid argument "a" for "c"`
|
|
|
|
if got != expected {
|
|
|
|
t.Errorf("Expected: %q, got: %q", expected, got)
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 11:57:17 -07:00
|
|
|
|
2021-11-16 15:20:18 -07:00
|
|
|
func noArgsWithArgs(err error, t *testing.T) {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
|
|
|
}
|
2017-10-31 11:57:17 -07:00
|
|
|
got := err.Error()
|
|
|
|
expected := `unknown command "illegal" for "c"`
|
|
|
|
if got != expected {
|
|
|
|
t.Errorf("Expected: %q, got: %q", expected, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:20:18 -07:00
|
|
|
func minimumNArgsWithLessArgs(err error, t *testing.T) {
|
|
|
|
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)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
2021-11-16 15:20:18 -07:00
|
|
|
}
|
2017-10-31 11:57:17 -07:00
|
|
|
|
2021-11-16 15:20:18 -07:00
|
|
|
func maximumNArgsWithMoreArgs(err error, t *testing.T) {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
2021-11-16 15:20:18 -07:00
|
|
|
got := err.Error()
|
|
|
|
expected := "accepts at most 2 arg(s), received 3"
|
|
|
|
if got != expected {
|
|
|
|
t.Fatalf("Expected %q, got %q", expected, got)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:20:18 -07:00
|
|
|
func exactArgsWithInvalidCount(err error, t *testing.T) {
|
|
|
|
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)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
2021-11-16 15:20:18 -07:00
|
|
|
}
|
2017-10-31 11:57:17 -07:00
|
|
|
|
2021-11-16 15:20:18 -07:00
|
|
|
func rangeArgsWithInvalidCount(err error, t *testing.T) {
|
2017-10-31 11:57:17 -07:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error")
|
|
|
|
}
|
|
|
|
got := err.Error()
|
2021-11-16 15:20:18 -07:00
|
|
|
expected := "accepts between 2 and 4 arg(s), received 1"
|
2017-10-31 11:57:17 -07:00
|
|
|
if got != expected {
|
2021-11-16 15:20:18 -07:00
|
|
|
t.Fatalf("Expected %q, got %q", expected, got)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:20:18 -07:00
|
|
|
func TestNoArgs(t *testing.T) {
|
|
|
|
c := getCommand(NoArgs, false)
|
|
|
|
output, err := executeCommand(c)
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoArgsWithArgs(t *testing.T) {
|
|
|
|
c := getCommand(NoArgs, false)
|
|
|
|
_, err := executeCommand(c, "illegal")
|
|
|
|
noArgsWithArgs(err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnlyValidArgs(t *testing.T) {
|
|
|
|
c := getCommand(OnlyValidArgs, true)
|
|
|
|
output, err := executeCommand(c, "one", "two")
|
|
|
|
expectSuccess(output, err, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {
|
|
|
|
c := getCommand(OnlyValidArgs, true)
|
|
|
|
_, err := executeCommand(c, "a")
|
|
|
|
validWithInvalidArgs(err, t)
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:57:17 -07:00
|
|
|
func TestArbitraryArgs(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(ArbitraryArgs, false)
|
2017-10-31 11:57:17 -07:00
|
|
|
output, err := executeCommand(c, "a", "b")
|
2021-11-16 15:20:18 -07:00
|
|
|
expectSuccess(output, err, t)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinimumNArgs(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(MinimumNArgs(2), false)
|
2017-10-31 11:57:17 -07:00
|
|
|
output, err := executeCommand(c, "a", "b", "c")
|
2021-11-16 15:20:18 -07:00
|
|
|
expectSuccess(output, err, t)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinimumNArgsWithLessArgs(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(MinimumNArgs(2), false)
|
2017-10-31 11:57:17 -07:00
|
|
|
_, err := executeCommand(c, "a")
|
2021-11-16 15:20:18 -07:00
|
|
|
minimumNArgsWithLessArgs(err, t)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaximumNArgs(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(MaximumNArgs(3), false)
|
2017-10-31 11:57:17 -07:00
|
|
|
output, err := executeCommand(c, "a", "b")
|
2021-11-16 15:20:18 -07:00
|
|
|
expectSuccess(output, err, t)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaximumNArgsWithMoreArgs(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(MaximumNArgs(2), false)
|
2017-10-31 11:57:17 -07:00
|
|
|
_, err := executeCommand(c, "a", "b", "c")
|
2021-11-16 15:20:18 -07:00
|
|
|
maximumNArgsWithMoreArgs(err, t)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactArgs(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(ExactArgs(3), false)
|
2017-10-31 11:57:17 -07:00
|
|
|
output, err := executeCommand(c, "a", "b", "c")
|
2021-11-16 15:20:18 -07:00
|
|
|
expectSuccess(output, err, t)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactArgsWithInvalidCount(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(ExactArgs(2), false)
|
2017-10-31 11:57:17 -07:00
|
|
|
_, err := executeCommand(c, "a", "b", "c")
|
2021-11-16 15:20:18 -07:00
|
|
|
exactArgsWithInvalidCount(err, t)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
|
2018-10-21 07:01:21 -07:00
|
|
|
func TestExactValidArgs(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(ExactValidArgs(3), true)
|
|
|
|
output, err := executeCommand(c, "three", "one", "two")
|
|
|
|
expectSuccess(output, err, t)
|
2018-10-21 07:01:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactValidArgsWithInvalidCount(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(ExactValidArgs(2), false)
|
|
|
|
_, err := executeCommand(c, "three", "one", "two")
|
|
|
|
exactArgsWithInvalidCount(err, t)
|
2018-10-21 07:01:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactValidArgsWithInvalidArgs(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(ExactValidArgs(3), true)
|
|
|
|
_, err := executeCommand(c, "three", "a", "two")
|
|
|
|
validWithInvalidArgs(err, t)
|
2018-10-21 07:01:21 -07:00
|
|
|
}
|
|
|
|
|
2017-10-31 11:57:17 -07:00
|
|
|
func TestRangeArgs(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(RangeArgs(2, 4), false)
|
2017-10-31 11:57:17 -07:00
|
|
|
output, err := executeCommand(c, "a", "b", "c")
|
2021-11-16 15:20:18 -07:00
|
|
|
expectSuccess(output, err, t)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRangeArgsWithInvalidCount(t *testing.T) {
|
2021-11-16 15:20:18 -07:00
|
|
|
c := getCommand(RangeArgs(2, 4), false)
|
2017-10-31 11:57:17 -07:00
|
|
|
_, err := executeCommand(c, "a")
|
2021-11-16 15:20:18 -07:00
|
|
|
rangeArgsWithInvalidCount(err, t)
|
2017-10-31 11:57:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|