51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package exec
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
// mockProvider is a configurable Provider used to prove the interface can be
|
|
// mocked. It mocks the interface, not a concrete type, per the test
|
|
// conventions.
|
|
type mockProvider struct {
|
|
execute func(ctx context.Context, req ExecRequest) (*ExecResult, error)
|
|
}
|
|
|
|
func (m mockProvider) Execute(ctx context.Context, req ExecRequest) (*ExecResult, error) {
|
|
if m.execute == nil {
|
|
return &ExecResult{}, nil
|
|
}
|
|
return m.execute(ctx, req)
|
|
}
|
|
|
|
// TestProvider_InterfaceConformance is a compile-time assertion that both the
|
|
// mock and the real executor satisfy Provider.
|
|
func TestProvider_InterfaceConformance(t *testing.T) {
|
|
t.Parallel()
|
|
var _ Provider = mockProvider{}
|
|
var _ Provider = (*TerminalExecutor)(nil)
|
|
}
|
|
|
|
// TestProvider_MockRoundTrip exercises the interface through a mock without any
|
|
// real process.
|
|
func TestProvider_MockRoundTrip(t *testing.T) {
|
|
t.Parallel()
|
|
var got ExecRequest
|
|
p := mockProvider{execute: func(ctx context.Context, req ExecRequest) (*ExecResult, error) {
|
|
got = req
|
|
return &ExecResult{Stdout: "ok"}, nil
|
|
}}
|
|
|
|
res, err := p.Execute(context.Background(), ExecRequest{Command: []string{"echo", "hi"}})
|
|
if err != nil {
|
|
t.Fatalf("Execute error: %v", err)
|
|
}
|
|
if res.Stdout != "ok" {
|
|
t.Errorf("Stdout = %q, want ok", res.Stdout)
|
|
}
|
|
if len(got.Command) != 2 || got.Command[0] != "echo" {
|
|
t.Errorf("got.Command = %v, want [echo hi]", got.Command)
|
|
}
|
|
}
|