mirror of
https://github.com/spf13/cobra
synced 2024-11-05 05:17:12 +00:00
6d2f1d2fa7
Added the ability to have hidden commands that cobra will still run as intended, however they won't show up in any usage/help text adding internal field to command private is a better name hiding private commands in default help/usage opting for 'hidden' over 'private' updating all 'help command' checks to exclude hidden commands updating how commands are displayed in usage/help text by updating/adding some methods. added tests for hidden/deprecated commands making command hidden when testing hidden command execution test now leverage the included suite and are much less custom. also removed deprecation tests, once I discovered them in cobra_test.go updating hidden command test to be more reliable removing unnecessary () when checking len(c.Deprecated) updating command comments to be godoc friendly
114 lines
2.5 KiB
Go
114 lines
2.5 KiB
Go
package cobra
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
// test to ensure hidden commands run as intended
|
|
func TestHiddenCommandExecutes(t *testing.T) {
|
|
|
|
// ensure that outs does not already equal what the command will be setting it
|
|
// to, if it did this test would not actually be testing anything...
|
|
if outs == "hidden" {
|
|
t.Errorf("outs should NOT EQUAL hidden")
|
|
}
|
|
|
|
cmdHidden.Execute()
|
|
|
|
// upon running the command, the value of outs should now be 'hidden'
|
|
if outs != "hidden" {
|
|
t.Errorf("Hidden command failed to run!")
|
|
}
|
|
}
|
|
|
|
// test to ensure hidden commands do not show up in usage/help text
|
|
func TestHiddenCommandIsHidden(t *testing.T) {
|
|
if cmdHidden.IsAvailableCommand() {
|
|
t.Errorf("Hidden command found!")
|
|
}
|
|
}
|
|
|
|
func TestStripFlags(t *testing.T) {
|
|
tests := []struct {
|
|
input []string
|
|
output []string
|
|
}{
|
|
{
|
|
[]string{"foo", "bar"},
|
|
[]string{"foo", "bar"},
|
|
},
|
|
{
|
|
[]string{"foo", "--bar", "-b"},
|
|
[]string{"foo"},
|
|
},
|
|
{
|
|
[]string{"-b", "foo", "--bar", "bar"},
|
|
[]string{},
|
|
},
|
|
{
|
|
[]string{"-i10", "echo"},
|
|
[]string{"echo"},
|
|
},
|
|
{
|
|
[]string{"-i=10", "echo"},
|
|
[]string{"echo"},
|
|
},
|
|
{
|
|
[]string{"--int=100", "echo"},
|
|
[]string{"echo"},
|
|
},
|
|
{
|
|
[]string{"-ib", "echo", "-bfoo", "baz"},
|
|
[]string{"echo", "baz"},
|
|
},
|
|
{
|
|
[]string{"-i=baz", "bar", "-i", "foo", "blah"},
|
|
[]string{"bar", "blah"},
|
|
},
|
|
{
|
|
[]string{"--int=baz", "-bbar", "-i", "foo", "blah"},
|
|
[]string{"blah"},
|
|
},
|
|
{
|
|
[]string{"--cat", "bar", "-i", "foo", "blah"},
|
|
[]string{"bar", "blah"},
|
|
},
|
|
{
|
|
[]string{"-c", "bar", "-i", "foo", "blah"},
|
|
[]string{"bar", "blah"},
|
|
},
|
|
{
|
|
[]string{"--persist", "bar"},
|
|
[]string{"bar"},
|
|
},
|
|
{
|
|
[]string{"-p", "bar"},
|
|
[]string{"bar"},
|
|
},
|
|
}
|
|
|
|
cmdPrint := &Command{
|
|
Use: "print [string to print]",
|
|
Short: "Print anything to the screen",
|
|
Long: `an utterly useless command for testing.`,
|
|
Run: func(cmd *Command, args []string) {
|
|
tp = args
|
|
},
|
|
}
|
|
|
|
var flagi int
|
|
var flagstr string
|
|
var flagbool bool
|
|
cmdPrint.PersistentFlags().BoolVarP(&flagbool, "persist", "p", false, "help for persistent one")
|
|
cmdPrint.Flags().IntVarP(&flagi, "int", "i", 345, "help message for flag int")
|
|
cmdPrint.Flags().StringVarP(&flagstr, "bar", "b", "bar", "help message for flag string")
|
|
cmdPrint.Flags().BoolVarP(&flagbool, "cat", "c", false, "help message for flag bool")
|
|
|
|
for _, test := range tests {
|
|
output := stripFlags(test.input, cmdPrint)
|
|
if !reflect.DeepEqual(test.output, output) {
|
|
t.Errorf("expected: %v, got: %v", test.output, output)
|
|
}
|
|
}
|
|
}
|