testguard

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package testguard provides test isolation primitives that prevent ox subprocesses from hitting production endpoints during tests.

All test code that runs ox as a subprocess MUST use RunOx or OxCmd instead of exec.Command + os.Environ(). This ensures:

  • minimal environment (no leaked auth, daemon sockets, or SAGEOX_ENDPOINT)
  • OX_NO_DAEMON=1 always injected
  • production hostnames in env values cause immediate test failure
  • mock server responses are validated for production URL leaks

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildOxBinary

func BuildOxBinary(t *testing.T, projectRoot string) string

BuildOxBinary compiles the ox binary and returns its path. This is the one place where os.Environ() is acceptable (go build only).

func MinimalEnv

func MinimalEnv(testVars []string) []string

MinimalEnv builds a clean environment from the allowlist + caller overrides. Always injects OX_NO_DAEMON=1.

func OxCmd

func OxCmd(t *testing.T, oxBin, dir string, envVars []string, args ...string) *exec.Cmd

OxCmd builds an isolated exec.Cmd for running the ox binary in tests.

func OxCmdContext

func OxCmdContext(t *testing.T, ctx context.Context, oxBin, dir string, envVars []string, args ...string) *exec.Cmd

OxCmdContext builds an isolated exec.Cmd with an explicit context. Validates that no env value contains production hostnames.

func RequireNoFDLeak added in v0.6.2

func RequireNoFDLeak(t *testing.T)

RequireNoFDLeak snapshots the open file descriptor count at call time and registers a t.Cleanup that fails the test if FDs grew beyond a small delta. Use it in any test that exercises resource-acquiring code (fsnotify watchers, go-git repos, database connections) to catch leaked handles.

The allowed delta accounts for runtime noise (GC finalizers, runtime file table resizing). Set it higher only if the test legitimately opens long-lived descriptors that outlive the cleanup.

Platforms: works on macOS and Linux via dup2 probing. Silently skips on unsupported platforms (Windows).

func RequireNoFDLeakWithDelta added in v0.6.2

func RequireNoFDLeakWithDelta(t *testing.T, allowedDelta int)

RequireNoFDLeakWithDelta is like RequireNoFDLeak but accepts a custom delta.

func RunOx

func RunOx(t *testing.T, oxBin, dir string, envVars []string, args ...string) (string, int, time.Duration)

RunOx executes an ox subprocess with isolated environment. Returns combined output, exit code, and duration. Uses a 60-second timeout to prevent hangs.

func StopDaemonCleanup

func StopDaemonCleanup(t *testing.T, oxBin, repoDir string, envVars []string)

StopDaemonCleanup registers a cleanup that stops any daemon in the test env.

Types

type MockServer

type MockServer = httptest.Server

MockServer is an alias for httptest.Server, returned by SafeMockServer.

func SafeMockServer

func SafeMockServer(t *testing.T, handler http.Handler) *MockServer

SafeMockServer wraps an http.Handler to validate that response bodies never contain production URLs. If detected, the test fails immediately.

Jump to

Keyboard shortcuts

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