Skip to content
Myr

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

2 files+34 added−4 removed

modified:cobra_test.go+4 added
1 unmodified line
23import (4 "bytes"5 "fmt"6 "os"7 "reflect"8 "runtime"9 "strings"10 "testing"
1142 unmodified lines
1153 pflag.CommandLine.String(flagName, "", "about my flag")1154 r := fullSetupTest("--help")11551156 checkResultContains(t, r, flagName)11571158 // reset CommandLine flagset1159 pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)1160}11611162func TestAddTemplateFunctions(t *testing.T) {1163 AddTemplateFunc("t", func() bool { return true })
45 unmodified lines
modified:command_test.go+30 added-4 removed
3 unmodified lines
4 "bytes"5 "fmt"6 "os"7 "reflect"8 "strings"9 "testing"1011 "github.com/spf13/pflag"12)
105 unmodified lines
118 }119 }120}121121func Test_DisableFlagParsing(t *testing.T) {122func TestDisableFlagParsing(t *testing.T) {123 as := []string{"-v", "-race", "-file", "foo.go"}124 targs := []string{}125 cmdPrint := &Command{126 DisableFlagParsing: true,
99 unmodified lines
226}227228// 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/404232func TestSortedFlags(t *testing.T) {233 cmd := &Command{}234 cmd.Flags().SortFlags = false235 names := []string{"C", "B", "A", "D"}
5 unmodified lines
241 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 line
251 })252}253254// contains checks, if s is in ss.253func contains(s string, ss []string) bool {255func isStringInStringSlice(s string, ss []string) bool {256 for _, v := range ss {257 if v == s {258 return true259 }260 }261 return false262}263264// 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/302269func 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)273274 child := &Command{Use: "child", Long: "long", Run: func(*Command, []string) { return }}275 parent.AddCommand(child)276277 parent.SetArgs([]string{"help", "child"})278 err := parent.Execute()279 if err != nil {280 t.Fatal(err)281 }282283 if !strings.Contains(output.String(), "[flags]") {284 t.Fatalf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())285 }286}