testenv

package
v0.2.4 Latest Latest
Warning

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

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

Documentation

Overview

Package testenv holds small helpers that keep tests portable across operating systems. It is only imported from _test.go files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FakeBinary

func FakeBinary(t *testing.T, dir, name string) string

FakeBinary writes a do-nothing executable named name into dir and returns its path.

On Windows the file gets a .bat suffix, because exec.LookPath there resolves only the suffixes listed in %PATHEXT% -- an extensionless file is invisible to it, however executable its mode bits claim to be.

func RequireShell

func RequireShell(t *testing.T) string

RequireShell skips the test unless a POSIX shell is on PATH.

The GitHub Windows runners ship Git for Windows, so "bash" and "sh" resolve there; a bare Windows box has neither. Tests that drive shell commands call this instead of hardcoding /bin/sh, which never exists on Windows.

func SetHome

func SetHome(t *testing.T, dir string)

SetHome points the user's home directory at dir for the duration of the test.

os.UserHomeDir reads $HOME on Unix but %USERPROFILE% on Windows, so a test that sets only HOME still resolves to the real profile directory there -- which makes the test read and write the developer's (or CI runner's) actual ~/.pi-go instead of its sandbox.

func SetUnwritableHome

func SetUnwritableHome(t *testing.T) string

SetUnwritableHome points the home directory at a regular file for the duration of the test, so anything the code under test tries to create below ~ fails. It returns that path.

Tests used to spell this as HOME=/nonexistent/..., which does not travel: on Windows a leading slash is drive-relative, so os.MkdirAll cheerfully creates the directory and the expected failure never happens.

func UnsetHome

func UnsetHome(t *testing.T)

UnsetHome removes the home-directory environment variables for the duration of the test, so os.UserHomeDir has nothing to resolve. Unsetting HOME alone leaves %USERPROFILE% in place, which is the only variable Windows consults.

Types

This section is empty.

Jump to

Keyboard shortcuts

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