Documentation
¶
Overview ¶
Package testenv gives a test binary a home of its own.
ocfp finds its config, state, data, keys, and logs from HOME, the XDG base directory variables, and OCFP_HOME. A package whose tests reach any of those calls Main or Run from its TestMain. Before any test runs, they point HOME and the XDG variables at one temporary tree and clear OCFP_HOME, so no test can find the developer's real directories through them, and the tests resolve ocfp's directories through the XDG layout that a user without OCFP_HOME gets. A package whose tests need the single-directory layout calls UseOcfpHome. Main and Run also mark the process as a test process for the write guard, which panics if a write still heads for a real directory.
Moving HOME would move the Go build and module caches as well, for any test that runs the go command, so the helper pins them where they were.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GuardOnly ¶
GuardOnly makes the write guard refuse path, and no other directory the test names, until the test ends. A test uses it to prove that the check at one write point fires. The config resolvers check the directories they return, so a test that guarded a whole directory would hit the resolver's check before the one it means to test. Guarding the exact file the write point names leaves the resolvers alone.
It sets environment variables, so the test can't run in parallel.
func Isolate ¶
Isolate points HOME and the XDG variables at directories it makes inside tree, clears OCFP_HOME, and marks the process as a test process. Before it moves HOME it pins the go command's caches to where goSettings says they are, and it records the ocfp directories that the variables named, so the write guard keeps guarding them.
func OcfpHome ¶
func OcfpHome() string
OcfpHome returns the directory OCFP_HOME names, for a test that builds a path inside it. It panics when OCFP_HOME is empty or not absolute, since a path built from either would sit in the package directory, inside the source tree, where the write guard doesn't look.
func PanicMessage ¶
func PanicMessage(fn func()) (message string)
PanicMessage runs fn and returns what it panicked with, or "" when it returned.
func Run ¶
Run runs run in a temporary home, removes the home, and returns run's code. It returns 1 without running anything when it can't set the home up, after saying why on stderr.
A panic in a test goroutine ends the test binary without unwinding Run, so the tree stays in the temporary directory. The write guard panics when it catches a write, so a real violation leaves its ocfp-test-home-* directory behind. Nothing reads it later, and it can be deleted.
func UseOcfpHome ¶
func UseOcfpHome() string
UseOcfpHome points OCFP_HOME at a directory in the temporary tree and returns it, for a package whose tests need ocfp's single-directory layout. A TestMain calls it from inside Main or Run, before any test starts, since tests that run in parallel can't set the variable. It panics when the process isn't isolated, because the directory it would name then belongs to the developer.