testutil

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package testutil provides utilities for testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRemote

func AddRemote(t *testing.T, repoDir, name, url string)

AddRemote adds a remote to the repository.

func BackgroundContext

func BackgroundContext() context.Context

BackgroundContext returns context.Background() for use in tests where cleanup isn't needed.

func CancelableContext

func CancelableContext(t *testing.T) (context.Context, context.CancelFunc)

CancelableContext returns a context and cancel function. The context is automatically canceled when the test ends if not canceled earlier.

func CommitFile

func CommitFile(t *testing.T, repoDir, path, content, message string)

CommitFile creates or updates a file and commits it.

func CopyFixture

func CopyFixture(t *testing.T, fixturePath string) string

CopyFixture copies a fixture file to a temporary location. Returns the path to the copy.

func CreateBranch

func CreateBranch(t *testing.T, repoDir, branch string)

CreateBranch creates a new branch in the test repo.

func GetCurrentBranch

func GetCurrentBranch(t *testing.T, repoDir string) string

GetCurrentBranch returns the current branch name.

func GetHeadSHA

func GetHeadSHA(t *testing.T, repoDir string) string

GetHeadSHA returns the current HEAD SHA.

func GetShortSHA

func GetShortSHA(t *testing.T, repoDir string) string

GetShortSHA returns the short SHA for HEAD.

func LoadFixture

func LoadFixture(t *testing.T, path string) []byte

LoadFixture loads a fixture file from the testdata directory. The path is relative to the testdata directory.

func LoadFixtureString

func LoadFixtureString(t *testing.T, path string) string

LoadFixtureString loads a fixture file as a string.

func LoadJSONFixture

func LoadJSONFixture[T any](t *testing.T, path string) T

LoadJSONFixture loads a fixture file and unmarshals it as JSON.

func MergeBranch

func MergeBranch(t *testing.T, repoDir, branch string)

MergeBranch merges a branch into the current branch.

func MustLoadFixture

func MustLoadFixture(path string) []byte

MustLoadFixture loads a fixture file, panicking on error. Use this for test setup outside of test functions.

func SetupTestRepo

func SetupTestRepo(t *testing.T) string

SetupTestRepo creates a temporary git repository for testing. Returns the path to the repository. The repository is automatically cleaned up when the test ends.

func SetupTestRepoWithFiles

func SetupTestRepoWithFiles(t *testing.T, files map[string]string) string

SetupTestRepoWithFiles creates a test repo with specified files.

func Stash

func Stash(t *testing.T, repoDir string)

Stash creates a stash entry.

func StashPop

func StashPop(t *testing.T, repoDir string)

StashPop pops the latest stash entry.

func SwitchBranch

func SwitchBranch(t *testing.T, repoDir, branch string)

SwitchBranch switches to an existing branch.

func TODO

func TODO() context.Context

TODO returns context.TODO() - useful for tests that need an explicit TODO context.

func Tag

func Tag(t *testing.T, repoDir, tag string)

Tag creates a tag at HEAD.

func TempDir

func TempDir(t *testing.T) string

TempDir creates a temporary directory for the test. It returns the directory path and is automatically cleaned up when the test ends.

func TempFile

func TempFile(t *testing.T, name string, content []byte) string

TempFile creates a temporary file with the given content. Returns the file path. File is automatically cleaned up when the test ends.

func TempFileString

func TempFileString(t *testing.T, name, content string) string

TempFileString creates a temporary file with string content.

func TestContext

func TestContext(t *testing.T) context.Context

TestContext returns a context that is canceled when the test ends. This ensures any goroutines started during the test are properly cleaned up.

func TestContextWithDeadline

func TestContextWithDeadline(t *testing.T, deadline time.Time) context.Context

TestContextWithDeadline returns a context with a deadline. The context is also canceled when the test ends.

func TestContextWithTimeout

func TestContextWithTimeout(t *testing.T, timeout time.Duration) context.Context

TestContextWithTimeout returns a context with a timeout. The context is also canceled when the test ends.

func TestNameFromContext

func TestNameFromContext(ctx context.Context) string

TestNameFromContext retrieves the test name from context.

func WithTestName

func WithTestName(ctx context.Context, t *testing.T) context.Context

WithTestName adds the test name to the context.

func WithValue

func WithValue(ctx context.Context, key, value any) context.Context

WithValue returns a context with the given key-value pair. This is a convenience wrapper around context.WithValue.

func WriteFixture

func WriteFixture(t *testing.T, path string, data []byte)

WriteFixture writes data to a fixture file. Useful for generating test fixtures from real API responses.

func WriteJSONFixture

func WriteJSONFixture(t *testing.T, path string, v any)

WriteJSONFixture marshals data to JSON and writes it to a fixture file.

Types

This section is empty.

Jump to

Keyboard shortcuts

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