run

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package run provides a small fluent harness for exercising a Command[[]byte, []byte] in tests: configure stdin, run, and inspect the captured output lines and error.

res := run.Command(myCmd).WithStdinLines("a", "b").Run()
// res.Stdout == []string{...}, res.Err == nil

It complements the package-level testable.Test / testable.TestLines helpers with explicit input control (custom readers, injected read errors).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Input added in v0.1.0

type Input string

Input is the full stdin text fed to a command under test.

type Result

type Result struct {
	Err    error
	Stdout []string
}

Result holds the captured output and error from a command execution.

func Quick

func Quick(cmd gloo.Command[[]byte, []byte]) *Result

Quick executes cmd with empty stdin and returns the result.

Example
package main

import (
	"fmt"
	"strings"

	gloo "github.com/gloo-foo/framework"
	"github.com/gloo-foo/framework/patterns"

	"github.com/gloo-foo/testable/run"
)

// upper upper-cases each input line; it stands in for a real cmd-* command.
func upper() gloo.Command[[]byte, []byte] {
	return patterns.Map(func(line []byte) ([]byte, error) {
		return []byte(strings.ToUpper(string(line))), nil
	})
}

func main() {
	res := run.Quick(upper())
	fmt.Println(res.Err)
	fmt.Println(len(res.Stdout))
}
Output:
<nil>
0

func WithInput

func WithInput(cmd gloo.Command[[]byte, []byte], stdin Input) *Result

WithInput executes cmd with the given stdin text and returns the result.

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner configures and executes a command for testing. Every With* method returns a NEW Runner, so a Runner is immutable and safe to reuse.

func Command

func Command(cmd gloo.Command[[]byte, []byte]) Runner

Command creates a Runner for cmd with empty stdin and a background context.

Example
package main

import (
	"fmt"
	"strings"

	gloo "github.com/gloo-foo/framework"
	"github.com/gloo-foo/framework/patterns"

	"github.com/gloo-foo/testable/run"
)

// upper upper-cases each input line; it stands in for a real cmd-* command.
func upper() gloo.Command[[]byte, []byte] {
	return patterns.Map(func(line []byte) ([]byte, error) {
		return []byte(strings.ToUpper(string(line))), nil
	})
}

func main() {
	res := run.Command(upper()).WithStdinLines("alpha", "beta").Run()
	fmt.Println(res.Err)
	fmt.Println(res.Stdout)
}
Output:
<nil>
[ALPHA BETA]

func WithContext

func WithContext(ctx context.Context, cmd gloo.Command[[]byte, []byte]) Runner

WithContext (standalone) creates a Runner bound to ctx.

func (Runner) Run

func (r Runner) Run() *Result

Run executes the configured command and returns the captured result. It is a terminal operation.

func (Runner) WithContext

func (r Runner) WithContext(ctx context.Context) Runner

WithContext returns a copy of the Runner bound to ctx.

func (Runner) WithStdin

func (r Runner) WithStdin(input string) Runner

WithStdin returns a copy of the Runner whose input is input.

func (Runner) WithStdinError

func (r Runner) WithStdinError(err error) Runner

WithStdinError returns a copy of the Runner that fails the input stream with err, for exercising error propagation.

func (Runner) WithStdinLines

func (r Runner) WithStdinLines(lines ...string) Runner

WithStdinLines returns a copy of the Runner whose input is lines joined by newlines (each line, including the last, terminated by '\n').

func (Runner) WithStdinReader

func (r Runner) WithStdinReader(reader io.Reader) Runner

WithStdinReader returns a copy of the Runner reading input from reader.

Jump to

Keyboard shortcuts

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