Non-DI approach to the exec.Command mocking

This commit is contained in:
Erin Call 2019-12-05 09:57:59 -08:00
parent 990d1856d8
commit e77f6842b9
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
3 changed files with 15 additions and 9 deletions

View file

@ -41,6 +41,10 @@ type execCmd struct {
*exec.Cmd
}
var Command = func() cmd {
return &execCmd{}
}
func (c *execCmd) Path(p string) { c.Cmd.Path = p }
func (c *execCmd) Args(a []string) { c.Cmd.Args = a }
func (c *execCmd) Env(e []string) { c.Cmd.Env = e }

View file

@ -5,13 +5,9 @@ import ()
const HELM_BIN = "/usr/bin/helm"
func Install(args ...string) error {
cmd := &execCmd{}
cmd := Command()
cmd.Path(HELM_BIN)
return install(cmd, args)
}
func install(cmd cmd, args []string) error {
args = append([]string{"install"}, args...)
cmd.Args(args)

View file

@ -9,12 +9,18 @@ func TestInstall(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
cmd := NewMockcmd(ctrl)
cmd.EXPECT().
mCmd := NewMockcmd(ctrl)
originalCommand := Command
Command = func() cmd { return mCmd }
defer func() { Command = originalCommand }()
mCmd.EXPECT().
Path(HELM_BIN)
mCmd.EXPECT().
Args(gomock.Eq([]string{"install", "arg1", "arg2"}))
cmd.EXPECT().
mCmd.EXPECT().
Run().
Times(1)
install(cmd, []string{"arg1", "arg2"})
Install("arg1", "arg2")
}