Documentation
¶
Overview ¶
Package processtest provides isolated command fakes for applications that accept process.Executor. It never starts a real child process.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnexpectedCommand = errors.New("processtest: unexpected command")
ErrUnexpectedCommand reports a call without a queued response.
Functions ¶
This section is empty.
Types ¶
type Call ¶
type Call struct {
// Name is the requested executable.
Name string
// Args contains the explicit arguments without the executable name.
Args []string
}
Call records one execution request. Args is independent of the caller's slice.
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake implements process.Executor with FIFO responses and recorded calls. Its zero value is ready to use and rejects unconfigured calls. Each instance owns its state and is safe for concurrent use. Concurrent calls consume queued responses in lock acquisition order.
Example ¶
ExampleFake shows an application dependency with queued responses and recorded calls.
package main
import (
"context"
"errors"
"fmt"
"strings"
"github.com/goforj/process"
"github.com/goforj/process/processtest"
)
func main() {
status := func(ctx context.Context, commands process.Executor) (string, error) {
result, err := commands.Run(ctx, "git", "status", "--short")
if err != nil {
return "", err
}
if err := result.EnsureSuccessful(); err != nil {
return "", err
}
return strings.TrimSpace(result.Stdout), nil
}
var fake processtest.Fake
fake.Queue(process.Result{Stdout: " M README.md\n"})
output, err := status(context.Background(), &fake)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(output)
call := fake.Calls()[0]
fmt.Println(call.Name, strings.Join(call.Args, " "))
_, err = fake.Run(context.Background(), "unexpected-command")
fmt.Println(errors.Is(err, processtest.ErrUnexpectedCommand))
}
Output: M README.md git status --short true