mirror of https://github.com/spf13/cobra.git
Add TestHelpFlagInHelp
This commit is contained in:
parent
867d414548
commit
ea3c5beeff
|
@ -3,6 +3,7 @@ package cobra
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -1153,6 +1154,9 @@ func TestFlagOnPflagCommandLine(t *testing.T) {
|
||||||
r := fullSetupTest("--help")
|
r := fullSetupTest("--help")
|
||||||
|
|
||||||
checkResultContains(t, r, flagName)
|
checkResultContains(t, r, flagName)
|
||||||
|
|
||||||
|
// reset CommandLine flagset
|
||||||
|
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddTemplateFunctions(t *testing.T) {
|
func TestAddTemplateFunctions(t *testing.T) {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/pflag"
|
"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"}
|
as := []string{"-v", "-race", "-file", "foo.go"}
|
||||||
targs := []string{}
|
targs := []string{}
|
||||||
cmdPrint := &Command{
|
cmdPrint := &Command{
|
||||||
|
@ -226,7 +227,8 @@ func TestFlagErrorFunc(t *testing.T) {
|
||||||
|
|
||||||
// TestSortedFlags checks,
|
// TestSortedFlags checks,
|
||||||
// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.
|
// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.
|
||||||
// https://github.com/spf13/cobra/issues/404
|
//
|
||||||
|
// Source: https://github.com/spf13/cobra/issues/404
|
||||||
func TestSortedFlags(t *testing.T) {
|
func TestSortedFlags(t *testing.T) {
|
||||||
cmd := &Command{}
|
cmd := &Command{}
|
||||||
cmd.Flags().SortFlags = false
|
cmd.Flags().SortFlags = false
|
||||||
|
@ -240,7 +242,7 @@ func TestSortedFlags(t *testing.T) {
|
||||||
if i == len(names) {
|
if i == len(names) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if contains(f.Name, names) {
|
if isStringInStringSlice(f.Name, names) {
|
||||||
if names[i] != f.Name {
|
if names[i] != f.Name {
|
||||||
t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)
|
t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)
|
||||||
}
|
}
|
||||||
|
@ -250,7 +252,7 @@ func TestSortedFlags(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// contains checks, if s is in ss.
|
// 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 {
|
for _, v := range ss {
|
||||||
if v == s {
|
if v == s {
|
||||||
return true
|
return true
|
||||||
|
@ -258,3 +260,27 @@ func contains(s string, ss []string) bool {
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestHelpFlagInHelp checks,
|
||||||
|
// if '--help' flag is shown in help for child (executing `parent help child`),
|
||||||
|
// that has no other flags.
|
||||||
|
//
|
||||||
|
// Source: https://github.com/spf13/cobra/issues/302
|
||||||
|
func TestHelpFlagInHelp(t *testing.T) {
|
||||||
|
output := new(bytes.Buffer)
|
||||||
|
parent := &Command{Use: "parent", Long: "long", Run: func(*Command, []string) { return }}
|
||||||
|
parent.SetOutput(output)
|
||||||
|
|
||||||
|
child := &Command{Use: "child", Long: "long", Run: func(*Command, []string) { return }}
|
||||||
|
parent.AddCommand(child)
|
||||||
|
|
||||||
|
parent.SetArgs([]string{"help", "child"})
|
||||||
|
err := parent.Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(output.String(), "[flags]") {
|
||||||
|
t.Fatalf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue