Handle Before and After of Command without handling it as subCommand if there is no subCommand.

This commit is contained in:
Gregor Noczinski
2015-12-25 21:45:58 +01:00
parent c31a797586
commit f90cd56647
3 changed files with 53 additions and 3 deletions

View File

@@ -5,6 +5,8 @@ import (
"flag"
"io/ioutil"
"testing"
"fmt"
"strings"
)
func TestCommandFlagParsing(t *testing.T) {
@@ -43,3 +45,26 @@ func TestCommandFlagParsing(t *testing.T) {
expect(t, []string(context.Args()), c.testArgs)
}
}
func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
app := NewApp()
app.Commands = []Command{
Command{
Name: "bar",
Before: func(c *Context) error { return fmt.Errorf("before error") },
After: func(c *Context) error { return fmt.Errorf("after error") },
},
}
err := app.Run([]string{"foo", "bar"})
if err == nil {
t.Fatalf("expected to recieve error from Run, got none")
}
if !strings.Contains(err.Error(), "before error") {
t.Errorf("expected text of error from Before method, but got none in \"%v\"", err)
}
if !strings.Contains(err.Error(), "after error") {
t.Errorf("expected text of error from After method, but got none in \"%v\"", err)
}
}