testenv

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const VerbChildEnv = "OCFP_JOB_VERB_CHILD"

VerbChildEnv marks a process that job exec started to run a verb. It is jobexec.VerbChildEnv, named here apart since this package cannot import that one, and a test there checks that the two agree.

Variables

This section is empty.

Functions

func GuardOnly

func GuardOnly(tb testing.TB, path string)

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

func Isolate(tree string, goSettings func() (map[string]string, error)) error

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 Main

func Main(m Runner)

Main runs m's tests in a temporary home and exits with their code.

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

func Run(run func() int) int

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, or when the process holds VerbChildEnv, after saying why on stderr. A process that holds the mark is a job's verb process whose TestMain did not serve the verb first, and running the tests in it again could start it again, without end.

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.

Types

type Runner

type Runner interface {
	Run() int
}

Runner is what a TestMain receives. *testing.M satisfies it.

Jump to

Keyboard shortcuts

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