testutils

package
v0.13.3 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MPL-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package testutils provides utilities for capturing output, managing test files, logging, and making assertions in tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertContainsAll

func AssertContainsAll(t *testing.T, contents string, elements []string)

AssertContainsAll verifies that the given contents string contains all specified elements.

func AssertContainsNone

func AssertContainsNone(t *testing.T, contents string, elements []string)

AssertContainsNone verifies that the given contents string contains none of the specified elements.

func AssertFileContains

func AssertFileContains(t *testing.T, filePath string, want []string, notWant []string)

AssertFileContains checks if a file contains all strings from want slice and none from notWant slice.

func AssertFileContentsSameAs

func AssertFileContentsSameAs(t *testing.T, wantFilePath string, gotFilePath string)

AssertFileContentsSameAs verifies that two files have identical contents.

func AssertNotBlank

func AssertNotBlank(t *testing.T, value string)

AssertNotBlank asserts that the given string is not blank (i.e., not empty or consisting only of whitespace).

func CaptureStdout

func CaptureStdout(t *testing.T, fn func()) (stdout string)

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

func CreateMockFile(t *testing.T, namePattern string, contents []byte) string

CreateMockFile creates a temporary file with the given name pattern and contents, returning the file path.

func CreateMockServer

func CreateMockServer(t *testing.T, responses map[string]MockHTTPResponse) *httptest.Server

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

func CreateOpenNewTestFile(t *testing.T, namePattern string) *os.File

CreateOpenNewTestFile creates and opens a new temporary test file with the given name pattern. The caller is responsible for closing the file.

func Ptr

func Ptr[T any](value T) *T

Ptr returns a pointer to the given value.

func ReadFile

func ReadFile(t *testing.T, filePath string) []byte

ReadFile reads the entire file at the given path and returns its contents.

func SyncCall

func SyncCall(lock *sync.Mutex, fn func())

SyncCall executes the provided function while holding the specified mutex lock.

func WithArgs

func WithArgs(_ *testing.T, fn func(), args ...string)

WithArgs temporarily replaces os.Args with the provided arguments while executing the given function. This function is synchronized to prevent concurrent modifications.

Types

type MockHTTPResponse

type MockHTTPResponse struct {
	StatusCode int
	Content    []byte
	Delay      time.Duration
}

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.

func (*TestLogger) Message added in v0.5.0

func (tl *TestLogger) Message(ctx context.Context, level slog.Level, msg string, args ...any)

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.

Jump to

Keyboard shortcuts

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