testutils

package
v0.0.0-...-55c58f5 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2025 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package testutils provides helper functions for testing

Index

Constants

View Source
const (
	// UpdateGoldenFilesEnv is the environment variable used to indicate go test that
	// the golden files should be overwritten with the current test results.
	UpdateGoldenFilesEnv = `TESTS_UPDATE_GOLDEN`
)

Variables

This section is empty.

Functions

func AppendCovEnv

func AppendCovEnv(env []string) []string

AppendCovEnv returns the env needed to enable coverage when running a go binary, if coverage is enabled.

func CopyDir

func CopyDir(t *testing.T, srcDir, dstDir string) error

CopyDir copies the contents of a directory to another directory.

func CopyFile

func CopyFile(t *testing.T, src, dst string) error

CopyFile copies a file from source to destination.

func CopySymlink(t *testing.T, src, dst string) error

CopySymlink copies a symlink from source to destination.

func CoverDirEnv

func CoverDirEnv() string

CoverDirEnv returns the cover dir env variable to run a go binary, if coverage is enabled.

func CoverDirForTests

func CoverDirForTests() string

CoverDirForTests parses the test arguments and return the cover profile directory, if coverage is enabled.

func CurrentDir

func CurrentDir() string

CurrentDir returns the current file directory.

func GetDirContents

func GetDirContents(t *testing.T, dir string, maxDepth uint) (map[string]string, error)

GetDirContents returns the contents of a directory as a map of file paths to file contents. The contents are read as strings. The maxDepth parameter limits the depth of the directory tree to read.

func GetDirHashedContents

func GetDirHashedContents(t *testing.T, dir string, maxDepth uint) (map[string]string, error)

GetDirHashedContents is like GetDirContents but hashes the contents of the files.

This is for situations where it is very unlikely that the contents of the files will change, and we don't care about not being able to see the actual diff, and we can identify the contents in other ways, such as by the filename. For those situations, hashing the contents can make the golden files much more readable.

func GetFakeCmdArgs

func GetFakeCmdArgs() (args []string, err error)

GetFakeCmdArgs gets the arguments passed into a fake testing command, or errors without the proper environment.

func GetFreePort

func GetFreePort(t *testing.T, host string, protocol Protocol) int

GetFreePort returns a free port on the specified host for the given protocol (TCP or UDP).

func GoldenPath

func GoldenPath(t *testing.T) string

GoldenPath returns the golden path for the provided test.

func HashString

func HashString(s string) uint32

HashString returns the crc32 checksum of a string, removing any Windows line endings before hashing.

func IsRace

func IsRace() bool

IsRace returns whether the tests are running with thread sanitizer.

func IsUnix

func IsUnix() bool

IsUnix returns true if the current operating system is Unix-like.

func IsUnixNonRoot

func IsUnixNonRoot() bool

IsUnixNonRoot returns true if the current operating system is Unix-like and not running as root.

func IsVerbose

func IsVerbose() bool

IsVerbose returns whether the tests are running in verbose mode.

func LoadWithUpdateFromGolden

func LoadWithUpdateFromGolden(t *testing.T, data string, opts ...GoldenOption) string

LoadWithUpdateFromGolden loads the element from a plaintext golden file. It will update the file if the update flag is used prior to loading it.

func LoadWithUpdateFromGoldenYAML

func LoadWithUpdateFromGoldenYAML[E any](t *testing.T, got E, opts ...GoldenOption) E

LoadWithUpdateFromGoldenYAML load the generic element from a YAML serialized golden file. It will update the file if the update flag is used prior to deserializing it.

func MakeReadOnly

func MakeReadOnly(t *testing.T, dest string)

MakeReadOnly makes dest read only and restore permission on cleanup.

func NormalizeGoldenName

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

NormalizeGoldenName returns the name of the golden file with illegal Windows characters replaced or removed.

func PortOpen

func PortOpen(t *testing.T, host string, port int) bool

PortOpen checks if a port is open on the specified TCP host.

func ProjectRoot

func ProjectRoot() string

ProjectRoot returns the absolute path to the project root.

func SetupFakeCmdArgs

func SetupFakeCmdArgs(fakeCmdFunc string, args ...string) []string

SetupFakeCmdArgs sets up arguments to run a fake testing command.

func SetupHelperCoverdir

func SetupHelperCoverdir() (string, bool)

SetupHelperCoverdir creates a directory and sets GOCOVERDIR to it but only if in a helper and GOCOVERDIR is set. It is the callers job to remove the directory. This function will exit with 1 if it cannot create the directory.

func TestFamilyPath

func TestFamilyPath(t *testing.T) string

TestFamilyPath returns the path of the dir for storing fixtures and other files related to the test.

func UpdateEnabled

func UpdateEnabled() bool

UpdateEnabled returns true if updating the golden files is requested.

func WaitForPortClosed

func WaitForPortClosed(t *testing.T, host string, port int, timeout time.Duration)

WaitForPortClosed waits for a port to be closed on the specified TCP host.

Types

type GoldenOption

type GoldenOption func(*goldenOptions)

GoldenOption is a supported option reference to change the golden files comparison.

func WithGoldenPath

func WithGoldenPath(path string) GoldenOption

WithGoldenPath overrides the default path for golden files used.

type MockHandler

type MockHandler struct {
	IgnoreBelow    slog.Level
	EnabledCalls   []slog.Level
	HandleCalls    []slog.Record
	WithAttrsCalls [][]slog.Attr
	WithGroupCalls []string
	// contains filtered or unexported fields
}

MockHandler tracks calls to logging functions and implements slog.Handler.

func NewMockHandler

func NewMockHandler(ignoreBelow slog.Level) MockHandler

NewMockHandler returns a new MockHandler. levels <= ignoreBelow will not call handle.

func (*MockHandler) AssertLevels

func (h *MockHandler) AssertLevels(t *testing.T, levels map[slog.Level]uint) bool

AssertLevels asserts that the logging levels observed match the expected amount.

func (*MockHandler) Enabled

func (h *MockHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled implements Handler.Enabled.

func (*MockHandler) GetLevels

func (h *MockHandler) GetLevels() map[slog.Level]uint

GetLevels returns the levels of the logged records.

func (*MockHandler) Handle

func (h *MockHandler) Handle(ctx context.Context, record slog.Record) error

Handle implements Handler.Handle.

func (*MockHandler) OutputLogs

func (h *MockHandler) OutputLogs(t *testing.T)

OutputLogs outputs the logs collected by the handler in a readable format.

func (*MockHandler) WithAttrs

func (h *MockHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements Handler.WithAttrs.

func (*MockHandler) WithGroup

func (h *MockHandler) WithGroup(name string) slog.Handler

WithGroup implements Handler.WithGroup.

type Protocol

type Protocol uint

Protocol represents the network protocol type.

const (
	// TCP protocol.
	TCP Protocol = iota
	// UDP protocol.
	UDP
)

Jump to

Keyboard shortcuts

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