Documentation
¶
Overview ¶
Package fake provides an in-memory scm.Provider implementation for tests and local examples.
The fake provider mirrors core provider behavior but performs no network calls, which makes it useful for deterministic unit and integration tests.
Index ¶
- func CreateTestRepositories(project string) []*scm.Repository
- func New(_ context.Context, project string) scm.Provider
- type Fake
- func (f *Fake) AddRepositories(repos ...*scm.Repository)
- func (f *Fake) AddRepository(repo *scm.Repository)
- func (f *Fake) CheckCapabilities(opts *scm.PROptions) error
- func (f *Fake) Clear()
- func (f *Fake) ClearAllErrors()
- func (f *Fake) ClearError(method string)
- func (f *Fake) GetAllLabels() []string
- func (f *Fake) GetPullRequest(repo, branch string) (*scm.PullRequest, error)
- func (f *Fake) GetPullRequestCount() int
- func (f *Fake) GetRepositoriesByLabel(label string) []*scm.Repository
- func (f *Fake) GetRepositoryByName(name string) *scm.Repository
- func (f *Fake) GetRepositoryCount() int
- func (f *Fake) HasPullRequest(repo, branch string) bool
- func (f *Fake) ListRepositories() ([]*scm.Repository, error)
- func (f *Fake) MergePullRequest(repo, branch string, opts *scm.PRMergeOptions) (*scm.PullRequest, error)
- func (f *Fake) OpenPullRequest(repo, branch string, opts *scm.PROptions) (*scm.PullRequest, error)
- func (f *Fake) SeedErrors(errors map[string]error)
- func (f *Fake) SetError(method string, err error)
- func (f *Fake) SetPRMergeable(repo, branch string, mergeable bool) error
- func (f *Fake) UpdatePullRequest(repo, branch string, opts *scm.PROptions) (*scm.PullRequest, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateTestRepositories ¶
func CreateTestRepositories(project string) []*scm.Repository
CreateTestRepositories creates a set of test repositories with various labels
Example ¶
ExampleCreateTestRepositories demonstrates using the CreateTestRepositories helper
// Create a fake provider with pre-built test data
testRepos := CreateTestRepositories("test-project")
fake := NewFake("test-project", testRepos)
// Get repositories by label
activeRepos := fake.GetRepositoriesByLabel("active")
println("Found", len(activeRepos), "active repositories")
// Get all available labels
labels := fake.GetAllLabels()
println("Available labels:", len(labels))
Types ¶
type Fake ¶
type Fake struct {
Project string
Repositories []*scm.Repository
PullRequests map[string]*scm.PullRequest // key: "repo:branch"
Errors map[string]error // configurable errors for testing
Capabilities *scm.Capabilities // configurable capabilities for testing
}
Fake implements a mock SCM provider for testing purposes
Example (BasicUsage) ¶
ExampleFake_basicUsage demonstrates basic usage of the fake SCM provider
ctx := context.Background()
// Create a new fake provider
fake := New(ctx, "my-project").(*Fake)
// Add some test repositories
fake.AddRepository(&scm.Repository{
Name: "repo-1",
Description: "First repository",
Project: "my-project",
Labels: []string{"backend", "go"},
})
fake.AddRepository(&scm.Repository{
Name: "repo-2",
Description: "Second repository",
Project: "my-project",
Labels: []string{"frontend", "javascript"},
})
// List repositories
repos, _ := fake.ListRepositories()
println("Found", len(repos), "repositories")
// Create a pull request
pr, _ := fake.OpenPullRequest("repo-1", "feature-branch", &scm.PROptions{Title: "My Feature", Description: "Description", Reviewers: []string{"reviewer1"}})
println("Created PR with ID:", pr.ID)
Example (ErrorTesting) ¶
ExampleFake_errorTesting demonstrates how to configure errors for testing
ctx := context.Background()
fake := New(ctx, "test-project").(*Fake)
// Configure the provider to return an error
fake.SetError("ListRepositories", fmt.Errorf("simulated API error"))
// Now ListRepositories will return the configured error
_, err := fake.ListRepositories()
if err != nil {
println("Got expected error:", err.Error())
}
// Clear the error
fake.ClearError("ListRepositories")
// Now it works normally again
repos, err := fake.ListRepositories()
if err == nil {
println("No error, found", len(repos), "repositories")
}
func NewFake ¶
func NewFake(project string, repos []*scm.Repository, caps ...*scm.Capabilities) *Fake
NewFake creates a new fake SCM provider with the specified project and optional seed data. Optionally pass a Capabilities struct to override the default (which supports all features).
func (*Fake) AddRepositories ¶
func (f *Fake) AddRepositories(repos ...*scm.Repository)
AddRepositories adds multiple repositories to the fake provider
func (*Fake) AddRepository ¶
func (f *Fake) AddRepository(repo *scm.Repository)
AddRepository adds a repository to the fake provider
func (*Fake) CheckCapabilities ¶ added in v0.11.0
CheckCapabilities validates that the provided PR options are supported by the fake provider.
func (*Fake) ClearAllErrors ¶
func (f *Fake) ClearAllErrors()
ClearAllErrors removes all configured errors
func (*Fake) ClearError ¶
ClearError removes a configured error for a specific method
func (*Fake) GetAllLabels ¶
GetAllLabels returns all unique labels across all repositories
func (*Fake) GetPullRequest ¶
func (f *Fake) GetPullRequest(repo, branch string) (*scm.PullRequest, error)
GetPullRequest retrieves a pull request by repository name and source branch
func (*Fake) GetPullRequestCount ¶
GetPullRequestCount returns the number of pull requests in the fake provider
func (*Fake) GetRepositoriesByLabel ¶
func (f *Fake) GetRepositoriesByLabel(label string) []*scm.Repository
GetRepositoriesByLabel returns repositories that have the specified label
func (*Fake) GetRepositoryByName ¶
func (f *Fake) GetRepositoryByName(name string) *scm.Repository
GetRepositoryByName returns a repository by name, or nil if not found
func (*Fake) GetRepositoryCount ¶
GetRepositoryCount returns the number of repositories in the fake provider
func (*Fake) HasPullRequest ¶
HasPullRequest checks if a pull request exists for the given repo and branch
func (*Fake) ListRepositories ¶
func (f *Fake) ListRepositories() ([]*scm.Repository, error)
ListRepositories returns the configured repositories
func (*Fake) MergePullRequest ¶
func (f *Fake) MergePullRequest(repo, branch string, opts *scm.PRMergeOptions) (*scm.PullRequest, error)
MergePullRequest merges an existing pull request
func (*Fake) OpenPullRequest ¶
OpenPullRequest creates a new pull request
func (*Fake) SeedErrors ¶
SeedErrors configures the provider to return specific errors for testing
func (*Fake) SetPRMergeable ¶ added in v0.9.6
SetPRMergeable sets the mergeable status of a pull request for testing
func (*Fake) UpdatePullRequest ¶
func (f *Fake) UpdatePullRequest(repo, branch string, opts *scm.PROptions) (*scm.PullRequest, error)
UpdatePullRequest updates an existing pull request