mirror of
https://github.com/spf13/cobra.git
synced 2024-11-15 01:38:17 -07:00
Better testing support. Easy to reset to blank slate in tests.
This commit is contained in:
parent
a618569eea
commit
c568b575aa
5
cobra.go
5
cobra.go
@ -150,6 +150,11 @@ func (c *Command) execute(args []string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
// Used for testing
|
||||
func (c *Command) ResetCommands() {
|
||||
c.commands = nil
|
||||
}
|
||||
|
||||
// Add one or many commands as children of this
|
||||
func (c *Command) AddCommand(cmds ...*Command) {
|
||||
for i, x := range cmds {
|
||||
|
@ -57,11 +57,18 @@ func flagInit() {
|
||||
cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree")
|
||||
}
|
||||
|
||||
func commandInit() {
|
||||
cmdEcho.ResetCommands()
|
||||
cmdPrint.ResetCommands()
|
||||
cmdTimes.ResetCommands()
|
||||
}
|
||||
|
||||
func initialize() *Commander {
|
||||
tt, tp, te = nil, nil, nil
|
||||
var c = NewCommander()
|
||||
c.SetName("cobra test")
|
||||
|
||||
flagInit()
|
||||
commandInit()
|
||||
return c
|
||||
}
|
||||
|
||||
@ -103,7 +110,6 @@ func TestChildCommand(t *testing.T) {
|
||||
func TestFlagLong(t *testing.T) {
|
||||
c := initialize()
|
||||
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
||||
flagInit()
|
||||
c.SetArgs(strings.Split("echo --intone=13 something here", " "))
|
||||
c.Execute()
|
||||
|
||||
@ -121,7 +127,6 @@ func TestFlagLong(t *testing.T) {
|
||||
func TestFlagShort(t *testing.T) {
|
||||
c := initialize()
|
||||
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
||||
flagInit()
|
||||
c.SetArgs(strings.Split("echo -i13 something here", " "))
|
||||
c.Execute()
|
||||
|
||||
@ -137,7 +142,6 @@ func TestFlagShort(t *testing.T) {
|
||||
|
||||
c = initialize()
|
||||
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
||||
flagInit()
|
||||
c.SetArgs(strings.Split("echo -i 13 something here", " "))
|
||||
c.Execute()
|
||||
|
||||
@ -154,7 +158,6 @@ func TestFlagShort(t *testing.T) {
|
||||
// Testing same shortcode, different command
|
||||
c = initialize()
|
||||
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
||||
flagInit()
|
||||
c.SetArgs(strings.Split("print -i99 one two", " "))
|
||||
c.Execute()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user