2013-09-04 02:54:51 +04:00
|
|
|
package cobra_test
|
|
|
|
|
|
|
|
import (
|
2013-09-24 20:12:32 +04:00
|
|
|
"bytes"
|
2013-09-04 02:54:51 +04:00
|
|
|
. "cobra"
|
2013-09-04 19:27:10 +04:00
|
|
|
"fmt"
|
2013-09-04 02:54:51 +04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-09-04 19:27:10 +04:00
|
|
|
var _ = fmt.Println
|
|
|
|
|
2013-09-04 02:54:51 +04:00
|
|
|
var tp, te, tt, t1 []string
|
|
|
|
var flagb1, flagb2, flagb3 bool
|
|
|
|
var flags1, flags2, flags3 string
|
|
|
|
var flagi1, flagi2, flagi3 int
|
|
|
|
var globalFlag1 bool
|
|
|
|
var flagEcho bool
|
|
|
|
|
|
|
|
var cmdPrint = &Command{
|
|
|
|
Use: "print [string to print]",
|
|
|
|
Short: "Print anything to the screen",
|
|
|
|
Long: `an utterly useless command for testing.`,
|
|
|
|
Run: func(cmd *Command, args []string) {
|
|
|
|
tp = args
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdEcho = &Command{
|
|
|
|
Use: "echo [string to echo]",
|
|
|
|
Short: "Echo anything to the screen",
|
|
|
|
Long: `an utterly useless command for testing.`,
|
|
|
|
Run: func(cmd *Command, args []string) {
|
|
|
|
te = args
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdTimes = &Command{
|
2013-09-12 18:32:51 +04:00
|
|
|
Use: "times [# times] [string to echo]",
|
2013-09-04 02:54:51 +04:00
|
|
|
Short: "Echo anything to the screen more times",
|
|
|
|
Long: `an slightly useless command for testing.`,
|
2013-09-04 03:04:50 +04:00
|
|
|
Run: timesRunner,
|
|
|
|
}
|
|
|
|
|
|
|
|
func timesRunner(cmd *Command, args []string) {
|
|
|
|
tt = args
|
2013-09-04 02:54:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func flagInit() {
|
|
|
|
cmdEcho.ResetFlags()
|
|
|
|
cmdPrint.ResetFlags()
|
|
|
|
cmdTimes.ResetFlags()
|
|
|
|
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
|
|
|
|
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
|
|
|
|
cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")
|
|
|
|
cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")
|
|
|
|
cmdTimes.PersistentFlags().StringVarP(&flags2, "strtwo", "t", "two", "help message for flag strtwo")
|
|
|
|
cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")
|
|
|
|
cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone")
|
|
|
|
cmdTimes.Flags().BoolVarP(&flagb2, "booltwo", "c", false, "help message for flag booltwo")
|
|
|
|
cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree")
|
|
|
|
}
|
|
|
|
|
2013-09-04 19:25:32 +04:00
|
|
|
func commandInit() {
|
|
|
|
cmdEcho.ResetCommands()
|
|
|
|
cmdPrint.ResetCommands()
|
|
|
|
cmdTimes.ResetCommands()
|
|
|
|
}
|
|
|
|
|
2013-09-04 02:54:51 +04:00
|
|
|
func initialize() *Commander {
|
|
|
|
tt, tp, te = nil, nil, nil
|
|
|
|
var c = NewCommander()
|
2013-09-12 18:32:51 +04:00
|
|
|
c.SetName("cobratest")
|
2013-09-04 19:25:32 +04:00
|
|
|
flagInit()
|
|
|
|
commandInit()
|
2013-09-04 02:54:51 +04:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSingleCommand(t *testing.T) {
|
|
|
|
c := initialize()
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("print one two", " "))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if te != nil || tt != nil {
|
|
|
|
t.Error("Wrong command called")
|
|
|
|
}
|
|
|
|
if tp == nil {
|
|
|
|
t.Error("Wrong command called")
|
|
|
|
}
|
|
|
|
if strings.Join(tp, " ") != "one two" {
|
|
|
|
t.Error("Command didn't parse correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChildCommand(t *testing.T) {
|
|
|
|
c := initialize()
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("echo times one two", " "))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if te != nil || tp != nil {
|
|
|
|
t.Error("Wrong command called")
|
|
|
|
}
|
|
|
|
if tt == nil {
|
|
|
|
t.Error("Wrong command called")
|
|
|
|
}
|
|
|
|
if strings.Join(tt, " ") != "one two" {
|
|
|
|
t.Error("Command didn't parse correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlagLong(t *testing.T) {
|
|
|
|
c := initialize()
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
|
|
|
c.SetArgs(strings.Split("echo --intone=13 something here", " "))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if strings.Join(te, " ") != "something here" {
|
|
|
|
t.Errorf("flags didn't leave proper args remaining..%s given", te)
|
|
|
|
}
|
|
|
|
if flagi1 != 13 {
|
|
|
|
t.Errorf("int flag didn't get correct value, had %d", flagi1)
|
|
|
|
}
|
|
|
|
if flagi2 != 234 {
|
|
|
|
t.Errorf("default flag value changed, 234 expected, %d given", flagi2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlagShort(t *testing.T) {
|
|
|
|
c := initialize()
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
|
|
|
c.SetArgs(strings.Split("echo -i13 something here", " "))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if strings.Join(te, " ") != "something here" {
|
|
|
|
t.Errorf("flags didn't leave proper args remaining..%s given", te)
|
|
|
|
}
|
|
|
|
if flagi1 != 13 {
|
|
|
|
t.Errorf("int flag didn't get correct value, had %d", flagi1)
|
|
|
|
}
|
|
|
|
if flagi2 != 234 {
|
|
|
|
t.Errorf("default flag value changed, 234 expected, %d given", flagi2)
|
|
|
|
}
|
|
|
|
|
|
|
|
c = initialize()
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
|
|
|
c.SetArgs(strings.Split("echo -i 13 something here", " "))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if strings.Join(te, " ") != "something here" {
|
|
|
|
t.Errorf("flags didn't leave proper args remaining..%s given", te)
|
|
|
|
}
|
|
|
|
if flagi1 != 13 {
|
|
|
|
t.Errorf("int flag didn't get correct value, had %d", flagi1)
|
|
|
|
}
|
|
|
|
if flagi2 != 234 {
|
|
|
|
t.Errorf("default flag value changed, 234 expected, %d given", flagi2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Testing same shortcode, different command
|
|
|
|
c = initialize()
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
|
|
|
c.SetArgs(strings.Split("print -i99 one two", " "))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if strings.Join(tp, " ") != "one two" {
|
|
|
|
t.Errorf("flags didn't leave proper args remaining..%s given", tp)
|
|
|
|
}
|
|
|
|
if flagi3 != 99 {
|
|
|
|
t.Errorf("int flag didn't get correct value, had %d", flagi3)
|
|
|
|
}
|
|
|
|
if flagi1 != 123 {
|
2013-09-04 19:27:10 +04:00
|
|
|
t.Errorf("default flag value changed on different command with same shortname, 234 expected, %d given", flagi2)
|
2013-09-04 02:54:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChildCommandFlags(t *testing.T) {
|
|
|
|
c := initialize()
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("echo times -j 99 one two", " "))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if strings.Join(tt, " ") != "one two" {
|
|
|
|
t.Errorf("flags didn't leave proper args remaining..%s given", tt)
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:12:32 +04:00
|
|
|
buf := new(bytes.Buffer)
|
2013-09-04 19:27:10 +04:00
|
|
|
// Testing with flag that shouldn't be persistent
|
|
|
|
c = initialize()
|
2013-09-24 20:12:32 +04:00
|
|
|
c.SetOutput(buf)
|
|
|
|
// define children
|
2013-09-04 19:27:10 +04:00
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
2013-09-24 20:12:32 +04:00
|
|
|
// define grandchild
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
2013-09-04 19:27:10 +04:00
|
|
|
c.SetArgs(strings.Split("echo times -j 99 -i77 one two", " "))
|
|
|
|
e := c.Execute()
|
|
|
|
|
|
|
|
if e == nil {
|
|
|
|
t.Errorf("invalid flag should generate error")
|
|
|
|
}
|
2013-09-04 02:54:51 +04:00
|
|
|
|
2013-09-24 20:12:32 +04:00
|
|
|
if !strings.Contains(buf.String(), "inttwo=234") {
|
|
|
|
t.Errorf("Wrong error message displayed, \n %s", buf.String())
|
|
|
|
}
|
|
|
|
|
2013-09-04 19:27:10 +04:00
|
|
|
if flagi2 != 99 {
|
|
|
|
t.Errorf("flag value should be 99, %d given", flagi2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if flagi1 != 123 {
|
|
|
|
t.Errorf("unset flag should have default value, expecting 123, given %d", flagi1)
|
|
|
|
}
|
2013-09-24 20:12:32 +04:00
|
|
|
|
|
|
|
// Testing with flag only existing on child
|
2013-09-25 00:03:22 +04:00
|
|
|
buf.Reset()
|
2013-09-24 20:12:32 +04:00
|
|
|
c = initialize()
|
2013-09-25 00:03:22 +04:00
|
|
|
c.SetOutput(buf)
|
2013-09-24 20:12:32 +04:00
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("echo -j 99 -i77 one two", " "))
|
|
|
|
err := c.Execute()
|
|
|
|
|
2013-09-24 21:15:30 +04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("invalid flag should generate error")
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:03:22 +04:00
|
|
|
if !strings.Contains(buf.String(), "intone=123") {
|
2013-09-24 21:15:30 +04:00
|
|
|
t.Errorf("Wrong error message displayed, \n %s", buf.String())
|
|
|
|
}
|
2013-09-04 02:54:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersistentFlags(t *testing.T) {
|
|
|
|
c := initialize()
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("echo -s something more here", " "))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
// persistentFlag should act like normal flag on it's own command
|
|
|
|
if strings.Join(te, " ") != "more here" {
|
|
|
|
t.Errorf("flags didn't leave proper args remaining..%s given", te)
|
|
|
|
}
|
|
|
|
|
|
|
|
// persistentFlag should act like normal flag on it's own command
|
|
|
|
if flags1 != "something" {
|
|
|
|
t.Errorf("string flag didn't get correct value, had %v", flags1)
|
|
|
|
}
|
|
|
|
|
2013-09-11 02:26:46 +04:00
|
|
|
c = initialize()
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("echo times -s again -c test here", " "))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if strings.Join(tt, " ") != "test here" {
|
|
|
|
t.Errorf("flags didn't leave proper args remaining..%s given", tt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags1 != "again" {
|
|
|
|
t.Errorf("string flag didn't get correct value, had %v", flags1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if flagb2 != true {
|
|
|
|
t.Errorf("local flag not parsed correctly. Expected false, had %v", flagb2)
|
|
|
|
}
|
2013-09-04 02:54:51 +04:00
|
|
|
}
|
2013-09-25 00:03:22 +04:00
|
|
|
|
|
|
|
func TestHelpCommand(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
c := initialize()
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("help echo", " "))
|
|
|
|
c.SetOutput(buf)
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if !strings.Contains(buf.String(), cmdEcho.Long) {
|
|
|
|
t.Errorf("Wrong error message displayed, \n %s", buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.Reset()
|
|
|
|
c = initialize()
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("help echo times", " "))
|
|
|
|
c.SetOutput(buf)
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if !strings.Contains(buf.String(), cmdTimes.Long) {
|
|
|
|
t.Errorf("Wrong error message displayed, \n %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|