fakegh

package
v0.24.2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fakegh is an in-memory GitHub REST and GraphQL server for the endpoints loupe calls, reached through the production client so request encoding is exercised end to end.

Index

Constants

View Source
const PullRequestCommitCap = 250

PullRequestCommitCap is the most commits GitHub lists for a pull request.

Variables

This section is empty.

Functions

func IsolateConfig

func IsolateConfig(dir string) error

IsolateConfig points GH_CONFIG_DIR at dir for the whole process. A TestMain whose tests call t.Parallel MUST call it before m.Run, because Client otherwise calls t.Setenv, which panics in a parallel test.

Types

type Outcome

type Outcome struct {
	// contains filtered or unexported fields
}

func OK

func OK() Outcome

OK stores the review and returns it.

func OKWithoutIdentity

func OKWithoutIdentity() Outcome

OKWithoutIdentity stores nothing and returns 200 with an empty object, a success that names no review.

func Reject422

func Reject422(message string) Outcome

Reject422 stores nothing and returns 422 with message.

func Reject422Errors

func Reject422Errors(message string, errors ...string) Outcome

Reject422Errors stores nothing and returns 422 in GitHub's shape with a generic message and the reasons in errors.

func ServerErrorAfterRecord

func ServerErrorAfterRecord() Outcome

ServerErrorAfterRecord stores the review and still returns 500, as when GitHub's response is lost.

func ServerErrorDrop

func ServerErrorDrop() Outcome

ServerErrorDrop returns 500 without storing anything.

func Unauthorized

func Unauthorized() Outcome

Unauthorized stores nothing and returns 401, as when GitHub rejects the token.

type Request

type Request struct {
	Method   string
	Path     string
	RawQuery string
	// Body is the decoded JSON body, nil when the request had none.
	Body any
}

func (Request) Read

func (r Request) Read() bool

Read reports whether the request only reads: a GET, or a GraphQL query, which GitHub takes as a POST.

type Server

type Server struct {
	URL string
	// contains filtered or unexported fields
}

func New

func New(t *testing.T) *Server

func Start

func Start() (s *Server, stop func())

Start serves the fake outside a test, for the demo program; stop closes the listener.

func (*Server) AddIssueComment

func (s *Server) AddIssueComment(owner, repo string, number int, c github.IssueComment) github.IssueComment

AddIssueComment stores a top-level pull request comment and returns it with its id and URL.

func (*Server) AddReview

func (s *Server) AddReview(owner, repo string, number int, review github.Review)

AddReview stores a review as if it already existed; a zero ID and empty HTMLURL are assigned.

func (*Server) AddReviewThread

func (s *Server) AddReviewThread(owner, repo string, number int, t github.ReviewThread)

AddReviewThread stores an inline thread. A comment's User ending in [bot] is served as a GraphQL Bot without the suffix, and an empty User as a deleted account, so the client's naming is exercised.

func (*Server) AllowUser

func (s *Server) AllowUser()

AllowUser undoes DenyUser, for a test that hands the same pull request from an App back to a person.

func (*Server) Client

func (s *Server) Client(t *testing.T) github.Client

Client builds the production REST client against this server with a fixed token.

func (*Server) CreateCount

func (s *Server) CreateCount() int

CreateCount is the number of review creation requests received, whatever their outcome.

func (*Server) DenyUser

func (s *Server) DenyUser()

DenyUser makes GET /user answer 403, as GitHub does for an App installation token. The created review's author still follows SetViewer.

func (*Server) EditReview

func (s *Server) EditReview(owner, repo string, number int, id int64, body string)

EditReview replaces a stored review's body, as another round or the author on GitHub would.

func (*Server) Fail

func (s *Server) Fail(method, path string, status int)

Fail answers every later request with method and exact path with status, after recording it; status 0 stops.

func (*Server) FailAfter

func (s *Server) FailAfter(method, path string, after, status int)

FailAfter answers the requests with method and exact path with status once after have been answered normally, so a listing can fail on a later page.

func (*Server) NewClient

func (s *Server) NewClient() (github.Client, error)

NewClient is Client for a caller without a test, which MUST point GH_CONFIG_DIR away from the developer's own.

func (*Server) OnCreate

func (s *Server) OnCreate(fn func(*http.Request))

OnCreate runs fn on every review creation request before the server stores or responds to anything. fn may read the request body.

func (*Server) OnUpdate

func (s *Server) OnUpdate(fn func(*http.Request))

OnUpdate runs fn on every review edit request before the server stores or responds to anything.

func (*Server) QueueCreate

func (s *Server) QueueCreate(outcomes ...Outcome)

QueueCreate sets the outcomes of the next review creations in order; once empty, creations succeed.

func (*Server) QueueUpdate

func (s *Server) QueueUpdate(outcomes ...Outcome)

QueueUpdate sets the outcomes of the next review body edits in order; once empty, edits succeed. A rejection or a dropped edit leaves the body as it was, and ServerErrorAfterRecord replaces it.

func (*Server) Requests

func (s *Server) Requests() []Request

func (*Server) SetBranch

func (s *Server) SetBranch(owner, repo string, number int, branch string)

SetBranch names the pull request's head branch for the branch lookup endpoint.

func (*Server) SetCommits

func (s *Server) SetCommits(owner, repo string, number int, commits ...github.Commit)

SetCommits is the pull request's commit list, in the oldest-first order loupe assumes GitHub uses. The fake lists at most PullRequestCommitCap of them, as GitHub does.

func (*Server) SetComparison

func (s *Server) SetComparison(owner, repo, base, head string, c github.Comparison)

SetComparison is what comparing base to head returns; a pair never set is a 404, as for a commit GitHub does not have.

func (*Server) SetForkBranch

func (s *Server) SetForkBranch(owner, repo string, number int, headOwner, branch string)

SetForkBranch names a head branch that lives in headOwner's fork.

func (*Server) SetHead

func (s *Server) SetHead(owner, repo string, number int, sha string)

func (*Server) SetLatestRelease

func (s *Server) SetLatestRelease(tag string)

SetLatestRelease sets the tag GET /repos/{owner}/{repo}/releases/latest answers with, for every repository.

func (*Server) SetPR

func (s *Server) SetPR(owner, repo string, pr github.PullRequest)

func (*Server) SetPageSize

func (s *Server) SetPageSize(n int)

SetPageSize caps every listing's page at n items whatever the client asks for, as GitHub MAY, so a test crosses pages with a few items; 0 removes the cap.

func (*Server) SetViewer

func (s *Server) SetViewer(login string)

func (*Server) StallLatestRelease

func (s *Server) StallLatestRelease() <-chan struct{}

StallLatestRelease makes the latest-release request hang until the caller cancels it. The returned channel closes when the first such request reaches the server, so a test cancels on arrival, not on a timer.

func (*Server) StallPullRequest

func (s *Server) StallPullRequest() <-chan struct{}

StallPullRequest makes the single pull request request hang until the caller cancels it. The returned channel closes when the first such request reaches the server, so a test can cancel a request it knows is in flight.

func (*Server) UpdateCount

func (s *Server) UpdateCount() int

UpdateCount is the number of review edit requests received, whatever their outcome.

Jump to

Keyboard shortcuts

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