Skip to content

omegion/go-command

go-command

GithubBuild Coverage Status Go Report Card GoDoc

exec.Command wrapper for better testing.

How does it work?

const dummyCommand = "dummy"

type Foo struct {
    Commander command.Interface
}

func (f Foo) Run() error {
    _, err := f.Commander.Output(dummyCommand)
    if err != nil {
        return err
    }

    return nil
}

func TestCommand_Output(t *testing.T) {
    ctrl := gomock.NewController(t)
    
    defer ctrl.Finish()
    
    m := mocks.NewMockInterface(ctrl)
    
    m.EXPECT().Output(dummyCommand).Return([]byte{}, nil)
    
    foo := Foo{
        Commander: m,
    }
    
    err := foo.Run()
    assert.Equal(t, err, nil)
}