testguard

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 10 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 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