Documentation
¶
Overview ¶
Package testutils provides utilities for capturing output, managing test files, logging, and making assertions in tests.
Index ¶
- func AssertContainsAll(t *testing.T, contents string, elements []string)
- func AssertContainsNone(t *testing.T, contents string, elements []string)
- func AssertFileContains(t *testing.T, filePath string, want []string, notWant []string)
- func AssertFileContentsSameAs(t *testing.T, wantFilePath string, gotFilePath string)
- func AssertNotBlank(t *testing.T, value string)
- func CaptureStderr(t *testing.T, fn func()) (stderr string)
- func CaptureStdout(t *testing.T, fn func()) (stdout string)
- func CreateMockFile(t *testing.T, namePattern string, contents []byte) string
- func CreateMockServer(t *testing.T, responses map[string]MockHTTPResponse) *httptest.Server
- func CreateOpenNewTestFile(t *testing.T, namePattern string) *os.File
- func Ptr[T any](value T) *T
- func ReadFile(t *testing.T, filePath string) []byte
- func SyncCall(lock *sync.Mutex, fn func())
- func WithArgs(_ *testing.T, fn func(), args ...string)
- type DockerFake
- type DockerFakeContainer
- type DockerFakeMount
- type DockerFakeResult
- type MockHTTPResponse
- type TestLogger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertContainsAll ¶
AssertContainsAll verifies that the given contents string contains all specified elements.
func AssertContainsNone ¶
AssertContainsNone verifies that the given contents string contains none of the specified elements.
func AssertFileContains ¶
AssertFileContains checks if a file contains all strings from want slice and none from notWant slice.
func AssertFileContentsSameAs ¶
AssertFileContentsSameAs verifies that two files have identical contents.
func AssertNotBlank ¶
AssertNotBlank asserts that the given string is not blank (i.e., not empty or consisting only of whitespace).
func CaptureStderr ¶ added in v0.22.0
CaptureStderr captures standard error during the execution of the provided function and returns it as a string. This function is synchronized to prevent concurrent stderr capture.
func CaptureStdout ¶
CaptureStdout captures standard output during the execution of the provided function and returns it as a string. This function is synchronized to prevent concurrent stdout capture.
func CreateMockFile ¶
CreateMockFile creates a temporary file with the given name pattern and contents, returning the file path.
func CreateMockServer ¶
CreateMockServer creates a test HTTP server with configurable responses. It accepts a map of paths to response configurations (status code and content). Returns the server which should be closed after use.
func CreateOpenNewTestFile ¶
CreateOpenNewTestFile creates and opens a new temporary test file with the given name pattern. The caller is responsible for closing the file.
Types ¶
type DockerFake ¶
type DockerFake struct {
// contains filtered or unexported fields
}
DockerFake is a minimal Docker Engine API server for tests of code that runs one-shot containers through the Docker client configured from the environment.
func NewDockerFake ¶
func NewDockerFake(t *testing.T, result DockerFakeResult) *DockerFake
NewDockerFake starts a DockerFake and points the Docker client environment of the test at it.
func (*DockerFake) Containers ¶
func (f *DockerFake) Containers() []DockerFakeContainer
Containers returns the containers created so far, in creation order.
type DockerFakeContainer ¶
type DockerFakeContainer struct {
// Cmd is the container command.
Cmd []string
// Env is the container environment in NAME=value form.
Env []string
// NetworkMode is the container network mode.
NetworkMode string
// Mounts maps the container path of every bind mount to the mounted file captured at creation.
Mounts map[string]DockerFakeMount
}
DockerFakeContainer describes a container created through a DockerFake.
type DockerFakeMount ¶
type DockerFakeMount struct {
// Content is the mounted file content at container creation.
Content []byte
// ReadOnly reports whether the mount is read-only.
ReadOnly bool
}
DockerFakeMount is a file bind mounted into a DockerFake container.
type DockerFakeResult ¶
type DockerFakeResult struct {
// Stdout is written to the standard output of every container.
Stdout string
// ExitCode is the exit code of every container.
ExitCode int
// Hang keeps every container running until the waiting request is canceled.
Hang bool
// CreateError, when set, fails every container creation with this Docker API error message.
CreateError string
}
DockerFakeResult configures how a DockerFake runs every container.
type MockHTTPResponse ¶
MockHTTPResponse defines a mock HTTP response for testing.
type TestLogger ¶ added in v0.5.0
type TestLogger struct {
// contains filtered or unexported fields
}
TestLogger is a logger implementation for testing that wraps zerolog and integrates with the Go testing framework. It outputs log messages through the test writer so they appear properly in test output and are captured when tests fail.
func NewTestLogger ¶ added in v0.5.0
func NewTestLogger(t *testing.T) *TestLogger
NewTestLogger creates a new TestLogger that outputs to the test framework. Log messages will be properly associated with the test and displayed in test output.
func (*TestLogger) Error ¶ added in v0.5.0
func (tl *TestLogger) Error(ctx context.Context, level slog.Level, err error, msg string, args ...any)
Error logs an error message at the specified level with optional formatting arguments. If the error implements logging.StructuredError, its fields are appended to the log event.
func (*TestLogger) Message ¶ added in v0.5.0
Message logs a message at the specified level with optional formatting arguments.
func (*TestLogger) WithContext ¶ added in v0.5.0
func (tl *TestLogger) WithContext(context string) logging.Logger
WithContext returns a new logger with additional context. The context string will be prepended to all log messages from the returned logger.