summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/spf13/cobra/command_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/spf13/cobra/command_test.go')
-rw-r--r--vendor/github.com/spf13/cobra/command_test.go50
1 files changed, 45 insertions, 5 deletions
diff --git a/vendor/github.com/spf13/cobra/command_test.go b/vendor/github.com/spf13/cobra/command_test.go
index 3921a0be7..978bacc52 100644
--- a/vendor/github.com/spf13/cobra/command_test.go
+++ b/vendor/github.com/spf13/cobra/command_test.go
@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"reflect"
+ "strings"
"testing"
"github.com/spf13/pflag"
@@ -118,7 +119,7 @@ func TestStripFlags(t *testing.T) {
}
}
-func Test_DisableFlagParsing(t *testing.T) {
+func TestDisableFlagParsing(t *testing.T) {
as := []string{"-v", "-race", "-file", "foo.go"}
targs := []string{}
cmdPrint := &Command{
@@ -145,7 +146,7 @@ func TestInitHelpFlagMergesFlags(t *testing.T) {
cmd := Command{Use: "do"}
baseCmd.AddCommand(&cmd)
- cmd.initHelpFlag()
+ cmd.InitDefaultHelpFlag()
actual := cmd.Flags().Lookup("help").Usage
if actual != usage {
t.Fatalf("Expected the help flag from the base command with usage '%s', but got the default with usage '%s'", usage, actual)
@@ -226,7 +227,7 @@ func TestFlagErrorFunc(t *testing.T) {
// TestSortedFlags checks,
// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.
-// https://github.com/spf13/cobra/issues/404
+// Related to https://github.com/spf13/cobra/issues/404.
func TestSortedFlags(t *testing.T) {
cmd := &Command{}
cmd.Flags().SortFlags = false
@@ -240,7 +241,7 @@ func TestSortedFlags(t *testing.T) {
if i == len(names) {
return
}
- if contains(f.Name, names) {
+ if isStringInStringSlice(f.Name, names) {
if names[i] != f.Name {
t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)
}
@@ -250,7 +251,7 @@ func TestSortedFlags(t *testing.T) {
}
// contains checks, if s is in ss.
-func contains(s string, ss []string) bool {
+func isStringInStringSlice(s string, ss []string) bool {
for _, v := range ss {
if v == s {
return true
@@ -258,3 +259,42 @@ func contains(s string, ss []string) bool {
}
return false
}
+
+// TestHelpFlagInHelp checks,
+// if '--help' flag is shown in help for child (executing `parent help child`),
+// that has no other flags.
+// Related to https://github.com/spf13/cobra/issues/302.
+func TestHelpFlagInHelp(t *testing.T) {
+ output := new(bytes.Buffer)
+ parent := &Command{Use: "parent", Run: func(*Command, []string) {}}
+ parent.SetOutput(output)
+
+ child := &Command{Use: "child", Run: func(*Command, []string) {}}
+ parent.AddCommand(child)
+
+ parent.SetArgs([]string{"help", "child"})
+ err := parent.Execute()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !strings.Contains(output.String(), "[flags]") {
+ t.Errorf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())
+ }
+}
+
+// TestMergeCommandLineToFlags checks,
+// if pflag.CommandLine is correctly merged to c.Flags() after first call
+// of c.mergePersistentFlags.
+// Related to https://github.com/spf13/cobra/issues/443.
+func TestMergeCommandLineToFlags(t *testing.T) {
+ pflag.Bool("boolflag", false, "")
+ c := &Command{Use: "c", Run: func(*Command, []string) {}}
+ c.mergePersistentFlags()
+ if c.Flags().Lookup("boolflag") == nil {
+ t.Fatal("Expecting to have flag from CommandLine in c.Flags()")
+ }
+
+ // Reset pflag.CommandLine flagset.
+ pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
+}