Add TestHelpFlagInHelp
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent 867d414 · Browse files · 2 files·+34 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+34 added−4 removed
- other:+34 added−4 removed
Provenance
- Commit
ea3c5be- Tree
9a9ccd0- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
867d414
2 files changed+34 added−4 removed
modified:cobra_test.go+4 added
1 unmodified line2 3 import (4 "bytes"5 "fmt"6+ "os"7 "reflect"8 "runtime"9 "strings"10 "testing"1142 unmodified lines1153 pflag.CommandLine.String(flagName, "", "about my flag")1154 r := fullSetupTest("--help")1155 1156 checkResultContains(t, r, flagName)1157+1158+ // reset CommandLine flagset1159+ pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)1160 }1161 1162 func TestAddTemplateFunctions(t *testing.T) {1163 AddTemplateFunc("t", func() bool { return true })45 unmodified linesmodified:command_test.go+30 added-4 removed
3 unmodified lines4 "bytes"5 "fmt"6 "os"7 "reflect"8+ "strings"9 "testing"10 11 "github.com/spf13/pflag"12 )105 unmodified lines118 }119 }120 }121 121-func Test_DisableFlagParsing(t *testing.T) {122+func TestDisableFlagParsing(t *testing.T) {123 as := []string{"-v", "-race", "-file", "foo.go"}124 targs := []string{}125 cmdPrint := &Command{126 DisableFlagParsing: true,99 unmodified lines226 }227 228 // TestSortedFlags checks,229 // if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.230+//229-// https://github.com/spf13/cobra/issues/404231+// Source: https://github.com/spf13/cobra/issues/404232 func TestSortedFlags(t *testing.T) {233 cmd := &Command{}234 cmd.Flags().SortFlags = false235 names := []string{"C", "B", "A", "D"}5 unmodified lines241 cmd.LocalFlags().VisitAll(func(f *pflag.Flag) {242 if i == len(names) {243 return244 }243- if contains(f.Name, names) {245+ if isStringInStringSlice(f.Name, names) {246 if names[i] != f.Name {247 t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)248 }249 i++1 unmodified line251 })252 }253 254 // contains checks, if s is in ss.253-func contains(s string, ss []string) bool {255+func isStringInStringSlice(s string, ss []string) bool {256 for _, v := range ss {257 if v == s {258 return true259 }260 }261 return false262 }263+264+// TestHelpFlagInHelp checks,265+// if '--help' flag is shown in help for child (executing `parent help child`),266+// that has no other flags.267+//268+// Source: https://github.com/spf13/cobra/issues/302269+func TestHelpFlagInHelp(t *testing.T) {270+ output := new(bytes.Buffer)271+ parent := &Command{Use: "parent", Long: "long", Run: func(*Command, []string) { return }}272+ parent.SetOutput(output)273+274+ child := &Command{Use: "child", Long: "long", Run: func(*Command, []string) { return }}275+ parent.AddCommand(child)276+277+ parent.SetArgs([]string{"help", "child"})278+ err := parent.Execute()279+ if err != nil {280+ t.Fatal(err)281+ }282+283+ if !strings.Contains(output.String(), "[flags]") {284+ t.Fatalf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())285+ }286+}