Non-DI approach to the exec.Command mocking
This commit is contained in:
parent
990d1856d8
commit
e77f6842b9
|
@ -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 }
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue