Documentation
¶
Overview ¶
Package testenv isolates a test process from the ambient machine state documented in internal/envguard: WB_AGENT_* variables inherited from whichever agent is operating the shell that launched `go test`, and an ambient GOWORK the test process would otherwise carry into every `go` invocation it makes.
WB's own test suite runs as a subprocess of that operating agent. A test that asserts "no active owner" or "no live registered session" observes the outer agent's identity instead of a clean slate unless it calls Isolate first: WB_AGENT_PID/WB_AGENT_RUNTIME/WB_AGENT_MODEL/WB_AGENT_ID are inherited by the `go test` binary itself, not only by subprocesses wb spawns, so internal/envguard's subprocess-environment sanitizing cannot reach them.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConfigureGitAutoMaintenanceOff ¶ added in v0.164.0
ConfigureGitAutoMaintenanceOff runs `git config` inside repoPath to disable gc.auto, maintenance.auto and receive.autogc directly in that repository's own config file.
This is REQUIRED, not merely redundant, for any bare repository a test fixture pushes to over a local transport: git strips every GIT_CONFIG_* variable from the environment it hands to the server-side `receive-pack` it spawns for that push, so SetGitAutoMaintenanceOff/GitAutoMaintenanceOffEnv alone never reaches it. #711's review traced this directly: a bare origin.git configured only through the env still started `gc --auto` 34 times from inside receive-pack, while every directly-started git subprocess (the clones) dropped to 0.
func GitAutoMaintenanceOffEnv ¶ added in v0.164.0
GitAutoMaintenanceOffEnv returns base with GIT_CONFIG_COUNT/KEY_N/VALUE_N entries appended that disable gc.auto, maintenance.auto and receive.autogc for any git subprocess started with the returned environment. If base already carries a GIT_CONFIG_COUNT sequence (for example a caller's own repo-identity overrides), the three keys are appended after the existing ones and GIT_CONFIG_COUNT is raised to match, rather than overwriting or colliding with them.
This only reaches a git subprocess started directly with this environment. It does not reach a server-side `receive-pack` a same-host `git push` spawns as its own child process: git strips every GIT_CONFIG_* variable from the environment it hands to that child (#711's review traced this directly). A bare remote a test pushes to must also be configured with ConfigureGitAutoMaintenanceOff on the repository itself.
func GitAutoMaintenanceOffProcess ¶ added in v0.164.2
func GitAutoMaintenanceOffProcess()
GitAutoMaintenanceOffProcess performs SetGitAutoMaintenanceOff's work for a caller with no *testing.T -- a package's TestMain -- so that every git subprocess the test binary starts inherits gc.auto, maintenance.auto and receive.autogc disabled. That includes git started by the production code under test (a pull, fetch or commit against a fixture clone, or a clone or temporary worktree production creates itself), which no per-command GitAutoMaintenanceOffEnv reaches and no ConfigureGitAutoMaintenanceOff call can name in advance. Like IsolateProcess this is not restored: TestMain's process exits once m.Run() returns. A bare remote pushed to over a local transport still needs ConfigureGitAutoMaintenanceOff (see its doc comment).
func Isolate ¶
Isolate truly unsets every WB_AGENT_* variable -- the key itself is removed from the environment, not merely emptied -- and sets GOWORK=off for the current test's environment. It restores every value it changed, via t.Cleanup, when t (or the subtest it was called from) completes.
t.Setenv("WB_AGENT_X", "") is not enough: it leaves the key present with an empty value, and envguard.Inspect (like most agent-var detection) keys off presence in os.Environ(), not value, so an emptied variable still reads as set. Isolate calls os.Unsetenv directly instead.
A test that intentionally exercises agent-mode behavior calls Isolate first and then sets its own WB_AGENT_* value with t.Setenv, so the intentional value applies last and is the one the code under test observes.
Like t.Setenv, Isolate makes the calling test (and its subtests) unsafe to run in parallel with sibling tests that also touch the process environment: it mutates shared process state and only restores it on this test's own Cleanup.
func IsolateProcess ¶
func IsolateProcess()
IsolateProcess performs the same isolation as Isolate for a caller with no *testing.T -- typically a package's TestMain, which isolates its whole test binary process before any test runs. Unlike Isolate this is not restored: TestMain's process exits once m.Run() returns, so there is nothing to restore it for.
func SetGitAutoMaintenanceOff ¶ added in v0.164.0
SetGitAutoMaintenanceOff sets GIT_CONFIG_COUNT/KEY_N/VALUE_N for the current test's process environment (via t.Setenv, restored on Cleanup) so that every git subprocess this test starts directly -- and every subprocess of those, such as a locally spawned `git-upload-pack` -- has gc.auto, maintenance.auto and receive.autogc disabled. It extends any GIT_CONFIG_COUNT sequence already present in the process environment rather than overwriting it.
Like GitAutoMaintenanceOffEnv, this does not reach a server-side `receive-pack` a same-host `git push` spawns for a bare remote (see its doc comment); call ConfigureGitAutoMaintenanceOff on that repository directly as well.
Types ¶
This section is empty.