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