FakeGit#

class graftkit.testing.FakeGit[source]#

Bases:

An in-memory Git: clones are folders it creates, pull requests are recorded.

Attributes:
clones

Each clone it made, by folder; replace one to change its state, e.g. clean=False.

unreachable

Repositories whose clone fails, as gh reports a repository it cannot see.

permission

The person’s access to every repository; below WRITE, proposing fails.

content

The folder propose applies a change to, standing in for a fresh clone.

opened

Every pull request opened, in order.

Methods

clone(repository, folder, config)

Create a folder holding .git and record the clone.

origin(folder)

Return a clone's origin URL.

branch(folder)

Return a clone's branch.

is_clean(folder)

Whether a clone has no changes.

fast_forward(folder)

Fast-forward a clone that is behind.

propose(repository, branch, title, body, change)

Apply a change to content and record the pull request.

propose_here(root, branch, title, body, apply)

Run a change in a checkout and record the pull request.

clone(repository: str, folder: Path, config: Mapping[str, str]) → None[source]#

Create a folder holding .git and record the clone.

Parameters:
repositorystr

owner/name.

folderPath

The new clone’s folder; it must not exist.

configMapping[str, str]

The git config the clone is made with.

Raises:
CliError

If the repository is unreachable or the folder exists, as gh reports it.

origin(folder: Path) → str | None[source]#

Return a clone’s origin URL.

Parameters:
folderPath

The clone.

Returns:
str | None

Its origin, or None if it has none.

branch(folder: Path) → str[source]#

Return a clone’s branch.

Parameters:
folderPath

The clone.

Returns:
str

The branch it has checked out.

is_clean(folder: Path) → bool[source]#

Whether a clone has no changes.

Parameters:
folderPath

The clone.

Returns:
bool

Its clean state.

fast_forward(folder: Path) → bool[source]#

Fast-forward a clone that is behind.

Parameters:
folderPath

The clone.

Returns:
bool

Whether it moved: it was behind, and no longer is.

Raises:
CliError

If it diverged, as git pull --ff-only reports it.

propose(repository: str, branch: str, title: str, body: str, change: Callable[[Path], None]) → str[source]#

Apply a change to content and record the pull request.

Parameters:
repositorystr

owner/name.

branchstr

The branch to propose.

titlestr

The pull request’s title.

bodystr

Its description.

changeCallable[[Path], None]

Writes the change into the folder it is given.

Returns:
str

The pull request’s URL.

Raises:
CliError

If permission is below WRITE, before anything is changed.

AssertionError

If content is not set.

propose_here(root: Path, branch: str, title: str, body: str, apply: Callable[[], None]) → str[source]#

Run a change in a checkout and record the pull request.

Parameters:
rootPath

The checkout.

branchstr

The branch to propose.

titlestr

The pull request’s title.

bodystr

Its description.

applyCallable[[], None]

Makes the change in the checkout.

Returns:
str

The pull request’s URL.

Raises:
CliError

If permission is below WRITE, before anything is changed.