testutil

package
v0.17.17 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package testutil provides shared helpers for sprout's test suite.

It is intentionally leaf-only (no imports of internal sprout packages) so any package's internal tests can import it without creating cycles.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureStdout

func CaptureStdout(t *testing.T, fn func()) string

CaptureStdout runs fn with os.Stdout redirected to an in-memory pipe and returns whatever fn wrote to stdout.

A goroutine drains the pipe's read end concurrently with fn's writes so callers can emit arbitrarily large output without deadlocking the writer when the OS pipe buffer (~64 KiB on Linux) fills. Without the concurrent reader, an output larger than the pipe buffer blocks the writer inside fn and the test hangs until its timeout.

os.Stdout is restored to its original value before this function returns. t.Cleanup is registered as a safety net so the restoration also happens if fn panics (in which case the explicit restore never runs but the cleanup does, once the testing framework recovers).

Only os.Stdout is redirected; os.Stderr is left untouched. Use a similar helper for stderr if you need it.

The supplied *testing.T is used for t.Helper (so failures are reported at the caller's line) and t.Cleanup. Pass the test's t.

func CaptureStdoutPanicking

func CaptureStdoutPanicking(fn func()) string

CaptureStdoutPanicking is like CaptureStdout but for callers that cannot supply a *testing.T (legacy helpers, helpers used from non-test contexts). On os.Pipe failure it panics with a descriptive message; on a panicking fn, os.Stdout is restored via defer.

Prefer CaptureStdout when *testing.T is available.

func GitSandbox added in v0.17.17

func GitSandbox(t *testing.T) string

GitSandbox returns an initialized throwaway git repository suitable for tests that execute real git commands.

History-rewriting or side-effecting git commands (rebase, reset, push, clean, checkout …) must never be able to escape into a real checkout: a test whose "it will fail harmlessly in the temp workspace" assumption silently breaks (cwd fallback, discovery walk-up) ends up operating on the developer's actual repository. Two defenses:

  1. The sandbox is an initialized repo with zero commits, so most history-rewrite commands fail inside it ("no commits yet", "unknown revision") instead of touching anything else.
  2. GIT_CEILING_DIRECTORIES is set to the sandbox root for the duration of the test, so git never walks UP past the sandbox looking for an enclosing .git — even if the command's working directory resolves somewhere unexpected.

The env var is set via t.Setenv, so it is restored automatically; tests using GitSandbox must not call t.Parallel (t.Setenv panics if they do).

Types

This section is empty.

Jump to

Keyboard shortcuts

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