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 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 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 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 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.
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.