Documentation
¶
Overview ¶
Package testhelpers provides testing utilities for the Stackit CLI, including a scene system, Git repository helpers, and custom assertions.
Package testhelpers provides shared test utilities for CLI packages.
Index ¶
- func BasicSceneSetup(scene *Scene) error
- func ExpectBranches(t *testing.T, repo *GitRepo, expected []string)
- func ExpectBranchesString(t *testing.T, repo *GitRepo, expected string)
- func ExpectCommits(t *testing.T, repo *GitRepo, branch string, expected []string)
- func ExpectCommitsString(t *testing.T, repo *GitRepo, expected string)
- func GetBinaryError() error
- func GetSharedBinaryPath() string
- func InitialCommitSceneSetup(scene *Scene) error
- func Must[T any](val T, err error) T
- func NewMockGitHubClient(t *testing.T, config *MockGitHubServerConfig) (*github.Client, string, string)
- func NewMockGitHubClientInterface(client *github.Client, owner, repo string, config *MockGitHubServerConfig) githubpkg.Client
- func NewMockGitHubServer(t *testing.T, config *MockGitHubServerConfig) *httptest.Server
- func NewSamplePullRequest(data SamplePRData) *github.PullRequest
- func NewTestPrInfo(number int) *engine.PrInfo
- func NewTestPrInfoClosed(number int) *engine.PrInfo
- func NewTestPrInfoDraft(number int) *engine.PrInfo
- func NewTestPrInfoEmpty() *engine.PrInfo
- func NewTestPrInfoFull(number int, title, body, state, base, url string, isDraft bool) *engine.PrInfo
- func NewTestPrInfoMerged(number int, base string) *engine.PrInfo
- func NewTestPrInfoWithState(number int, state string) *engine.PrInfo
- func NewTestPrInfoWithTitle(number int, title string) *engine.PrInfo
- func NormalizeOutput(output string) string
- func SetSharedBinaryPath(path string)
- func TestMain(m *testing.M, cleanup func())
- type GitRepo
- func (r *GitRepo) CheckoutBranch(name string) error
- func (r *GitRepo) CheckoutDetached(rev string) error
- func (r *GitRepo) CreateAndCheckoutBranch(name string) error
- func (r *GitRepo) CreateBareRemote(name string) (string, error)
- func (r *GitRepo) CreateBranch(name string) error
- func (r *GitRepo) CreateChange(textValue string, prefix string, unstaged bool) error
- func (r *GitRepo) CreateChangeAndAmend(textValue string, prefix string) error
- func (r *GitRepo) CreateChangeAndCommit(textValue string, prefix string) error
- func (r *GitRepo) CreatePrecommitHook(contents string) error
- func (r *GitRepo) CurrentBranchName() (string, error)
- func (r *GitRepo) DeleteBranch(name string) error
- func (r *GitRepo) ForcePushBranch(remote, branch string) error
- func (r *GitRepo) GetBranchSHA(branch string) (string, error)
- func (r *GitRepo) GetCommitCount(from, to string) (int, error)
- func (r *GitRepo) GetCurrentSHA() (string, error)
- func (r *GitRepo) GetLocalBranches() ([]string, error)
- func (r *GitRepo) GetRef(refName string) (string, error)
- func (r *GitRepo) GetRevision(rev string) (string, error)
- func (r *GitRepo) HasUnstagedChanges() (bool, error)
- func (r *GitRepo) HasUntrackedFiles() (bool, error)
- func (r *GitRepo) IsAncestor(ancestor, descendant string) (bool, error)
- func (r *GitRepo) ListCurrentBranchCommitMessages() ([]string, error)
- func (r *GitRepo) MarkMergeConflictsAsResolved() error
- func (r *GitRepo) MergeBranch(branch, mergeIn string) error
- func (r *GitRepo) PushBranch(remote, branch string) error
- func (r *GitRepo) RebaseInProgress() bool
- func (r *GitRepo) ResolveMergeConflicts() error
- func (r *GitRepo) RunCliCommand(command []string) error
- func (r *GitRepo) RunCliCommandAndGetOutput(command []string) (string, error)
- func (r *GitRepo) RunGitCommand(args ...string) error
- func (r *GitRepo) RunGitCommandAndGetOutput(args ...string) (string, error)
- func (r *GitRepo) TrackBranch(branch string, parentBranch string) error
- type MockGitHubClient
- func (c *MockGitHubClient) BatchGetPRChecksStatus(ctx context.Context, branchNames []string) (map[string]*githubpkg.CheckStatus, error)
- func (c *MockGitHubClient) BatchGetPRTitles(_ context.Context, _, _ string, prNumbers []int) (map[int]string, error)
- func (c *MockGitHubClient) ClosePullRequest(ctx context.Context, owner, repo string, prNumber int) error
- func (c *MockGitHubClient) CreatePRComment(ctx context.Context, owner, repo string, prNumber int, body string) (int64, error)
- func (c *MockGitHubClient) CreatePullRequest(ctx context.Context, owner, repo string, opts githubpkg.CreatePROptions) (*githubpkg.PullRequestInfo, error)
- func (c *MockGitHubClient) DeletePRComment(ctx context.Context, owner, repo string, commentID int64) error
- func (c *MockGitHubClient) GetAllowedMergeMethods(_ context.Context) (*githubpkg.MergeMethodSettings, error)
- func (c *MockGitHubClient) GetCurrentUser(_ context.Context) (string, error)
- func (c *MockGitHubClient) GetOwnerRepo() (string, string)
- func (c *MockGitHubClient) GetPRChecksStatus(ctx context.Context, branchName string) (*githubpkg.CheckStatus, error)
- func (c *MockGitHubClient) GetPullRequest(ctx context.Context, owner, repo string, prNumber int) (*githubpkg.PullRequestInfo, error)
- func (c *MockGitHubClient) GetPullRequestByBranch(ctx context.Context, owner, repo, branchName string) (*githubpkg.PullRequestInfo, error)
- func (c *MockGitHubClient) ListPRComments(ctx context.Context, owner, repo string, prNumber int) ([]githubpkg.PRComment, error)
- func (c *MockGitHubClient) MergePullRequest(_ context.Context, _ string, _ githubpkg.MergePROptions) error
- func (c *MockGitHubClient) UpdatePRComment(ctx context.Context, owner, repo string, commentID int64, body string) error
- func (c *MockGitHubClient) UpdatePullRequest(ctx context.Context, owner, repo string, prNumber int, ...) ([]string, error)
- type MockGitHubServerConfig
- type SamplePRData
- type Scene
- type SceneSetup
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BasicSceneSetup ¶
BasicSceneSetup is a setup function that creates a basic scene with a single commit.
func ExpectBranches ¶
ExpectBranches asserts that the repository has the expected branches. It filters out scene-related branches (prod, x2) and compares sorted lists.
func ExpectBranchesString ¶
ExpectBranchesString asserts that the repository has the expected branches as a comma-separated sorted string (matching TypeScript API).
func ExpectCommits ¶
ExpectCommits asserts that the repository has the expected commit messages on the current branch.
func ExpectCommitsString ¶
ExpectCommitsString asserts that the repository has the expected commit messages as a comma-separated string (matching TypeScript API).
func GetBinaryError ¶
func GetBinaryError() error
GetBinaryError returns any error that occurred during binary building.
func GetSharedBinaryPath ¶
func GetSharedBinaryPath() string
GetSharedBinaryPath returns the shared binary path, building it if necessary. This function is safe to call from any test package and will build the binary lazily on first access if it hasn't been set via SetSharedBinaryPath.
func InitialCommitSceneSetup ¶
InitialCommitSceneSetup creates a repo with a conventional "initial" commit.
func Must ¶
Must is a generic helper function that panics if err is not nil, otherwise returns the value. This is useful for test setup code where errors are not expected and should halt execution immediately. Requires Go 1.18+.
func NewMockGitHubClient ¶
func NewMockGitHubClient(t *testing.T, config *MockGitHubServerConfig) (*github.Client, string, string)
NewMockGitHubClient creates a GitHub client configured to use a mock server
func NewMockGitHubClientInterface ¶
func NewMockGitHubClientInterface(client *github.Client, owner, repo string, config *MockGitHubServerConfig) githubpkg.Client
NewMockGitHubClientInterface creates a GitHubClient interface implementation using the mock server
func NewMockGitHubServer ¶
func NewMockGitHubServer(t *testing.T, config *MockGitHubServerConfig) *httptest.Server
NewMockGitHubServer creates an httptest server that mocks GitHub API endpoints
func NewSamplePullRequest ¶
func NewSamplePullRequest(data SamplePRData) *github.PullRequest
NewSamplePullRequest creates a github.PullRequest from sample data
func NewTestPrInfo ¶
NewTestPrInfo creates a PrInfo for testing with common defaults Most common case: open PR with just a number
func NewTestPrInfoClosed ¶
NewTestPrInfoClosed creates a PrInfo for a closed PR
func NewTestPrInfoDraft ¶
NewTestPrInfoDraft creates a PrInfo for a draft PR
func NewTestPrInfoEmpty ¶
NewTestPrInfoEmpty creates an empty PrInfo (useful for clearing PR info)
func NewTestPrInfoFull ¶
func NewTestPrInfoFull(number int, title, body, state, base, url string, isDraft bool) *engine.PrInfo
NewTestPrInfoFull creates a PrInfo with all fields specified
func NewTestPrInfoMerged ¶
NewTestPrInfoMerged creates a PrInfo for a merged PR
func NewTestPrInfoWithState ¶
NewTestPrInfoWithState creates a PrInfo with a specific state
func NewTestPrInfoWithTitle ¶
NewTestPrInfoWithTitle creates a PrInfo with a title
func NormalizeOutput ¶
NormalizeOutput removes variable parts of output and extra whitespace for comparison. It strips ANSI escape codes (lipgloss v2 always generates them) and removes empty lines to make test output comparisons more stable.
func SetSharedBinaryPath ¶
func SetSharedBinaryPath(path string)
SetSharedBinaryPath sets the shared binary path for tests. This is called by TestMain in cli_test package.
Types ¶
type GitRepo ¶
GitRepo represents a Git repository for testing purposes. This is the Go equivalent of the TypeScript GitRepo class.
func NewGitRepo ¶
NewGitRepo initializes a new Git repository in the specified directory using 'git init'.
func NewGitRepoFromExisting ¶
NewGitRepoFromExisting wraps an existing Git repository directory (e.g., a worktree). This does not initialize or clone - it assumes the directory is already a valid git repo.
func NewGitRepoFromTemplate ¶
NewGitRepoFromTemplate clones a repository from a local template using 'git clone --local'.
func NewGitRepoFromURL ¶
NewGitRepoFromURL clones a repository from a remote URL.
func (*GitRepo) CheckoutBranch ¶
CheckoutBranch checks out a branch.
func (*GitRepo) CheckoutDetached ¶
CheckoutDetached checks out a revision in detached HEAD state.
func (*GitRepo) CreateAndCheckoutBranch ¶
CreateAndCheckoutBranch creates and checks out a new branch.
func (*GitRepo) CreateBareRemote ¶
CreateBareRemote creates a bare git repository to act as a remote. Returns the path to the bare repository.
func (*GitRepo) CreateBranch ¶
CreateBranch creates a new branch without checking it out.
func (*GitRepo) CreateChange ¶
CreateChange creates a file change in the repository.
func (*GitRepo) CreateChangeAndAmend ¶
CreateChangeAndAmend creates a file change and amends the last commit.
func (*GitRepo) CreateChangeAndCommit ¶
CreateChangeAndCommit creates a file change and commits it.
func (*GitRepo) CreatePrecommitHook ¶
CreatePrecommitHook creates a pre-commit hook.
func (*GitRepo) CurrentBranchName ¶
CurrentBranchName returns the name of the current branch.
func (*GitRepo) DeleteBranch ¶
DeleteBranch deletes a branch.
func (*GitRepo) ForcePushBranch ¶
ForcePushBranch force pushes a branch to a remote.
func (*GitRepo) GetBranchSHA ¶
GetBranchSHA returns the SHA of a branch.
func (*GitRepo) GetCommitCount ¶
GetCommitCount returns the number of commits between two refs.
func (*GitRepo) GetCurrentSHA ¶
GetCurrentSHA returns the SHA of HEAD.
func (*GitRepo) GetLocalBranches ¶
GetLocalBranches returns a list of all local branches.
func (*GitRepo) GetRevision ¶
GetRevision returns the SHA of a revision (branch, tag, or commit reference).
func (*GitRepo) HasUnstagedChanges ¶
HasUnstagedChanges checks if there are unstaged changes to tracked files.
func (*GitRepo) HasUntrackedFiles ¶
HasUntrackedFiles checks if there are untracked files.
func (*GitRepo) IsAncestor ¶
IsAncestor checks if the first ref is an ancestor of the second ref.
func (*GitRepo) ListCurrentBranchCommitMessages ¶
ListCurrentBranchCommitMessages returns the commit messages on the current branch.
func (*GitRepo) MarkMergeConflictsAsResolved ¶
MarkMergeConflictsAsResolved marks merge conflicts as resolved.
func (*GitRepo) MergeBranch ¶
MergeBranch merges a branch into another.
func (*GitRepo) PushBranch ¶
PushBranch pushes a branch to a remote.
func (*GitRepo) RebaseInProgress ¶
RebaseInProgress checks if a rebase is in progress.
func (*GitRepo) ResolveMergeConflicts ¶
ResolveMergeConflicts resolves merge conflicts by accepting theirs.
func (*GitRepo) RunCliCommand ¶
RunCliCommand executes a Stackit CLI command in the repository directory.
func (*GitRepo) RunCliCommandAndGetOutput ¶
RunCliCommandAndGetOutput executes a Stackit CLI command and returns its output.
func (*GitRepo) RunGitCommand ¶
RunGitCommand executes a git command and returns an error if it fails.
func (*GitRepo) RunGitCommandAndGetOutput ¶
RunGitCommandAndGetOutput executes a git command and returns its output.
type MockGitHubClient ¶
type MockGitHubClient struct {
// contains filtered or unexported fields
}
MockGitHubClient implements githubpkg.Client using the mock server
func (*MockGitHubClient) BatchGetPRChecksStatus ¶
func (c *MockGitHubClient) BatchGetPRChecksStatus(ctx context.Context, branchNames []string) (map[string]*githubpkg.CheckStatus, error)
BatchGetPRChecksStatus returns the check status for multiple branches
func (*MockGitHubClient) BatchGetPRTitles ¶
func (c *MockGitHubClient) BatchGetPRTitles(_ context.Context, _, _ string, prNumbers []int) (map[int]string, error)
BatchGetPRTitles returns synthetic titles for testing
func (*MockGitHubClient) ClosePullRequest ¶
func (c *MockGitHubClient) ClosePullRequest(ctx context.Context, owner, repo string, prNumber int) error
ClosePullRequest closes a pull request
func (*MockGitHubClient) CreatePRComment ¶
func (c *MockGitHubClient) CreatePRComment(ctx context.Context, owner, repo string, prNumber int, body string) (int64, error)
CreatePRComment creates a new comment on a pull request
func (*MockGitHubClient) CreatePullRequest ¶
func (c *MockGitHubClient) CreatePullRequest(ctx context.Context, owner, repo string, opts githubpkg.CreatePROptions) (*githubpkg.PullRequestInfo, error)
CreatePullRequest creates a new pull request
func (*MockGitHubClient) DeletePRComment ¶
func (c *MockGitHubClient) DeletePRComment(ctx context.Context, owner, repo string, commentID int64) error
DeletePRComment deletes a pull request comment
func (*MockGitHubClient) GetAllowedMergeMethods ¶
func (c *MockGitHubClient) GetAllowedMergeMethods(_ context.Context) (*githubpkg.MergeMethodSettings, error)
GetAllowedMergeMethods returns the allowed merge methods for the repository
func (*MockGitHubClient) GetCurrentUser ¶
func (c *MockGitHubClient) GetCurrentUser(_ context.Context) (string, error)
GetCurrentUser returns a mock GitHub username
func (*MockGitHubClient) GetOwnerRepo ¶
func (c *MockGitHubClient) GetOwnerRepo() (string, string)
GetOwnerRepo returns the repository owner and name
func (*MockGitHubClient) GetPRChecksStatus ¶
func (c *MockGitHubClient) GetPRChecksStatus(ctx context.Context, branchName string) (*githubpkg.CheckStatus, error)
GetPRChecksStatus returns the check status for a PR
func (*MockGitHubClient) GetPullRequest ¶
func (c *MockGitHubClient) GetPullRequest(ctx context.Context, owner, repo string, prNumber int) (*githubpkg.PullRequestInfo, error)
GetPullRequest gets a pull request by number
func (*MockGitHubClient) GetPullRequestByBranch ¶
func (c *MockGitHubClient) GetPullRequestByBranch(ctx context.Context, owner, repo, branchName string) (*githubpkg.PullRequestInfo, error)
GetPullRequestByBranch gets a pull request for a branch
func (*MockGitHubClient) ListPRComments ¶
func (c *MockGitHubClient) ListPRComments(ctx context.Context, owner, repo string, prNumber int) ([]githubpkg.PRComment, error)
ListPRComments lists all comments on a pull request
func (*MockGitHubClient) MergePullRequest ¶
func (c *MockGitHubClient) MergePullRequest(_ context.Context, _ string, _ githubpkg.MergePROptions) error
MergePullRequest merges a pull request using the specified merge method
func (*MockGitHubClient) UpdatePRComment ¶
func (c *MockGitHubClient) UpdatePRComment(ctx context.Context, owner, repo string, commentID int64, body string) error
UpdatePRComment updates an existing pull request comment
func (*MockGitHubClient) UpdatePullRequest ¶
func (c *MockGitHubClient) UpdatePullRequest(ctx context.Context, owner, repo string, prNumber int, opts githubpkg.UpdatePROptions) ([]string, error)
UpdatePullRequest updates an existing pull request
type MockGitHubServerConfig ¶
type MockGitHubServerConfig struct {
// PRs maps branch names to PR data for GetPullRequestByBranch
PRs map[string]*github.PullRequest
// CreatedPRs stores PRs that were created (for testing)
CreatedPRs []*github.PullRequest
// UpdatedPRs stores PRs that were updated (for testing)
UpdatedPRs map[int]*github.PullRequest
// ErrorResponses maps endpoint+method to error responses
ErrorResponses map[string]error
// Owner and Repo for the mock server
Owner string
Repo string
// contains filtered or unexported fields
}
MockGitHubServerConfig configures the behavior of a mock GitHub server
func NewMockGitHubServerConfig ¶
func NewMockGitHubServerConfig() *MockGitHubServerConfig
NewMockGitHubServerConfig creates a new mock server config with defaults
type SamplePRData ¶
type SamplePRData struct {
Number int
Title string
Body string
Head string
Base string
HTMLURL string
Draft bool
State string
Reviewers []string
TeamReviewers []string
}
SamplePRData provides common PR data for testing
func DefaultPRData ¶
func DefaultPRData() SamplePRData
DefaultPRData returns a default PR data structure for testing
func PRWithReviewersData ¶
func PRWithReviewersData(reviewers []string, teamReviewers []string) SamplePRData
PRWithReviewersData returns PR data with reviewers
type Scene ¶
Scene represents a test scene with a temporary directory and Git repository. This is the Go equivalent of the TypeScript AbstractScene.
func NewRemoteSceneParallel ¶
NewRemoteSceneParallel creates a parallel-safe scene with an initial commit, a local bare "origin" remote, and main pushed to that remote.
func NewScene ¶
func NewScene(t *testing.T, setup SceneSetup) *Scene
NewScene creates a new test scene with a temporary directory and Git repository. It automatically handles cleanup using t.Cleanup(). NOTE: This function uses os.Chdir() and is NOT safe for parallel tests. Use NewSceneParallel for tests that can run in parallel.
func NewSceneParallel ¶
func NewSceneParallel(t *testing.T, setup SceneSetup) *Scene
NewSceneParallel creates a new test scene that is safe for parallel tests. Unlike NewScene, this does NOT change the working directory. Tests using this must ensure all git operations use explicit directory paths (e.g., via scene.Repo methods or cmd.Dir = scene.Dir).
type SceneSetup ¶
SceneSetup is a function type for setting up a scene.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package inprocess provides in-process CLI execution for tests.
|
Package inprocess provides in-process CLI execution for tests. |
|
Package scenario provides a high-level test scenario that combines a Scene, an Engine, and a runtime Context to provide a terse API for integration tests.
|
Package scenario provides a high-level test scenario that combines a Scene, an Engine, and a runtime Context to provide a terse API for integration tests. |