2013-09-24 21:04:00 +00:00
|
|
|
package cobra
|
2013-09-03 22:54:51 +00:00
|
|
|
|
|
|
|
import (
|
2013-09-24 16:12:32 +00:00
|
|
|
"bytes"
|
2013-09-04 15:27:10 +00:00
|
|
|
"fmt"
|
2013-09-03 22:54:51 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-09-04 15:27:10 +00:00
|
|
|
var _ = fmt.Println
|
|
|
|
|
2013-09-03 22:54:51 +00:00
|
|
|
var tp, te, tt, t1 []string
|
2013-10-01 01:54:46 +00:00
|
|
|
var flagb1, flagb2, flagb3, flagbr bool
|
2013-09-03 22:54:51 +00:00
|
|
|
var flags1, flags2, flags3 string
|
2013-10-01 01:54:46 +00:00
|
|
|
var flagi1, flagi2, flagi3, flagir int
|
2013-09-03 22:54:51 +00:00
|
|
|
var globalFlag1 bool
|
2013-10-01 01:54:46 +00:00
|
|
|
var flagEcho, rootcalled bool
|
2013-09-03 22:54:51 +00:00
|
|
|
|
|
|
|
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 14:32:51 +00:00
|
|
|
Use: "times [# times] [string to echo]",
|
2013-09-03 22:54:51 +00:00
|
|
|
Short: "Echo anything to the screen more times",
|
|
|
|
Long: `an slightly useless command for testing.`,
|
2013-09-03 23:04:50 +00:00
|
|
|
Run: timesRunner,
|
|
|
|
}
|
|
|
|
|
2013-11-01 22:36:24 +00:00
|
|
|
var cmdRootNoRun = &Command{
|
|
|
|
Use: "cobra-test",
|
|
|
|
Short: "The root can run it's own function",
|
|
|
|
Long: "The root description for help",
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdRootWithRun = &Command{
|
2013-10-01 01:54:46 +00:00
|
|
|
Use: "cobra-test",
|
|
|
|
Short: "The root can run it's own function",
|
|
|
|
Long: "The root description for help",
|
|
|
|
Run: func(cmd *Command, args []string) {
|
|
|
|
rootcalled = true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-09-03 23:04:50 +00:00
|
|
|
func timesRunner(cmd *Command, args []string) {
|
|
|
|
tt = args
|
2013-09-03 22:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func flagInit() {
|
|
|
|
cmdEcho.ResetFlags()
|
|
|
|
cmdPrint.ResetFlags()
|
|
|
|
cmdTimes.ResetFlags()
|
2013-11-01 22:36:24 +00:00
|
|
|
cmdRootNoRun.ResetFlags()
|
|
|
|
cmdRootWithRun.ResetFlags()
|
2013-09-03 22:54:51 +00:00
|
|
|
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 15:25:32 +00:00
|
|
|
func commandInit() {
|
|
|
|
cmdEcho.ResetCommands()
|
|
|
|
cmdPrint.ResetCommands()
|
|
|
|
cmdTimes.ResetCommands()
|
|
|
|
}
|
|
|
|
|
2013-11-01 22:36:24 +00:00
|
|
|
func initialize() *Command {
|
2013-09-03 22:54:51 +00:00
|
|
|
tt, tp, te = nil, nil, nil
|
2013-11-01 22:36:24 +00:00
|
|
|
var c = cmdRootNoRun
|
2013-09-04 15:25:32 +00:00
|
|
|
flagInit()
|
|
|
|
commandInit()
|
2013-09-03 22:54:51 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2013-11-01 22:36:24 +00:00
|
|
|
func initializeWithRootCmd() *Command {
|
|
|
|
cmdRootWithRun.ResetCommands()
|
2013-10-01 01:54:46 +00:00
|
|
|
tt, tp, te, rootcalled = nil, nil, nil, false
|
|
|
|
flagInit()
|
2013-11-01 22:36:24 +00:00
|
|
|
cmdRootWithRun.Flags().BoolVarP(&flagbr, "boolroot", "b", false, "help message for flag boolroot")
|
|
|
|
cmdRootWithRun.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag intthree")
|
2013-10-01 01:54:46 +00:00
|
|
|
commandInit()
|
2013-11-01 22:36:24 +00:00
|
|
|
return cmdRootWithRun
|
2013-10-01 01:54:46 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 22:54:51 +00:00
|
|
|
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 15:27:10 +00:00
|
|
|
t.Errorf("default flag value changed on different command with same shortname, 234 expected, %d given", flagi2)
|
2013-09-03 22:54:51 +00: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 16:12:32 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2013-09-04 15:27:10 +00:00
|
|
|
// Testing with flag that shouldn't be persistent
|
|
|
|
c = initialize()
|
2013-09-24 16:12:32 +00:00
|
|
|
c.SetOutput(buf)
|
|
|
|
// define children
|
2013-09-04 15:27:10 +00:00
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
2013-09-24 16:12:32 +00:00
|
|
|
// define grandchild
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
2013-09-04 15:27:10 +00: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-03 22:54:51 +00:00
|
|
|
|
2013-09-29 06:01:56 +00:00
|
|
|
if !strings.Contains(buf.String(), "unknown shorthand") {
|
2013-09-24 16:12:32 +00:00
|
|
|
t.Errorf("Wrong error message displayed, \n %s", buf.String())
|
|
|
|
}
|
|
|
|
|
2013-09-04 15:27:10 +00: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 16:12:32 +00:00
|
|
|
|
|
|
|
// Testing with flag only existing on child
|
2013-09-24 20:03:22 +00:00
|
|
|
buf.Reset()
|
2013-09-24 16:12:32 +00:00
|
|
|
c = initialize()
|
2013-09-24 20:03:22 +00:00
|
|
|
c.SetOutput(buf)
|
2013-09-24 16:12:32 +00:00
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("echo -j 99 -i77 one two", " "))
|
|
|
|
err := c.Execute()
|
|
|
|
|
2013-09-24 17:15:30 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("invalid flag should generate error")
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:03:22 +00:00
|
|
|
if !strings.Contains(buf.String(), "intone=123") {
|
2013-09-24 17:15:30 +00:00
|
|
|
t.Errorf("Wrong error message displayed, \n %s", buf.String())
|
|
|
|
}
|
2013-09-03 22:54:51 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 00:50:47 +00:00
|
|
|
func TestTrailingCommandFlags(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
c := initialize()
|
|
|
|
c.SetOutput(buf)
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("echo two -x", " "))
|
|
|
|
e3 := c.Execute()
|
|
|
|
|
|
|
|
if e3 == nil {
|
|
|
|
t.Errorf("invalid flag should generate error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 22:54:51 +00: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-10 22:26:46 +00: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-03 22:54:51 +00:00
|
|
|
}
|
2013-09-24 20:03:22 +00: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())
|
|
|
|
}
|
|
|
|
}
|
2013-10-01 01:54:46 +00:00
|
|
|
|
2013-11-01 22:36:24 +00:00
|
|
|
func TestRunnableRootCommand(t *testing.T) {
|
2013-10-01 01:54:46 +00:00
|
|
|
c := initializeWithRootCmd()
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs([]string(nil))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if rootcalled != true {
|
|
|
|
t.Errorf("Root Function was not called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootFlags(t *testing.T) {
|
|
|
|
c := initializeWithRootCmd()
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split("-i 17 -b", " "))
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if flagbr != true {
|
|
|
|
t.Errorf("flag value should be true, %v given", flagbr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if flagir != 17 {
|
|
|
|
t.Errorf("flag value should be 17, %d given", flagir)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|