testenv

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package testenv checks host capabilities a test needs, so a test runs wherever the host provides them and says why it skipped where the host does not. Only test code imports this package.

Index

Constants

View Source
const EnvironHelper = "PIG_TEST_ENVIRON_HELPER"

EnvironHelper is the environment variable that makes a test binary whose TestMain calls ReportEnvironIfRequested report its environment.

Variables

This section is empty.

Functions

func Bash

func Bash(t testing.TB) string

Bash returns the bash that runs repository scripts, and skips the test when the host has none. On Windows it is Git for Windows' bash: bash.exe on PATH is usually the WSL launcher, which runs the script inside a Linux distribution without the host's toolchains or files.

func BashExecutionStringProbe

func BashExecutionStringProbe(t testing.TB) (probe, stderr string)

BashExecutionStringProbe writes a BASH_ENV script for a test that compares the -c command bash receives from Pi and from PiG. The script sends bash's stderr to the returned file, then prints BASH_EXECUTION_STRING, the -c argument as bash received it, as "<%s>\n". Pi and PiG merge a child's stdout and stderr pipes into one stream whose interleaving differs from run to run; with stderr in the file, that stream holds stdout in the order bash wrote it, and BashStderr reads stderr after each command.

func BashStderr

func BashStderr(t testing.TB, stderr string) string

BashStderr returns what the last bash that sourced a BashExecutionStringProbe script wrote to stderr.

func DeletedWorkingDirectory

func DeletedWorkingDirectory(t *testing.T)

DeletedWorkingDirectory makes the process working directory unreadable, as libuv reports for a removed directory: os.Getwd fails with a not-exist error and Node's process.cwd() throws ENOENT. It restores the previous directory when the test ends. The test must not run in parallel.

func GrantOthersRead

func GrantOthersRead(t testing.TB, path string)

GrantOthersRead lets principals other than the owner read the file at path (mode 0644), so an owner-only check must refuse it.

func InheritOthersRead

func InheritOthersRead(testing.TB, string)

InheritOthersRead is a no-op on POSIX: a new file's mode is set when it is created, so the directory cannot grant access to it beforehand.

func ReadOnlyDir

func ReadOnlyDir(t testing.TB, dir string)

ReadOnlyDir lets the current user list and traverse dir but not add or remove its entries (mode 0555) until the test ends. The test must start with RunUnprivileged, because root ignores permission bits.

func ReportEnvironIfRequested

func ReportEnvironIfRequested()

ReportEnvironIfRequested writes os.Environ, in the order of the process's environment block, to stdout as a JSON array and exits when EnvironHelper is "1". A TestMain calls it first, so the test binary can stand in for a spawned program.

func ReportStartupIfRequested

func ReportStartupIfRequested()

ReportStartupIfRequested does nothing outside Windows, where a process has no STARTUPINFO to report.

func RequireDirectoryLink(t testing.TB, oldname, newname string)

RequireDirectoryLink creates newname as a required link to the directory oldname. On Windows it creates a privilege-free drive-target junction, which Node and this module report as a symbolic link; a volume-GUID target creates a volume mount point instead. Elsewhere it creates a symbolic link. Any failure fails the test.

func RequireLiveEnv

func RequireLiveEnv(t testing.TB, names ...string) string

RequireLiveEnv skips a live test when any named variable is unset or empty. It reports only missing names, never values, and returns the first value when every requirement is present. With no names it returns an empty string.

func RequireSymlink(t testing.TB, oldname, newname string)

RequireSymlink creates a required fixture link. Missing privileges fail the test instead of removing its parity obligation.

func RunUnprivileged

func RunUnprivileged(t *testing.T) bool

RunUnprivileged makes the calling test run without root's permission bypass. When the process is root, it reruns only the calling test in a child process as the unprivileged "nobody" user, fails with the child's output unless the child passed, and returns true; the caller then returns without repeating the test. Otherwise it returns false and the test continues in this process. Call it before any other setup. The rerun has a fresh HOME and TMPDIR and cannot read package-relative files.

func ScriptCommand

func ScriptCommand(t testing.TB, path string, args ...string) *exec.Cmd

ScriptCommand returns a command that runs the repository script at path with args. A POSIX host runs the script directly. Windows cannot run a script by its #! line, so there the command names the interpreter that line selects: bash or sh through Bash or Sh, and python3 or node as found on PATH. A script with any other interpreter skips the test on Windows.

func Sh

func Sh(t testing.TB) string

Sh returns the POSIX sh that runs repository scripts, found as Bash is.

func ShebangInterpreter

func ShebangInterpreter(t testing.TB, path string) string

ShebangInterpreter is the program a script's #! line runs, with /usr/bin/env and the directory removed: "#!/usr/bin/env python3" is python3.

func Symlink(t testing.TB, oldname, newname string)

Symlink creates newname as a symbolic link to oldname. When the host does not let this process create symbolic links (Windows without Developer Mode or elevation), the test is skipped; any other error fails it.

func VerbatimArgs

func VerbatimArgs(*exec.Cmd)

VerbatimArgs is a no-op: a POSIX exec passes argv without a command line.

Types

This section is empty.

Jump to

Keyboard shortcuts

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