mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Edit ResetFlags and ResetCommands descriptions
This commit is contained in:
parent
65c8acb228
commit
d6a430541c
2 changed files with 31 additions and 48 deletions
|
@ -875,7 +875,7 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`,
|
||||||
c.AddCommand(c.helpCommand)
|
c.AddCommand(c.helpCommand)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResetCommands used for testing.
|
// ResetCommands delete parent, subcommand and help command from c.
|
||||||
func (c *Command) ResetCommands() {
|
func (c *Command) ResetCommands() {
|
||||||
c.parent = nil
|
c.parent = nil
|
||||||
c.commands = nil
|
c.commands = nil
|
||||||
|
@ -1271,7 +1271,7 @@ func (c *Command) PersistentFlags() *flag.FlagSet {
|
||||||
return c.pflags
|
return c.pflags
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResetFlags is used in testing.
|
// ResetFlags deletes all flags from command.
|
||||||
func (c *Command) ResetFlags() {
|
func (c *Command) ResetFlags() {
|
||||||
c.flagErrorBuf = new(bytes.Buffer)
|
c.flagErrorBuf = new(bytes.Buffer)
|
||||||
c.flagErrorBuf.Reset()
|
c.flagErrorBuf.Reset()
|
||||||
|
|
|
@ -32,6 +32,18 @@ func resetCommandLineFlagSet() {
|
||||||
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
|
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkStringContains(t *testing.T, got, expected string) {
|
||||||
|
if !strings.Contains(got, expected) {
|
||||||
|
t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkStringOmits(t *testing.T, got, expected string) {
|
||||||
|
if strings.Contains(got, expected) {
|
||||||
|
t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSingleCommand(t *testing.T) {
|
func TestSingleCommand(t *testing.T) {
|
||||||
var rootCmdArgs []string
|
var rootCmdArgs []string
|
||||||
rootCmd := &Command{
|
rootCmd := &Command{
|
||||||
|
@ -400,9 +412,7 @@ func TestChildFlagWithParentLocalFlag(t *testing.T) {
|
||||||
t.Errorf("Invalid flag should generate error")
|
t.Errorf("Invalid flag should generate error")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(err.Error(), "unknown shorthand") {
|
checkStringContains(t, err.Error(), "unknown shorthand")
|
||||||
t.Errorf("Wrong error message: %q", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if intFlagValue != 7 {
|
if intFlagValue != 7 {
|
||||||
t.Errorf("Expected flag value: %v, got %v", 7, intFlagValue)
|
t.Errorf("Expected flag value: %v, got %v", 7, intFlagValue)
|
||||||
|
@ -418,9 +428,7 @@ func TestFlagInvalidInput(t *testing.T) {
|
||||||
t.Errorf("Invalid flag value should generate error")
|
t.Errorf("Invalid flag value should generate error")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(err.Error(), "invalid syntax") {
|
checkStringContains(t, err.Error(), "invalid syntax")
|
||||||
t.Errorf("Wrong error message: %q", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFlagBeforeCommand(t *testing.T) {
|
func TestFlagBeforeCommand(t *testing.T) {
|
||||||
|
@ -731,9 +739,7 @@ func TestHelpCommandExecuted(t *testing.T) {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(output, rootCmd.Long) {
|
checkStringContains(t, output, rootCmd.Long)
|
||||||
t.Errorf("Expected to contain: %q, got: %q", rootCmd.Long, output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHelpCommandExecutedOnChild(t *testing.T) {
|
func TestHelpCommandExecutedOnChild(t *testing.T) {
|
||||||
|
@ -746,9 +752,7 @@ func TestHelpCommandExecutedOnChild(t *testing.T) {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(output, childCmd.Long) {
|
checkStringContains(t, output, childCmd.Long)
|
||||||
t.Errorf("Expected to contain: %q, got: %q", childCmd.Long, output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetHelpCommand(t *testing.T) {
|
func TestSetHelpCommand(t *testing.T) {
|
||||||
|
@ -782,9 +786,7 @@ func TestHelpFlagExecuted(t *testing.T) {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(output, rootCmd.Long) {
|
checkStringContains(t, output, rootCmd.Long)
|
||||||
t.Errorf("Expected to contain: %q, got: %q", rootCmd.Long, output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHelpFlagExecutedOnChild(t *testing.T) {
|
func TestHelpFlagExecutedOnChild(t *testing.T) {
|
||||||
|
@ -797,9 +799,7 @@ func TestHelpFlagExecutedOnChild(t *testing.T) {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(output, childCmd.Long) {
|
checkStringContains(t, output, childCmd.Long)
|
||||||
t.Errorf("Expected to contain: %q, got: %q", childCmd.Long, output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestHelpFlagInHelp checks,
|
// TestHelpFlagInHelp checks,
|
||||||
|
@ -807,22 +807,17 @@ func TestHelpFlagExecutedOnChild(t *testing.T) {
|
||||||
// that has no other flags.
|
// that has no other flags.
|
||||||
// Related to https://github.com/spf13/cobra/issues/302.
|
// Related to https://github.com/spf13/cobra/issues/302.
|
||||||
func TestHelpFlagInHelp(t *testing.T) {
|
func TestHelpFlagInHelp(t *testing.T) {
|
||||||
output := new(bytes.Buffer)
|
parentCmd := &Command{Use: "parent", Run: func(*Command, []string) {}}
|
||||||
parent := &Command{Use: "parent", Run: func(*Command, []string) {}}
|
|
||||||
parent.SetOutput(output)
|
|
||||||
|
|
||||||
child := &Command{Use: "child", Run: func(*Command, []string) {}}
|
childCmd := &Command{Use: "child", Run: func(*Command, []string) {}}
|
||||||
parent.AddCommand(child)
|
parentCmd.AddCommand(childCmd)
|
||||||
|
|
||||||
parent.SetArgs([]string{"help", "child"})
|
output, err := executeCommand(parentCmd, "help", "child")
|
||||||
err := parent.Execute()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(output.String(), "[flags]") {
|
checkStringContains(t, output, "[flags]")
|
||||||
t.Errorf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFlagsInUsage(t *testing.T) {
|
func TestFlagsInUsage(t *testing.T) {
|
||||||
|
@ -832,10 +827,7 @@ func TestFlagsInUsage(t *testing.T) {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := "[flags]"
|
checkStringContains(t, output, "[flags]")
|
||||||
if !strings.Contains(output, expected) {
|
|
||||||
t.Errorf("Expected to contain %q\ngot: %v", expected, output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
|
func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
|
||||||
|
@ -848,9 +840,7 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(output, childCmd.Long) {
|
checkStringContains(t, output, childCmd.Long)
|
||||||
t.Errorf("Expected to contain: %q, got: %q", childCmd.Long, output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUsageIsNotPrintedTwice(t *testing.T) {
|
func TestUsageIsNotPrintedTwice(t *testing.T) {
|
||||||
|
@ -997,10 +987,7 @@ func TestDeprecatedCommand(t *testing.T) {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := deprecatedCmd.Deprecated
|
checkStringContains(t, output, deprecatedCmd.Deprecated)
|
||||||
if !strings.Contains(output, expected) {
|
|
||||||
t.Errorf("Expected to contain: %q\nGot:%q", expected, output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHooks(t *testing.T) {
|
func TestHooks(t *testing.T) {
|
||||||
|
@ -1248,9 +1235,7 @@ func TestFlagOnPflagCommandLine(t *testing.T) {
|
||||||
c.AddCommand(&Command{Use: "child", Run: emptyRun})
|
c.AddCommand(&Command{Use: "child", Run: emptyRun})
|
||||||
|
|
||||||
output, _ := executeCommand(c, "--help")
|
output, _ := executeCommand(c, "--help")
|
||||||
if !strings.Contains(output, flagName) {
|
checkStringContains(t, output, flagName)
|
||||||
t.Errorf("Expected to contain: %q\nGot: %v", flagName, output)
|
|
||||||
}
|
|
||||||
|
|
||||||
resetCommandLineFlagSet()
|
resetCommandLineFlagSet()
|
||||||
}
|
}
|
||||||
|
@ -1406,9 +1391,7 @@ func TestUseDeprecatedFlags(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Unexpected error:", err)
|
t.Error("Unexpected error:", err)
|
||||||
}
|
}
|
||||||
if !strings.Contains(output, "This flag is deprecated") {
|
checkStringContains(t, output, "This flag is deprecated")
|
||||||
t.Errorf("Expected to contain deprecated message, but got %q", output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTraverseWithParentFlags(t *testing.T) {
|
func TestTraverseWithParentFlags(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue