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
|
2015-03-18 21:42:38 +00:00
|
|
|
var flagb1, flagb2, flagb3, flagbr, flagbp bool
|
2015-02-28 00:26:56 +00:00
|
|
|
var flags1, flags2a, flags2b, 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
|
|
|
|
2015-02-28 00:26:56 +00:00
|
|
|
const strtwoParentHelp = "help message for parent flag strtwo"
|
|
|
|
const strtwoChildHelp = "help message for child flag strtwo"
|
|
|
|
|
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{
|
2014-09-25 23:03:34 +00:00
|
|
|
Use: "echo [string to echo]",
|
|
|
|
Aliases: []string{"say"},
|
|
|
|
Short: "Echo anything to the screen",
|
|
|
|
Long: `an utterly useless command for testing.`,
|
2013-09-03 22:54:51 +00:00
|
|
|
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.`,
|
2014-09-25 23:03:34 +00:00
|
|
|
Run: func(cmd *Command, args []string) {
|
|
|
|
tt = args
|
|
|
|
},
|
2013-09-03 23:04:50 +00:00
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
2014-03-26 20:19:34 +00:00
|
|
|
var cmdRootSameName = &Command{
|
|
|
|
Use: "print",
|
|
|
|
Short: "Root with the same name as a subcommand",
|
|
|
|
Long: "The root description for help",
|
|
|
|
}
|
|
|
|
|
2013-11-01 22:36:24 +00:00
|
|
|
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 22:54:51 +00:00
|
|
|
func flagInit() {
|
|
|
|
cmdEcho.ResetFlags()
|
|
|
|
cmdPrint.ResetFlags()
|
|
|
|
cmdTimes.ResetFlags()
|
2013-11-01 22:36:24 +00:00
|
|
|
cmdRootNoRun.ResetFlags()
|
2014-03-26 20:19:34 +00:00
|
|
|
cmdRootSameName.ResetFlags()
|
2013-11-01 22:36:24 +00:00
|
|
|
cmdRootWithRun.ResetFlags()
|
2015-02-28 00:26:56 +00:00
|
|
|
cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp)
|
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")
|
2015-03-18 21:42:38 +00:00
|
|
|
cmdEcho.PersistentFlags().BoolVarP(&flagbp, "persistentbool", "p", false, "help message for flag persistentbool")
|
2015-02-28 00:26:56 +00:00
|
|
|
cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)
|
2013-09-03 22:54:51 +00:00
|
|
|
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()
|
2014-03-26 08:54:47 +00:00
|
|
|
cmdRootNoRun.ResetCommands()
|
2014-03-26 20:19:34 +00:00
|
|
|
cmdRootSameName.ResetCommands()
|
2014-03-26 08:54:47 +00:00
|
|
|
cmdRootWithRun.ResetCommands()
|
2013-09-04 15:25:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-03-26 20:19:34 +00:00
|
|
|
func initializeWithSameName() *Command {
|
|
|
|
tt, tp, te = nil, nil, nil
|
|
|
|
var c = cmdRootSameName
|
|
|
|
flagInit()
|
|
|
|
commandInit()
|
|
|
|
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")
|
2014-06-17 16:28:42 +00:00
|
|
|
cmdRootWithRun.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag introot")
|
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
|
|
|
}
|
|
|
|
|
2014-06-17 16:28:42 +00:00
|
|
|
type resulter struct {
|
|
|
|
Error error
|
|
|
|
Output string
|
|
|
|
Command *Command
|
|
|
|
}
|
|
|
|
|
|
|
|
func fullSetupTest(input string) resulter {
|
|
|
|
c := initializeWithRootCmd()
|
|
|
|
|
|
|
|
return fullTester(c, input)
|
|
|
|
}
|
|
|
|
|
|
|
|
func noRRSetupTest(input string) resulter {
|
|
|
|
c := initialize()
|
|
|
|
|
|
|
|
return fullTester(c, input)
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:32:54 +00:00
|
|
|
func rootOnlySetupTest(input string) resulter {
|
|
|
|
c := initializeWithRootCmd()
|
|
|
|
|
|
|
|
return simpleTester(c, input)
|
|
|
|
}
|
|
|
|
|
|
|
|
func simpleTester(c *Command, input string) resulter {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
// Testing flag with invalid input
|
|
|
|
c.SetOutput(buf)
|
|
|
|
c.SetArgs(strings.Split(input, " "))
|
|
|
|
|
|
|
|
err := c.Execute()
|
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
return resulter{err, output, c}
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:28:42 +00:00
|
|
|
func fullTester(c *Command, input string) resulter {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
// Testing flag with invalid input
|
|
|
|
c.SetOutput(buf)
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
|
|
|
c.AddCommand(cmdPrint, cmdEcho)
|
|
|
|
c.SetArgs(strings.Split(input, " "))
|
|
|
|
|
|
|
|
err := c.Execute()
|
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
return resulter{err, output, c}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkResultContains(t *testing.T, x resulter, check string) {
|
|
|
|
if !strings.Contains(x.Output, check) {
|
|
|
|
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", check, x.Output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:32:54 +00:00
|
|
|
func checkResultOmits(t *testing.T, x resulter, check string) {
|
|
|
|
if strings.Contains(x.Output, check) {
|
|
|
|
t.Errorf("Unexpected response.\nExpecting to omit: \n %q\nGot:\n %q\n", check, x.Output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-14 00:00:56 +00:00
|
|
|
func checkOutputContains(t *testing.T, c *Command, check string) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
c.SetOutput(buf)
|
|
|
|
c.Execute()
|
|
|
|
|
|
|
|
if !strings.Contains(buf.String(), check) {
|
|
|
|
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", check, buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 22:54:51 +00:00
|
|
|
func TestSingleCommand(t *testing.T) {
|
2014-06-17 16:32:27 +00:00
|
|
|
noRRSetupTest("print one two")
|
2013-09-03 22:54:51 +00:00
|
|
|
|
|
|
|
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) {
|
2014-06-17 16:32:27 +00:00
|
|
|
noRRSetupTest("echo times one two")
|
2013-09-03 22:54:51 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 23:03:34 +00:00
|
|
|
func TestCommandAlias(t *testing.T) {
|
|
|
|
noRRSetupTest("say times one two")
|
2014-03-26 08:56:46 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 19:41:19 +00:00
|
|
|
func TestPrefixMatching(t *testing.T) {
|
2014-10-07 20:15:19 +00:00
|
|
|
EnablePrefixMatching = true
|
2014-10-07 19:41:19 +00:00
|
|
|
noRRSetupTest("ech times one two")
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2014-10-07 20:15:19 +00:00
|
|
|
|
|
|
|
EnablePrefixMatching = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoPrefixMatching(t *testing.T) {
|
|
|
|
EnablePrefixMatching = false
|
|
|
|
|
|
|
|
noRRSetupTest("ech times one two")
|
|
|
|
|
|
|
|
if !(tt == nil && te == nil && tp == nil) {
|
|
|
|
t.Error("Wrong command called")
|
|
|
|
}
|
2014-10-07 19:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAliasPrefixMatching(t *testing.T) {
|
2014-10-07 20:15:19 +00:00
|
|
|
EnablePrefixMatching = true
|
2014-10-07 19:41:19 +00:00
|
|
|
noRRSetupTest("sa times one two")
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2014-10-07 20:15:19 +00:00
|
|
|
EnablePrefixMatching = false
|
2014-10-07 19:41:19 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 20:19:34 +00:00
|
|
|
func TestChildSameName(t *testing.T) {
|
|
|
|
c := initializeWithSameName()
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 22:54:51 +00:00
|
|
|
func TestFlagLong(t *testing.T) {
|
2014-06-17 16:32:27 +00:00
|
|
|
noRRSetupTest("echo --intone=13 something here")
|
2013-09-03 22:54:51 +00:00
|
|
|
|
|
|
|
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) {
|
2014-06-17 16:32:27 +00:00
|
|
|
noRRSetupTest("echo -i13 something here")
|
2013-09-03 22:54:51 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:32:27 +00:00
|
|
|
noRRSetupTest("echo -i 13 something here")
|
2013-09-03 22:54:51 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:32:27 +00:00
|
|
|
noRRSetupTest("print -i99 one two")
|
2013-09-03 22:54:51 +00:00
|
|
|
|
|
|
|
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) {
|
2014-06-17 16:32:27 +00:00
|
|
|
noRRSetupTest("echo times -j 99 one two")
|
2013-09-03 22:54:51 +00:00
|
|
|
|
|
|
|
if strings.Join(tt, " ") != "one two" {
|
|
|
|
t.Errorf("flags didn't leave proper args remaining..%s given", tt)
|
|
|
|
}
|
|
|
|
|
2013-09-04 15:27:10 +00:00
|
|
|
// Testing with flag that shouldn't be persistent
|
2014-06-17 16:32:27 +00:00
|
|
|
r := noRRSetupTest("echo times -j 99 -i77 one two")
|
2013-09-04 15:27:10 +00:00
|
|
|
|
2014-06-17 16:32:27 +00:00
|
|
|
if r.Error == nil {
|
2013-09-04 15:27:10 +00:00
|
|
|
t.Errorf("invalid flag should generate error")
|
|
|
|
}
|
2013-09-03 22:54:51 +00:00
|
|
|
|
2014-06-17 16:32:27 +00:00
|
|
|
if !strings.Contains(r.Output, "unknown shorthand") {
|
|
|
|
t.Errorf("Wrong error message displayed, \n %s", r.Output)
|
2013-09-24 16:12:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
2014-06-17 16:32:27 +00:00
|
|
|
r = noRRSetupTest("echo -j 99 -i77 one two")
|
2013-09-24 16:12:32 +00:00
|
|
|
|
2014-06-17 16:32:27 +00:00
|
|
|
if r.Error == nil {
|
2013-09-24 17:15:30 +00:00
|
|
|
t.Errorf("invalid flag should generate error")
|
|
|
|
}
|
|
|
|
|
2015-03-11 17:22:30 +00:00
|
|
|
if !strings.Contains(r.Output, "unknown shorthand flag") {
|
2014-06-17 16:32:27 +00:00
|
|
|
t.Errorf("Wrong error message displayed, \n %s", r.Output)
|
2013-09-24 17:15:30 +00:00
|
|
|
}
|
2014-06-14 00:45:55 +00:00
|
|
|
|
2015-02-28 00:26:56 +00:00
|
|
|
// Testing with persistent flag overwritten by child
|
|
|
|
noRRSetupTest("echo times --strtwo=child one two")
|
|
|
|
|
|
|
|
if flags2b != "child" {
|
|
|
|
t.Errorf("flag value should be child, %s given", flags2b)
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags2a != "two" {
|
|
|
|
t.Errorf("unset flag should have default value, expecting two, given %s", flags2a)
|
|
|
|
}
|
|
|
|
|
2014-06-14 00:45:55 +00:00
|
|
|
// Testing flag with invalid input
|
2014-06-17 16:32:27 +00:00
|
|
|
r = noRRSetupTest("echo -i10E")
|
2014-06-14 00:45:55 +00:00
|
|
|
|
2014-06-17 16:32:27 +00:00
|
|
|
if r.Error == nil {
|
2014-06-14 00:45:55 +00:00
|
|
|
t.Errorf("invalid input should generate error")
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:32:27 +00:00
|
|
|
if !strings.Contains(r.Output, "invalid argument \"10E\" for -i10E") {
|
|
|
|
t.Errorf("Wrong error message displayed, \n %s", r.Output)
|
2014-06-14 00:45:55 +00:00
|
|
|
}
|
2013-09-03 22:54:51 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 00:50:47 +00:00
|
|
|
func TestTrailingCommandFlags(t *testing.T) {
|
2014-06-17 16:32:27 +00:00
|
|
|
x := fullSetupTest("echo two -x")
|
2013-11-06 00:50:47 +00:00
|
|
|
|
2014-06-17 16:32:27 +00:00
|
|
|
if x.Error == nil {
|
2013-11-06 00:50:47 +00:00
|
|
|
t.Errorf("invalid flag should generate error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 22:54:51 +00:00
|
|
|
func TestPersistentFlags(t *testing.T) {
|
2015-03-18 21:42:38 +00:00
|
|
|
fullSetupTest("echo -s something -p more here")
|
2013-09-03 22:54:51 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
if flags1 != "something" {
|
|
|
|
t.Errorf("string flag didn't get correct value, had %v", flags1)
|
|
|
|
}
|
2015-03-18 21:42:38 +00:00
|
|
|
if !flagbp {
|
|
|
|
t.Errorf("persistent bool flag not parsed correctly. Expected true, had %v", flagbp)
|
|
|
|
}
|
2013-09-03 22:54:51 +00:00
|
|
|
|
2015-03-18 21:42:38 +00:00
|
|
|
// persistentFlag should act like normal flag on it's own command
|
|
|
|
fullSetupTest("echo times -s again -c -p test here")
|
2013-09-10 22:26:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-03-18 21:42:38 +00:00
|
|
|
if !flagb2 {
|
|
|
|
t.Errorf("local flag not parsed correctly. Expected true, had %v", flagb2)
|
|
|
|
}
|
|
|
|
if !flagbp {
|
|
|
|
t.Errorf("persistent bool flag not parsed correctly. Expected true, had %v", flagbp)
|
2013-09-10 22:26:46 +00:00
|
|
|
}
|
2013-09-03 22:54:51 +00:00
|
|
|
}
|
2013-09-24 20:03:22 +00:00
|
|
|
|
|
|
|
func TestHelpCommand(t *testing.T) {
|
2014-06-17 16:32:27 +00:00
|
|
|
c := fullSetupTest("help echo")
|
|
|
|
checkResultContains(t, c, cmdEcho.Long)
|
2013-09-24 20:03:22 +00:00
|
|
|
|
2014-06-17 16:32:27 +00:00
|
|
|
r := fullSetupTest("help echo times")
|
|
|
|
checkResultContains(t, r, cmdTimes.Long)
|
2013-09-24 20:03:22 +00:00
|
|
|
}
|
2013-10-01 01:54:46 +00:00
|
|
|
|
2015-02-28 00:26:56 +00:00
|
|
|
func TestChildCommandHelp(t *testing.T) {
|
|
|
|
c := noRRSetupTest("print --help")
|
|
|
|
checkResultContains(t, c, strtwoParentHelp)
|
|
|
|
r := noRRSetupTest("echo times --help")
|
|
|
|
checkResultContains(t, r, strtwoChildHelp)
|
|
|
|
}
|
|
|
|
|
2013-11-01 22:36:24 +00:00
|
|
|
func TestRunnableRootCommand(t *testing.T) {
|
2014-06-17 16:32:27 +00:00
|
|
|
fullSetupTest("")
|
2013-10-01 01:54:46 +00:00
|
|
|
|
|
|
|
if rootcalled != true {
|
|
|
|
t.Errorf("Root Function was not called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootFlags(t *testing.T) {
|
2014-06-17 16:32:27 +00:00
|
|
|
fullSetupTest("-i 17 -b")
|
2013-10-01 01:54:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2014-06-14 00:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRootHelp(t *testing.T) {
|
2014-06-17 16:28:42 +00:00
|
|
|
x := fullSetupTest("--help")
|
|
|
|
|
|
|
|
checkResultContains(t, x, "Available Commands:")
|
2015-01-31 16:08:05 +00:00
|
|
|
checkResultContains(t, x, "for more information about a command")
|
2014-06-17 16:28:42 +00:00
|
|
|
|
|
|
|
if strings.Contains(x.Output, "unknown flag: --help") {
|
|
|
|
t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)
|
|
|
|
}
|
|
|
|
|
2015-02-11 09:20:29 +00:00
|
|
|
if strings.Contains(x.Output, cmdEcho.Use) {
|
|
|
|
t.Errorf("--help shouldn't display subcommand's usage, Got: \n %s", x.Output)
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:28:42 +00:00
|
|
|
x = fullSetupTest("echo --help")
|
|
|
|
|
2015-02-11 09:20:29 +00:00
|
|
|
if strings.Contains(x.Output, cmdTimes.Use) {
|
|
|
|
t.Errorf("--help shouldn't display subsubcommand's usage, Got: \n %s", x.Output)
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:28:42 +00:00
|
|
|
checkResultContains(t, x, "Available Commands:")
|
2015-01-31 16:08:05 +00:00
|
|
|
checkResultContains(t, x, "for more information about a command")
|
2014-06-17 16:28:42 +00:00
|
|
|
|
|
|
|
if strings.Contains(x.Output, "unknown flag: --help") {
|
|
|
|
t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-10-01 01:54:46 +00:00
|
|
|
|
2015-02-28 00:26:56 +00:00
|
|
|
func TestFlagAccess(t *testing.T) {
|
|
|
|
initialize()
|
|
|
|
|
|
|
|
local := cmdTimes.LocalFlags()
|
|
|
|
inherited := cmdTimes.InheritedFlags()
|
|
|
|
|
|
|
|
for _, f := range []string{"inttwo", "strtwo", "booltwo"} {
|
|
|
|
if local.Lookup(f) == nil {
|
|
|
|
t.Errorf("LocalFlags expected to contain %s, Got: nil", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if inherited.Lookup("strone") == nil {
|
|
|
|
t.Errorf("InheritedFlags expected to contain strone, Got: nil")
|
|
|
|
}
|
|
|
|
if inherited.Lookup("strtwo") != nil {
|
|
|
|
t.Errorf("InheritedFlags shouldn not contain overwritten flag strtwo")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:32:54 +00:00
|
|
|
func TestRootNoCommandHelp(t *testing.T) {
|
|
|
|
x := rootOnlySetupTest("--help")
|
|
|
|
|
|
|
|
checkResultOmits(t, x, "Available Commands:")
|
2015-01-31 16:08:05 +00:00
|
|
|
checkResultOmits(t, x, "for more information about a command")
|
2014-11-12 04:32:54 +00:00
|
|
|
|
|
|
|
if strings.Contains(x.Output, "unknown flag: --help") {
|
|
|
|
t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
x = rootOnlySetupTest("echo --help")
|
|
|
|
|
|
|
|
checkResultOmits(t, x, "Available Commands:")
|
2015-01-31 16:08:05 +00:00
|
|
|
checkResultOmits(t, x, "for more information about a command")
|
2014-11-12 04:32:54 +00:00
|
|
|
|
|
|
|
if strings.Contains(x.Output, "unknown flag: --help") {
|
|
|
|
t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 05:07:34 +00:00
|
|
|
func TestRootUnknownCommand(t *testing.T) {
|
|
|
|
r := noRRSetupTest("bogus")
|
|
|
|
s := "Error: unknown command \"bogus\"\nRun 'cobra-test help' for usage.\n"
|
|
|
|
|
|
|
|
if r.Output != s {
|
|
|
|
t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 16:32:27 +00:00
|
|
|
func TestFlagsBeforeCommand(t *testing.T) {
|
|
|
|
// short without space
|
|
|
|
x := fullSetupTest("-i10 echo")
|
|
|
|
if x.Error != nil {
|
|
|
|
t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// short (int) with equals
|
|
|
|
// It appears that pflags doesn't support this...
|
|
|
|
// Commenting out until support can be added
|
|
|
|
|
|
|
|
//x = noRRSetupTest("echo -i=10")
|
|
|
|
//if x.Error != nil {
|
|
|
|
//t.Errorf("Valid Input shouldn't have errors, got:\n %s", x.Error)
|
|
|
|
//}
|
|
|
|
|
|
|
|
// long with equals
|
|
|
|
x = noRRSetupTest("--intone=123 echo one two")
|
|
|
|
if x.Error != nil {
|
|
|
|
t.Errorf("Valid Input shouldn't have errors, got:\n %s", x.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With parsing error properly reported
|
|
|
|
x = fullSetupTest("-i10E echo")
|
|
|
|
if !strings.Contains(x.Output, "invalid argument \"10E\" for -i10E") {
|
|
|
|
t.Errorf("Wrong error message displayed, \n %s", x.Output)
|
|
|
|
}
|
|
|
|
|
|
|
|
//With quotes
|
|
|
|
x = fullSetupTest("-s=\"walking\" echo")
|
|
|
|
if x.Error != nil {
|
|
|
|
t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
//With quotes and space
|
|
|
|
x = fullSetupTest("-s=\"walking fast\" echo")
|
|
|
|
if x.Error != nil {
|
|
|
|
t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
//With inner quote
|
|
|
|
x = fullSetupTest("-s=\"walking \\\"Inner Quote\\\" fast\" echo")
|
|
|
|
if x.Error != nil {
|
|
|
|
t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
//With quotes and space
|
|
|
|
x = fullSetupTest("-s=\"walking \\\"Inner Quote\\\" fast\" echo")
|
|
|
|
if x.Error != nil {
|
|
|
|
t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
|
|
|
|
}
|
|
|
|
|
2013-10-01 01:54:46 +00:00
|
|
|
}
|