Documentation
¶
Overview ¶
Package testutil provides shared test utilities for roborev tests.
Index ¶
- Constants
- func AssertStatusCode(t *testing.T, w *httptest.ResponseRecorder, expected int)
- func CreateCompletedReview(t *testing.T, db *storage.DB, repoID int64, sha, agent, reviewText string) *storage.ReviewJob
- func CreateReviewWithComments(t *testing.T, db *storage.DB, repoID int64, sha, reviewText string, ...) *storage.ReviewJob
- func CreateTestJobWithSHA(t *testing.T, db *storage.DB, repo *storage.Repo, sha, agent string) *storage.ReviewJob
- func CreateTestJobs(t *testing.T, db *storage.DB, repo *storage.Repo, count int, agent string) []*storage.ReviewJob
- func CreateTestRepo(t *testing.T, db *storage.DB) *storage.Repo
- func DecodeJSON(t *testing.T, w *httptest.ResponseRecorder, v any)
- func GetHeadSHA(t *testing.T, dir string) string
- func InitTestGitRepo(t *testing.T, dir string)
- func MakeJSONRequest(t *testing.T, method, path string, body any) *http.Request
- func MapKeys[K comparable, V any](m map[K]V) []K
- func MockBinaryInPath(t *testing.T, binName, scriptContent string) func()
- func MockExecutable(t *testing.T, binName string, exitCode int) func()
- func MockExecutableIsolated(t *testing.T, binName string, exitCode int) func()
- func OpenTestDB(t *testing.T) *storage.DB
- func OpenTestDBWithDir(t *testing.T) (*storage.DB, string)
- func Ptr[T any](v T) *T
- func ReceiveWithTimeout[T any](t *testing.T, ch <-chan T, timeout time.Duration) T
- func WaitForJobStatus(t *testing.T, db *storage.DB, jobID int64, timeout time.Duration, ...) *storage.ReviewJob
- type ReviewComment
- type TestRepo
- func (r *TestRepo) Chdir() func()
- func (r *TestRepo) Checkout(args ...string)
- func (r *TestRepo) CommitFile(filename, content, msg string) string
- func (r *TestRepo) Config(key, value string)
- func (r *TestRepo) GetHookPath(name string) string
- func (r *TestRepo) HeadSHA() string
- func (r *TestRepo) Path() string
- func (r *TestRepo) RemoveHooksDir()
- func (r *TestRepo) RevParse(args ...string) string
- func (r *TestRepo) Run(args ...string) string
- func (r *TestRepo) RunGit(args ...string)
- func (r *TestRepo) SymbolicRef(ref, target string)
- func (r *TestRepo) WriteFile(name, content string)
- func (r *TestRepo) WriteHook(content string)
- func (r *TestRepo) WriteNamedHook(name, content string)
Constants ¶
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 ¶
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
GetHeadSHA returns the HEAD commit SHA for the git repo at dir.
func InitTestGitRepo ¶ added in v0.21.0
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
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
MockBinaryInPath creates a fake executable in PATH and returns a cleanup function.
func MockExecutable ¶ added in v0.33.1
MockExecutable creates a fake executable in PATH that exits with the given code. Returns a cleanup function.
func MockExecutableIsolated ¶ added in v0.33.1
MockExecutableIsolated creates a fake executable in a new directory and sets PATH to ONLY that directory. Returns a cleanup function.
func OpenTestDB ¶
OpenTestDB creates a test database in a temporary directory. The database is automatically closed when the test completes.
func OpenTestDBWithDir ¶
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 ReceiveWithTimeout ¶ added in v0.21.0
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
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
InitTestRepo creates a standard test repository with an initial commit on the main branch.
func NewGitRepo ¶ added in v0.37.0
func NewTestRepo ¶ added in v0.21.0
NewTestRepo creates a temporary git repository.
func NewTestRepoWithCommit ¶ added in v0.21.0
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) CommitFile ¶ added in v0.33.1
CommitFile writes a file, stages it, commits, and returns the new HEAD SHA.
func (*TestRepo) GetHookPath ¶ added in v0.37.0
GetHookPath returns the path to a specific hook in the test repository.
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
RevParse runs git rev-parse and returns the trimmed output.
func (*TestRepo) SymbolicRef ¶ added in v0.33.1
SymbolicRef runs git symbolic-ref.
func (*TestRepo) WriteFile ¶ added in v0.49.0
WriteFile writes a file relative to the repo root, creating parent directories as needed.
func (*TestRepo) WriteHook ¶ added in v0.21.0
WriteHook writes a post-commit hook with the given content.
func (*TestRepo) WriteNamedHook ¶ added in v0.33.1
WriteNamedHook writes a hook with the given name and content.