testutil

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package testutil provides shared test utilities for roborev tests.

Index

Constants

View Source
const (
	GitUserName  = "Test"
	GitUserEmail = "test@test.com"
)

Variables

This section is empty.

Functions

func AssertStatusCode

func AssertStatusCode(t *testing.T, w *httptest.ResponseRecorder, expected int)

AssertStatusCode checks that the response has the expected HTTP status code. On failure, it reports the response body for debugging.

func CreateCompletedReview added in v0.21.0

func CreateCompletedReview(t *testing.T, db *storage.DB, repoID int64, sha, agent, reviewText string) *storage.ReviewJob

CreateCompletedReview creates a commit (if needed) and a completed review job. Returns the created job. NOTE: Uses ClaimJob which claims the next available job globally. This is safe when each test uses an isolated DB (via OpenTestDB), but callers must not enqueue multiple jobs before calling this helper.

func CreateReviewWithComments added in v0.21.0

func CreateReviewWithComments(t *testing.T, db *storage.DB, repoID int64, sha, reviewText string, comments []ReviewComment) *storage.ReviewJob

CreateReviewWithComments creates a completed review and adds comments to it. Comments are added in slice order to ensure deterministic behavior.

func CreateTestJobWithSHA

func CreateTestJobWithSHA(t *testing.T, db *storage.DB, repo *storage.Repo, sha, agent string) *storage.ReviewJob

CreateTestJobWithSHA creates a single test job with a specific SHA.

func CreateTestJobs

func CreateTestJobs(t *testing.T, db *storage.DB, repo *storage.Repo, count int, agent string) []*storage.ReviewJob

CreateTestJobs creates the specified number of test jobs in a repository. Each job is created with a unique SHA (sha0, sha1, etc.) and the specified agent. Returns the created jobs in order.

func CreateTestRepo

func CreateTestRepo(t *testing.T, db *storage.DB) *storage.Repo

CreateTestRepo creates a test repository in the database. Uses the test's temp directory as the repo path.

func DecodeJSON added in v0.21.0

func DecodeJSON(t *testing.T, w *httptest.ResponseRecorder, v any)

DecodeJSON unmarshals the response body from an httptest.ResponseRecorder into v.

func GetHeadSHA added in v0.21.0

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

GetHeadSHA returns the HEAD commit SHA for the git repo at dir.

func InitTestGitRepo added in v0.21.0

func InitTestGitRepo(t *testing.T, dir string)

InitTestGitRepo initializes a git repository with a commit in the given directory. Creates the directory if it doesn't exist, runs git init, configures user, creates a test file, and makes an initial commit.

func MakeJSONRequest added in v0.21.0

func MakeJSONRequest(t *testing.T, method, path string, body any) *http.Request

MakeJSONRequest creates an HTTP request with the given body marshaled as JSON.

func MapKeys added in v0.37.0

func MapKeys[K comparable, V any](m map[K]V) []K

MapKeys returns the keys of the map m.

func MockBinaryInPath added in v0.21.0

func MockBinaryInPath(t *testing.T, binName, scriptContent string) func()

MockBinaryInPath creates a fake executable in PATH and returns a cleanup function.

func MockExecutable added in v0.33.1

func MockExecutable(t *testing.T, binName string, exitCode int) func()

MockExecutable creates a fake executable in PATH that exits with the given code. Returns a cleanup function.

func MockExecutableIsolated added in v0.33.1

func MockExecutableIsolated(t *testing.T, binName string, exitCode int) func()

MockExecutableIsolated creates a fake executable in a new directory and sets PATH to ONLY that directory. Returns a cleanup function.

func OpenTestDB

func OpenTestDB(t *testing.T) *storage.DB

OpenTestDB creates a test database in a temporary directory. The database is automatically closed when the test completes.

func OpenTestDBWithDir

func OpenTestDBWithDir(t *testing.T) (*storage.DB, string)

OpenTestDBWithDir creates a test database and returns both the DB and the temporary directory path. Useful when tests need to create repos or other files in the same directory. The database is automatically closed when the test completes.

func Ptr added in v0.33.1

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

Ptr returns a pointer to the given value.

func ReceiveWithTimeout added in v0.21.0

func ReceiveWithTimeout[T any](t *testing.T, ch <-chan T, timeout time.Duration) T

ReceiveWithTimeout reads a single value from a channel, failing the test if nothing is received within the given timeout.

func WaitForJobStatus added in v0.21.0

func WaitForJobStatus(t *testing.T, db *storage.DB, jobID int64, timeout time.Duration, statuses ...storage.JobStatus) *storage.ReviewJob

WaitForJobStatus polls until the job reaches one of the expected statuses or the timeout expires. Returns the final job state.

Types

type ReviewComment added in v0.21.0

type ReviewComment struct {
	User string
	Text string
}

ReviewComment represents a comment on a review with a defined order.

type TestRepo added in v0.21.0

type TestRepo struct {
	Root     string
	GitDir   string
	HooksDir string
	HookPath string
	// contains filtered or unexported fields
}

TestRepo encapsulates a temporary git repository for tests.

func InitTestRepo added in v0.37.0

func InitTestRepo(t *testing.T) *TestRepo

InitTestRepo creates a standard test repository with an initial commit on the main branch.

func NewGitRepo added in v0.37.0

func NewGitRepo(t *testing.T) *TestRepo

func NewTestRepo added in v0.21.0

func NewTestRepo(t *testing.T) *TestRepo

NewTestRepo creates a temporary git repository.

func NewTestRepoWithCommit added in v0.21.0

func NewTestRepoWithCommit(t *testing.T) *TestRepo

NewTestRepoWithCommit creates a temporary git repository with a file and initial commit, suitable for tests that need a valid git history.

func (*TestRepo) Chdir added in v0.21.0

func (r *TestRepo) Chdir() func()

Chdir changes the working directory to the repo root and returns a restore function. The caller should defer the returned function.

func (*TestRepo) Checkout added in v0.33.1

func (r *TestRepo) Checkout(args ...string)

Checkout runs git checkout.

func (*TestRepo) CommitFile added in v0.33.1

func (r *TestRepo) CommitFile(filename, content, msg string) string

CommitFile writes a file, stages it, commits, and returns the new HEAD SHA.

func (*TestRepo) Config added in v0.33.1

func (r *TestRepo) Config(key, value string)

Config sets a git config value.

func (*TestRepo) GetHookPath added in v0.37.0

func (r *TestRepo) GetHookPath(name string) string

GetHookPath returns the path to a specific hook in the test repository.

func (*TestRepo) HeadSHA added in v0.49.0

func (r *TestRepo) HeadSHA() string

func (*TestRepo) Path added in v0.49.0

func (r *TestRepo) Path() string

func (*TestRepo) RemoveHooksDir added in v0.21.0

func (r *TestRepo) RemoveHooksDir()

RemoveHooksDir removes the .git/hooks directory.

func (*TestRepo) RevParse added in v0.33.1

func (r *TestRepo) RevParse(args ...string) string

RevParse runs git rev-parse and returns the trimmed output.

func (*TestRepo) Run added in v0.49.0

func (r *TestRepo) Run(args ...string) string

func (*TestRepo) RunGit added in v0.33.1

func (r *TestRepo) RunGit(args ...string)

RunGit runs a git command in the repo directory.

func (*TestRepo) SymbolicRef added in v0.33.1

func (r *TestRepo) SymbolicRef(ref, target string)

SymbolicRef runs git symbolic-ref.

func (*TestRepo) WriteFile added in v0.49.0

func (r *TestRepo) WriteFile(name, content string)

WriteFile writes a file relative to the repo root, creating parent directories as needed.

func (*TestRepo) WriteHook added in v0.21.0

func (r *TestRepo) WriteHook(content string)

WriteHook writes a post-commit hook with the given content.

func (*TestRepo) WriteNamedHook added in v0.33.1

func (r *TestRepo) WriteNamedHook(name, content string)

WriteNamedHook writes a hook with the given name and content.

Jump to

Keyboard shortcuts

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