testutils

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2025 License: MPL-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package testutils provides utilities for capturing output, managing test files, 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.

Jump to

Keyboard shortcuts

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