Help us shape the future of Go! Take the 2026 Go Developer Survey

testsupport

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package testsupport 提供测试共用的环境隔离与环境前提检查辅助函数。 只应被 _test.go 文件引用,不要在生产代码里 import。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FreeLocalPort added in v1.7.0

func FreeLocalPort(t *testing.T, minPort, maxPort int) int

FreeLocalPort 查找指定范围内的空闲端口;调用方应立即启动监听,端口未被预留。

func Isolate

func Isolate(t *testing.T) string

Isolate 把平台数据目录隔离到 t 的临时目录,返回该目录路径。

它同时重置 DataDir 缓存并在测试结束时再次重置:DataDir 在测试二进制下本就不做 缓存,这两次重置是为了让本函数不依赖 DataDir 的缓存实现细节。

需要额外隔离项(例如隔离 HOME 以切断用户全局 git 配置)的测试,在本函数之后 自行调用 t.Setenv 追加。

func RequireEnv

func RequireEnv(t *testing.T, name string) string

RequireEnv 返回环境变量 name 的值;未设置时跳过当前测试,返回值为空串。

func RequireEnvOrExit

func RequireEnvOrExit(name, hint string) string

RequireEnvOrExit 断言进程级必需的环境变量已设置,未设置时向 stderr 打印 FATAL 并 以状态码 1 退出,返回变量值。

用于 TestMain 这类拿不到 *testing.T、无法 t.Skip 的场合:e2e 必须在跑任何用例前 就拒绝未标记、未隔离的运行环境,否则会写到真实数据目录。

func RequireGit

func RequireGit(t *testing.T) string

RequireGit 返回系统 git 的可执行文件路径;系统无 git 时跳过当前测试。

调用方通常紧接着把返回值注入 GOINK_GIT_BIN:测试模式下 ResolveGit 默认只认 DataDir 下的 bundled git,而测试代码造不出 bundled 布局(Windows 上尤其如此)。

Types

This section is empty.

Jump to

Keyboard shortcuts

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