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.go46
1 files changed, 45 insertions, 1 deletions
diff --git a/vendor/github.com/spf13/cobra/command_test.go b/vendor/github.com/spf13/cobra/command_test.go
index 7947f6c4a..3921a0be7 100644
--- a/vendor/github.com/spf13/cobra/command_test.go
+++ b/vendor/github.com/spf13/cobra/command_test.go
@@ -6,6 +6,8 @@ import (
"os"
"reflect"
"testing"
+
+ "github.com/spf13/pflag"
)
// test to ensure hidden commands run as intended
@@ -191,8 +193,15 @@ func TestEnableCommandSortingIsDisabled(t *testing.T) {
EnableCommandSorting = true
}
-func TestFlagErrorFunc(t *testing.T) {
+func TestSetOutput(t *testing.T) {
+ cmd := &Command{}
+ cmd.SetOutput(nil)
+ if out := cmd.OutOrStdout(); out != os.Stdout {
+ t.Fatalf("expected setting output to nil to revert back to stdout, got %v", out)
+ }
+}
+func TestFlagErrorFunc(t *testing.T) {
cmd := &Command{
Use: "print",
RunE: func(cmd *Command, args []string) error {
@@ -214,3 +223,38 @@ func TestFlagErrorFunc(t *testing.T) {
t.Errorf("expected %v, got %v", expected, err.Error())
}
}
+
+// TestSortedFlags checks,
+// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.
+// https://github.com/spf13/cobra/issues/404
+func TestSortedFlags(t *testing.T) {
+ cmd := &Command{}
+ cmd.Flags().SortFlags = false
+ names := []string{"C", "B", "A", "D"}
+ for _, name := range names {
+ cmd.Flags().Bool(name, false, "")
+ }
+
+ i := 0
+ cmd.LocalFlags().VisitAll(func(f *pflag.Flag) {
+ if i == len(names) {
+ return
+ }
+ if contains(f.Name, names) {
+ if names[i] != f.Name {
+ t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)
+ }
+ i++
+ }
+ })
+}
+
+// contains checks, if s is in ss.
+func contains(s string, ss []string) bool {
+ for _, v := range ss {
+ if v == s {
+ return true
+ }
+ }
+ return false
+}