Documentation
¶
Overview ¶
Package testutil provides testing utilities, fixtures, and mocks for unit tests.
Index ¶
- Variables
- func APIRunListJSON(runs ...APIRun) string
- func AsGHRunViewLog(job, step, raw string) string
- func AssertCommand(t *testing.T, cmd ExecutedCommand, expectedArgs ...string)
- func AssertStepLogNames(t *testing.T, stepLogs []*logs.StepLogs, expectedNames []string)
- func DrainChainUpdates(t *testing.T, updates <-chan chain.ChainUpdate, timeout time.Duration)
- func GenerateANSILog() string
- func GenerateLargeLogFixture(lines int) string
- func GenerateMixedLog(lines int) string
- func GenerateUnicodeLog() string
- func LatestRunPath(owner, repo, workflow string) string
- func MustMarshalJSON(t *testing.T, v any) string
- type APIRun
- type CommandResult
- type ExecutedCommand
- type MockExecutor
- func (m *MockExecutor) AddCommand(name string, args []string, stdout, stderr string, err error)
- func (m *MockExecutor) AddGHAPIJobs(owner, repo string, runID int64, jobs string)
- func (m *MockExecutor) AddGHAPILatestRun(owner, repo, workflow string, runID int64, status string)
- func (m *MockExecutor) AddGHAPIRun(owner, repo string, runID int64, status, conclusion string)
- func (m *MockExecutor) AddGHAuthStatus(authenticated bool, username string)
- func (m *MockExecutor) AddGHRunView(runID, jobID int64, logOutput string)
- func (m *MockExecutor) AddGHRunViewError(runID, jobID int64, stderr string, err error)
- func (m *MockExecutor) AddGHVersion(version string)
- func (m *MockExecutor) AddGHWorkflowRun(workflow, branch string, inputs map[string]string)
- func (m *MockExecutor) AddGHWorkflowRunError(workflow, branch, stderr string, err error)
- func (m *MockExecutor) Execute(name string, args ...string) (string, string, error)
- func (m *MockExecutor) Reset()
- type MockGitHubClient
- func (m *MockGitHubClient) GetLatestRun(workflow string) (*github.WorkflowRun, error)
- func (m *MockGitHubClient) GetWorkflowRun(runID int64) (*github.WorkflowRun, error)
- func (m *MockGitHubClient) GetWorkflowRunJobs(runID int64) ([]github.Job, error)
- func (m *MockGitHubClient) ListRuns(q github.RunQuery) ([]github.WorkflowRun, error)
- func (m *MockGitHubClient) Owner() string
- func (m *MockGitHubClient) Repo() string
- func (m *MockGitHubClient) WithError(err error) *MockGitHubClient
- func (m *MockGitHubClient) WithJobs(runID int64, jobs []github.Job) *MockGitHubClient
- func (m *MockGitHubClient) WithOwnerRepo(owner, repo string) *MockGitHubClient
- func (m *MockGitHubClient) WithRun(run *github.WorkflowRun) *MockGitHubClient
- type MockRunWatcher
Constants ¶
This section is empty.
Variables ¶
var ErrMockCommandNotConfigured = errors.New("mock executor: no result configured for command")
ErrMockCommandNotConfigured indicates the MockExecutor has no result configured for a command.
var ErrMockExitStatus1 = errors.New("exit status 1")
ErrMockExitStatus1 simulates a generic "exit status 1" failure from an external command.
var ErrMockExitStatus127 = errors.New("exit status 127")
ErrMockExitStatus127 simulates a "command not found" (exit status 127) failure.
Functions ¶
func APIRunListJSON ¶ added in v1.9.0
APIRunListJSON renders a run listing the way the Actions API returns one.
func AsGHRunViewLog ¶ added in v1.2.2
AsGHRunViewLog prefixes each line of raw with the job name, step name, and a timestamp, the shape `gh run view --log` emits. A fixture without that prefix exercises no step-splitting at all, which is how a parser that never worked against real output stayed green.
func AssertCommand ¶
func AssertCommand(t *testing.T, cmd ExecutedCommand, expectedArgs ...string)
AssertCommand verifies that an executed command matches expected arguments.
func AssertStepLogNames ¶
AssertStepLogNames verifies that step logs have the expected names.
func DrainChainUpdates ¶
DrainChainUpdates reads all updates from the channel until it closes or times out.
func GenerateANSILog ¶
func GenerateANSILog() string
GenerateANSILog creates logs with ANSI color codes. Tests proper handling of terminal color escape sequences. Includes ##[group] markers for proper parsing.
func GenerateLargeLogFixture ¶
GenerateLargeLogFixture creates a realistic log file with N lines. Uses GitHub Actions log format patterns for authenticity.
func GenerateMixedLog ¶
GenerateMixedLog creates a log with various patterns for comprehensive testing. Includes errors, warnings, unicode, ANSI codes, and normal logs.
func GenerateUnicodeLog ¶
func GenerateUnicodeLog() string
GenerateUnicodeLog creates logs with unicode characters. Tests proper handling of international characters and emoji.
func LatestRunPath ¶ added in v1.9.0
LatestRunPath is the gh api path a latest-run read asks for.
Types ¶
type APIRun ¶ added in v1.9.0
type APIRun struct {
CreatedAt time.Time `json:"created_at,omitzero"`
UpdatedAt time.Time `json:"updated_at,omitzero"`
Name string `json:"name"`
Status string `json:"status"`
Conclusion string `json:"conclusion,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
HeadBranch string `json:"head_branch,omitempty"`
Event string `json:"event,omitempty"`
Path string `json:"path,omitempty"`
ID int64 `json:"id"`
}
APIRun is one run as the Actions API spells it. A fixture built from github.WorkflowRun would carry Go field names, which gh never emits, so the parser under test would be reading a shape that does not exist.
type CommandResult ¶ added in v1.9.0
CommandResult represents the result of a command execution.
type ExecutedCommand ¶ added in v1.9.0
ExecutedCommand tracks a command that was executed.
type MockExecutor ¶ added in v1.9.0
type MockExecutor struct {
// Commands maps command patterns to responses.
// Key format: "command arg1 arg2"
Commands map[string]*CommandResult
// DefaultResult is returned when no specific command matches.
DefaultResult *CommandResult
// ExecutedCommands tracks all commands that were executed.
ExecutedCommands []ExecutedCommand
}
MockExecutor simulates command execution for testing.
func NewMockExecutor ¶ added in v1.9.0
func NewMockExecutor() *MockExecutor
NewMockExecutor creates a new mock executor.
func (*MockExecutor) AddCommand ¶ added in v1.9.0
func (m *MockExecutor) AddCommand(name string, args []string, stdout, stderr string, err error)
AddCommand registers a command response.
func (*MockExecutor) AddGHAPIJobs ¶ added in v1.9.0
func (m *MockExecutor) AddGHAPIJobs(owner, repo string, runID int64, jobs string)
AddGHAPIJobs mocks a gh api call for workflow run jobs.
func (*MockExecutor) AddGHAPILatestRun ¶ added in v1.9.0
func (m *MockExecutor) AddGHAPILatestRun(owner, repo, workflow string, runID int64, status string)
AddGHAPILatestRun mocks a gh api call for the latest run of a workflow file. A workflow is filtered on its own endpoint rather than a query parameter, which the runs collection has none of.
func (*MockExecutor) AddGHAPIRun ¶ added in v1.9.0
func (m *MockExecutor) AddGHAPIRun(owner, repo string, runID int64, status, conclusion string)
AddGHAPIRun mocks a gh api call for workflow run data.
func (*MockExecutor) AddGHAuthStatus ¶ added in v1.9.0
func (m *MockExecutor) AddGHAuthStatus(authenticated bool, username string)
AddGHAuthStatus mocks the gh auth status command.
func (*MockExecutor) AddGHRunView ¶ added in v1.9.0
func (m *MockExecutor) AddGHRunView(runID, jobID int64, logOutput string)
AddGHRunView is a convenience method for adding gh run view commands.
func (*MockExecutor) AddGHRunViewError ¶ added in v1.9.0
func (m *MockExecutor) AddGHRunViewError(runID, jobID int64, stderr string, err error)
AddGHRunViewError is a convenience method for adding failing gh run view commands.
func (*MockExecutor) AddGHVersion ¶ added in v1.9.0
func (m *MockExecutor) AddGHVersion(version string)
AddGHVersion mocks the gh --version command.
func (*MockExecutor) AddGHWorkflowRun ¶ added in v1.9.0
func (m *MockExecutor) AddGHWorkflowRun(workflow, branch string, inputs map[string]string)
AddGHWorkflowRun mocks a successful gh workflow run command.
func (*MockExecutor) AddGHWorkflowRunError ¶ added in v1.9.0
func (m *MockExecutor) AddGHWorkflowRunError(workflow, branch, stderr string, err error)
AddGHWorkflowRunError mocks a failing gh workflow run command.
func (*MockExecutor) Execute ¶ added in v1.9.0
Execute simulates command execution by looking up the command in the Commands map.
func (*MockExecutor) Reset ¶ added in v1.9.0
func (m *MockExecutor) Reset()
Reset clears all command history and configurations.
type MockGitHubClient ¶
type MockGitHubClient struct {
Err error
ListRunsFunc func(q github.RunQuery) ([]github.WorkflowRun, error)
Runs map[int64]*github.WorkflowRun
Jobs map[int64][]github.Job
LatestByWorkflow map[string]int64
LatestID int64
// contains filtered or unexported fields
}
MockGitHubClient implements both chain.GitHubClient and watcher.GitHubClient interfaces.
func NewMockGitHubClient ¶
func NewMockGitHubClient() *MockGitHubClient
NewMockGitHubClient creates a MockGitHubClient with sensible defaults.
func (*MockGitHubClient) GetLatestRun ¶
func (m *MockGitHubClient) GetLatestRun(workflow string) (*github.WorkflowRun, error)
GetLatestRun returns the mocked latest run for workflow.
func (*MockGitHubClient) GetWorkflowRun ¶
func (m *MockGitHubClient) GetWorkflowRun(runID int64) (*github.WorkflowRun, error)
GetWorkflowRun returns the mocked run for runID, or a queued stub if none is configured.
func (*MockGitHubClient) GetWorkflowRunJobs ¶
func (m *MockGitHubClient) GetWorkflowRunJobs(runID int64) ([]github.Job, error)
GetWorkflowRunJobs returns the mocked jobs for runID.
func (*MockGitHubClient) ListRuns ¶ added in v1.12.0
func (m *MockGitHubClient) ListRuns(q github.RunQuery) ([]github.WorkflowRun, error)
ListRuns returns ListRunsFunc's result, or an empty listing if unset.
func (*MockGitHubClient) Owner ¶
func (m *MockGitHubClient) Owner() string
Owner returns the mocked repository owner.
func (*MockGitHubClient) Repo ¶
func (m *MockGitHubClient) Repo() string
Repo returns the mocked repository name.
func (*MockGitHubClient) WithError ¶
func (m *MockGitHubClient) WithError(err error) *MockGitHubClient
WithError sets the error to return from all methods.
func (*MockGitHubClient) WithJobs ¶
func (m *MockGitHubClient) WithJobs(runID int64, jobs []github.Job) *MockGitHubClient
WithJobs adds jobs for a run to the mock.
func (*MockGitHubClient) WithOwnerRepo ¶
func (m *MockGitHubClient) WithOwnerRepo(owner, repo string) *MockGitHubClient
WithOwnerRepo sets the owner and repo for the mock client.
func (*MockGitHubClient) WithRun ¶
func (m *MockGitHubClient) WithRun(run *github.WorkflowRun) *MockGitHubClient
WithRun adds a workflow run to the mock.
type MockRunWatcher ¶
MockRunWatcher implements chain.RunWatcher interface.
func NewMockRunWatcher ¶
func NewMockRunWatcher() *MockRunWatcher
NewMockRunWatcher creates a new MockRunWatcher.
func (*MockRunWatcher) SendUpdate ¶
func (m *MockRunWatcher) SendUpdate(update watcher.RunUpdate)
SendUpdate sends an update to the mock watcher's channel (for testing).
func (*MockRunWatcher) Unwatch ¶
func (m *MockRunWatcher) Unwatch(runID int64)
Unwatch removes runID from the watched set.
func (*MockRunWatcher) Updates ¶
func (m *MockRunWatcher) Updates() <-chan watcher.RunUpdate
Updates returns the mocked run update channel.
func (*MockRunWatcher) Watch ¶
func (m *MockRunWatcher) Watch(runID int64, workflowName string)
Watch records runID as watched for workflowName.