testenv

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 9 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 keeps test binaries away from the developer's environment.

Every test package runs its tests through Main:

func TestMain(m *testing.M) {
	testenv.Main(m)
}

Main points the home directory, HOME and on Windows USERPROFILE, at a new empty directory, so tests never read or write the developer's ~/.ahasend, and routes every request to a non-loopback host to a closed local port, so no test can reach the AhaSend API. Requests to loopback hosts are never proxied, so httptest servers keep working. Main also fails every DNS lookup, so connections the proxy does not cover, such as the SMTP connection of smtp send, reach only IP addresses and the hosts in /etc/hosts. A TestMain that cleans up after the tests calls Run instead.

Index

Constants

View Source
const BlockedProxy = "http://127.0.0.1:1"

BlockedProxy is a local port nothing listens on. Every request to a non-loopback host goes through it and fails at once.

Variables

View Source
var ErrDNSBlocked = errors.New("testenv: DNS lookups are blocked; tests may only connect to loopback servers")

ErrDNSBlocked is the error of every DNS lookup in a test binary.

Functions

func Main

func Main(m *testing.M)

Main runs the tests of a package in an isolated environment and exits with their result.

func Run

func Run(m *testing.M) int

Run is Main without the exit: it returns the exit code of the tests, for a TestMain that cleans up after them.

func SetHome

func SetHome(t testing.TB, dir string)

SetHome points the home directory at dir for the rest of the test: HOME, and USERPROFILE, which Windows reads it from.

Types

This section is empty.

Jump to

Keyboard shortcuts

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