Documentation
¶
Overview ¶
Package testutils provides helper functions for testing
Index ¶
- Constants
- func AppendCovEnv(env []string) []string
- func CopyDir(t *testing.T, srcDir, dstDir string) error
- func CopyFile(t *testing.T, src, dst string) error
- func CopySymlink(t *testing.T, src, dst string) error
- func CoverDirEnv() string
- func CoverDirForTests() string
- func CurrentDir() string
- func GetDirContents(t *testing.T, dir string, maxDepth uint) (map[string]string, error)
- func GetDirHashedContents(t *testing.T, dir string, maxDepth uint) (map[string]string, error)
- func GetFakeCmdArgs() (args []string, err error)
- func GetFreePort(t *testing.T, host string, protocol Protocol) int
- func GoldenPath(t *testing.T) string
- func HashString(s string) uint32
- func IsRace() bool
- func IsUnix() bool
- func IsUnixNonRoot() bool
- func IsVerbose() bool
- func LoadWithUpdateFromGolden(t *testing.T, data string, opts ...GoldenOption) string
- func LoadWithUpdateFromGoldenYAML[E any](t *testing.T, got E, opts ...GoldenOption) E
- func MakeReadOnly(t *testing.T, dest string)
- func NormalizeGoldenName(t *testing.T, name string) string
- func PortOpen(t *testing.T, host string, port int) bool
- func ProjectRoot() string
- func SetupFakeCmdArgs(fakeCmdFunc string, args ...string) []string
- func SetupHelperCoverdir() (string, bool)
- func TestFamilyPath(t *testing.T) string
- func UpdateEnabled() bool
- func WaitForPortClosed(t *testing.T, host string, port int, timeout time.Duration)
- type GoldenOption
- type MockHandler
- func (h *MockHandler) AssertLevels(t *testing.T, levels map[slog.Level]uint) bool
- func (h *MockHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *MockHandler) GetLevels() map[slog.Level]uint
- func (h *MockHandler) Handle(ctx context.Context, record slog.Record) error
- func (h *MockHandler) OutputLogs(t *testing.T)
- func (h *MockHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *MockHandler) WithGroup(name string) slog.Handler
- type Protocol
Constants ¶
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 ¶
AppendCovEnv returns the env needed to enable coverage when running a go binary, if coverage is enabled.
func CopySymlink ¶
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 GetDirContents ¶
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 ¶
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 ¶
GetFakeCmdArgs gets the arguments passed into a fake testing command, or errors without the proper environment.
func GetFreePort ¶
GetFreePort returns a free port on the specified host for the given protocol (TCP or UDP).
func GoldenPath ¶
GoldenPath returns the golden path for the provided test.
func HashString ¶
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 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 ¶
MakeReadOnly makes dest read only and restore permission on cleanup.
func NormalizeGoldenName ¶
NormalizeGoldenName returns the name of the golden file with illegal Windows characters replaced or removed.
func ProjectRoot ¶
func ProjectRoot() string
ProjectRoot returns the absolute path to the project root.
func SetupFakeCmdArgs ¶
SetupFakeCmdArgs sets up arguments to run a fake testing command.
func SetupHelperCoverdir ¶
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 ¶
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.
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 ¶
AssertLevels asserts that the logging levels observed match the expected amount.
func (*MockHandler) GetLevels ¶
func (h *MockHandler) GetLevels() map[slog.Level]uint
GetLevels returns the levels of the logged records.
func (*MockHandler) OutputLogs ¶
func (h *MockHandler) OutputLogs(t *testing.T)
OutputLogs outputs the logs collected by the handler in a readable format.