mirror of
https://github.com/spf13/cobra
synced 2025-05-05 12:57:22 +00:00
add test cases
This commit is contained in:
parent
2d6d8c7aec
commit
6495e27444
2 changed files with 87 additions and 3 deletions
|
@ -1,3 +0,0 @@
|
||||||
Things to fix:
|
|
||||||
* default --version flag is not shown when running `tool help` but is shown when running `tool` or `tool --help` by itself
|
|
||||||
* `tool --help command` shows general help instead of `tool command --help` results
|
|
|
@ -2161,3 +2161,90 @@ func TestSetContextPersistentPreRun(t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const VERSION_FLAG = "--version"
|
||||||
|
const HELP_FLAG = "--help"
|
||||||
|
|
||||||
|
func TestNoRootRunCommandExecutedWithVersionSet(t *testing.T) {
|
||||||
|
rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description"}
|
||||||
|
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
|
||||||
|
|
||||||
|
output, err := executeCommand(rootCmd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkStringContains(t, output, rootCmd.Long)
|
||||||
|
checkStringContains(t, output, HELP_FLAG)
|
||||||
|
checkStringContains(t, output, VERSION_FLAG)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNoRootRunCommandExecutedWithoutVersionSet(t *testing.T) {
|
||||||
|
rootCmd := &Command{Use: "root", Long: "Long description"}
|
||||||
|
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
|
||||||
|
|
||||||
|
output, err := executeCommand(rootCmd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkStringContains(t, output, rootCmd.Long)
|
||||||
|
checkStringContains(t, output, HELP_FLAG)
|
||||||
|
checkStringOmits(t, output, VERSION_FLAG)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHelpCommandExecutedWithVersionSet(t *testing.T) {
|
||||||
|
rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description", Run: emptyRun}
|
||||||
|
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
|
||||||
|
|
||||||
|
output, err := executeCommand(rootCmd, "help")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkStringContains(t, output, rootCmd.Long)
|
||||||
|
checkStringContains(t, output, HELP_FLAG)
|
||||||
|
checkStringContains(t, output, VERSION_FLAG)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHelpCommandExecutedWithoutVersionSet(t *testing.T) {
|
||||||
|
rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun}
|
||||||
|
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
|
||||||
|
|
||||||
|
output, err := executeCommand(rootCmd, "help")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkStringContains(t, output, rootCmd.Long)
|
||||||
|
checkStringContains(t, output, HELP_FLAG)
|
||||||
|
checkStringOmits(t, output, VERSION_FLAG)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHelpflagCommandExecutedWithVersionSet(t *testing.T) {
|
||||||
|
rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description", Run: emptyRun}
|
||||||
|
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
|
||||||
|
|
||||||
|
output, err := executeCommand(rootCmd, HELP_FLAG)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkStringContains(t, output, rootCmd.Long)
|
||||||
|
checkStringContains(t, output, HELP_FLAG)
|
||||||
|
checkStringContains(t, output, VERSION_FLAG)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHelpflagCommandExecutedWithoutVersionSet(t *testing.T) {
|
||||||
|
rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun}
|
||||||
|
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
|
||||||
|
|
||||||
|
output, err := executeCommand(rootCmd, HELP_FLAG)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkStringContains(t, output, rootCmd.Long)
|
||||||
|
checkStringContains(t, output, HELP_FLAG)
|
||||||
|
checkStringOmits(t, output, VERSION_FLAG)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue