processtest

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func (*Fake) Calls

func (f *Fake) Calls() []Call

Calls returns an independent snapshot of recorded commands and arguments.

func (*Fake) Queue

func (f *Fake) Queue(results ...process.Result)

Queue appends results consumed by subsequent Run calls. A result's Err is returned as the execution error. Nonzero exit codes alone do not return errors.

func (*Fake) Run

func (f *Fake) Run(ctx context.Context, name string, args ...string) (process.Result, error)

Run records a call and consumes its next response without starting a process. A canceled context is recorded but does not consume a queued response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL