Skip to content

livery.git._pytest_isolation

livery.git._pytest_isolation

Every pytest with livery-git installed gives its git a config of the session's own.

Registered through the livery-git-isolation pytest11 entry point, so every session in an environment with livery-git carries it. Before the first test, every test's git reads GIT_CONFIG as its global config, from a fresh directory in the system's temporary folder: one identity, so a repository a test creates commits without a config of its own, and none of the desk's settings, a signer among them. A GIT_CONFIG_GLOBAL the outer environment already set is kept, since an outer redirect is a redirect. The identity variables git reads ahead of every config leave the session's environment, so a colleague's clone a test configures commits as the colleague. Every repository a test creates or clones starts from an empty template directory beside that config, so its .git holds none of git's sample hooks. The session's end removes the directory and the variable it set; a session that is killed leaves the directory in the temporary folder.

-p no:livery-git-isolation turns the plugin off for one session.

Functions:

Name Description
session_config

Return the text of the session's global git config.

pytest_configure

Drop the identity variables, and point git at the session's own config.

pytest_unconfigure

Remove the session's config, and the variable that named it.

Attributes:

Name Type Description
GIT_CONFIG
IDENTITY_VARIABLES
TEMPLATES

GIT_CONFIG module-attribute

GIT_CONFIG = '[user]\n\tname = tester\n\temail = tester@example.invalid\n'

IDENTITY_VARIABLES module-attribute

IDENTITY_VARIABLES = ('GIT_AUTHOR_NAME', 'GIT_AUTHOR_EMAIL', 'GIT_COMMITTER_NAME', 'GIT_COMMITTER_EMAIL')

TEMPLATES module-attribute

TEMPLATES = 'templates'

session_config

session_config(templates: PurePath) -> str

Return the text of the session's global git config.

The config is GIT_CONFIG, and templates as git's template directory. The path is written with forward slashes, which git reads on every platform, since a backslash in a config value is an escape.

Parameters:

Name Type Description Default
templates PurePath

The empty directory new repositories start from.

required

Returns:

Type Description
str

The config's text.

Source code in packages/git/src/livery/git/_pytest_isolation.py
def session_config(templates: PurePath) -> str:
    """Return the text of the session's global git config.

    The config is `GIT_CONFIG`, and *templates* as git's template
    directory. The path is written with forward slashes, which git
    reads on every platform, since a backslash in a config value is
    an escape.

    Args:
        templates: The empty directory new repositories start from.

    Returns:
        The config's text.
    """
    return f'{GIT_CONFIG}[init]\n\ttemplateDir = "{templates.as_posix()}"\n'

pytest_configure

pytest_configure(config: Config) -> None

Drop the identity variables, and point git at the session's own config.

Source code in packages/git/src/livery/git/_pytest_isolation.py
def pytest_configure(config: pytest.Config) -> None:
    """Drop the identity variables, and point git at the session's own config."""
    for name in IDENTITY_VARIABLES:
        os.environ.pop(name, None)
    if "GIT_CONFIG_GLOBAL" in os.environ:
        return
    home = Path(tempfile.mkdtemp(prefix="livery-git-isolation-"))
    templates = home / TEMPLATES
    templates.mkdir()
    gitconfig = home / "gitconfig"
    gitconfig.write_text(session_config(templates), "utf-8")
    os.environ["GIT_CONFIG_GLOBAL"] = str(gitconfig)
    config.stash[_HOME] = str(home)

pytest_unconfigure

pytest_unconfigure(config: Config) -> None

Remove the session's config, and the variable that named it.

Source code in packages/git/src/livery/git/_pytest_isolation.py
def pytest_unconfigure(config: pytest.Config) -> None:
    """Remove the session's config, and the variable that named it."""
    home = config.stash.get(_HOME, "")
    if home:
        os.environ.pop("GIT_CONFIG_GLOBAL", None)
        shutil.rmtree(home, ignore_errors=True)