hometest

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package hometest isolates a test binary from the developer's real home directory.

cloop keeps per-user state outside the working tree: the multi-project registry at $HOME/.cloop/projects.json, the global budget ledger and cost journal under $HOME/.config/cloop, profiles, plugins and cached provider credentials. None of that is addressed by a parameter — it is resolved from the ambient environment at the point of use, so a test that exercises the code path writes to the machine it runs on and leaves it there.

That is not a tidiness concern, it is a correctness one, and it fails in the worst possible direction. A single dashboard test accumulated 99 entries into one developer's real projects.json, one per run. Past ~100, project index 99 began resolving to a long-deleted /tmp directory, and three unrelated authorization tests started failing — in isolation, on every run, for that developer only. Ephemeral CI runners always start from an empty HOME, so the pipeline was green by construction while the developer was red, and the failures presented as authorization assertions, which invites weakening the assertion rather than finding the leak.

Isolate makes that class of bug impossible rather than merely unlikely: the redirection happens once in TestMain, before any test runs, so it protects tests whose authors never considered it — including tests not yet written.

Index

Constants

View Source
const EnvRoot = "CLOOP_HOME"

EnvRoot must match multiui.EnvRoot. It is duplicated as a literal rather than imported because this package is imported by multiui's own tests, and an import back would be a cycle in that test binary. TestEnvRootMatchesHometest in pkg/multiui pins the two together.

Variables

This section is empty.

Functions

func Guard

func Guard(tb testing.TB)

Guard fails the calling test if per-user state still resolves outside a temporary directory. Use it in a test that asserts the isolation itself, or at the top of one that is about to write to the registry; TestMain callers get the same check for free via Isolate.

func Isolate

func Isolate(m *testing.M) int

Isolate points every per-user state path at a fresh temporary directory for the lifetime of the whole test binary, runs m, and removes the directory. It returns the exit code, so TestMain is:

func TestMain(m *testing.M) { os.Exit(hometest.Isolate(m)) }

Individual tests may still narrow further with t.Setenv("HOME", t.TempDir()) — that keeps working and is worth doing where a test needs a home of its own. The point of doing it here as well is that it does not depend on the author of the next test remembering to.

Types

This section is empty.

Jump to

Keyboard shortcuts

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