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 ¶
- func BuildOxBinary(t *testing.T, projectRoot string) string
- func MinimalEnv(testVars []string) []string
- func OxCmd(t *testing.T, oxBin, dir string, envVars []string, args ...string) *exec.Cmd
- func OxCmdContext(t *testing.T, ctx context.Context, oxBin, dir string, envVars []string, ...) *exec.Cmd
- func RequireNoFDLeak(t *testing.T)
- func RequireNoFDLeakWithDelta(t *testing.T, allowedDelta int)
- func RunOx(t *testing.T, oxBin, dir string, envVars []string, args ...string) (string, int, time.Duration)
- func StopDaemonCleanup(t *testing.T, oxBin, repoDir string, envVars []string)
- type MockServer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildOxBinary ¶
BuildOxBinary compiles the ox binary and returns its path. This is the one place where os.Environ() is acceptable (go build only).
func MinimalEnv ¶
MinimalEnv builds a clean environment from the allowlist + caller overrides. Always injects OX_NO_DAEMON=1.
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
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
RequireNoFDLeakWithDelta is like RequireNoFDLeak but accepts a custom delta.
Types ¶
type MockServer ¶
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.