Don’t shell out commands in Go tests

The changes in 0ed14758b7 mess up `go test`’s exit code since it executes test commands with exec(3) - `go test` always return 0. We need to stop shelling out commands in tests.
This commit is contained in:
Jingwen Owen Ou 2014-12-22 08:34:57 -08:00
Родитель 283782497b
Коммит 0270835a07
2 изменённых файлов: 15 добавлений и 4 удалений

Просмотреть файл

@ -39,10 +39,14 @@ func newExecError(err error) ExecError {
type Runner struct { type Runner struct {
commands map[string]*Command commands map[string]*Command
execute func(cmds []*cmd.Cmd) error
} }
func NewRunner() *Runner { func NewRunner() *Runner {
return &Runner{commands: make(map[string]*Command)} return &Runner{
commands: make(map[string]*Command),
execute: executeCommands,
}
} }
func (r *Runner) All() map[string]*Command { func (r *Runner) All() map[string]*Command {
@ -93,7 +97,7 @@ func (r *Runner) Call(cmd *Command, args *Args) ExecError {
if args.Noop { if args.Noop {
printCommands(cmds) printCommands(cmds)
} else { } else {
err = executeCommands(cmds) err = r.execute(cmds)
} }
return newExecError(err) return newExecError(err)

Просмотреть файл

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/github/hub/Godeps/_workspace/src/github.com/bmizerany/assert" "github.com/github/hub/Godeps/_workspace/src/github.com/bmizerany/assert"
"github.com/github/hub/cmd"
) )
func TestRunner_splitAliasCmd(t *testing.T) { func TestRunner_splitAliasCmd(t *testing.T) {
@ -19,7 +20,10 @@ func TestRunner_splitAliasCmd(t *testing.T) {
} }
func TestRunnerUseCommands(t *testing.T) { func TestRunnerUseCommands(t *testing.T) {
r := NewRunner() r := &Runner{
commands: make(map[string]*Command),
execute: func(cmds []*cmd.Cmd) error { return nil },
}
c := &Command{Usage: "foo"} c := &Command{Usage: "foo"}
r.Use(c) r.Use(c)
@ -33,7 +37,10 @@ func TestRunnerCallCommands(t *testing.T) {
args.Replace("git", "version", "") args.Replace("git", "version", "")
} }
r := NewRunner() r := &Runner{
commands: make(map[string]*Command),
execute: func(cmds []*cmd.Cmd) error { return nil },
}
c := &Command{Usage: "foo", Run: f} c := &Command{Usage: "foo", Run: f}
r.Use(c) r.Use(c)