Skip to main content

pytest-git-fixtures

Overview

Pytest fixtures to dynamically create GIT repositories for testing.

Getting Started

Update setup.py to include:

from distutils.core import setup

setup(
	tests_require=["pytest-git-fixtures"]
)

All fixtures should be automatically included via the pytest11 entry point.

import logging
import subprocess
from pytest_git_fixtures import GITRepo  # Optional, for typing

LOGGER = logging.getLogger(__name__)

def test_sanity_check(git_repo: GITRepo):
    environment = {
        "GNUPGHOME": git_repo.gnupg_keypair.gnupg_home,
        "HOME": str(git_repo.homedir),
    }
    completed_process = subprocess.run(
        ["git", "log", "--show-signature"],
        capture_output=True,
        check=True,
        cwd=str(git_repo.clone_work_tree),
        env=environment,
    )
    stdout = completed_process.stdout.decode("utf-8")
    LOGGER.debug(stdout)
    assert "Good signature from" in stdout
    assert git_repo.commit_message in stdout

    completed_process = subprocess.run(
        ["git", "log", "--show-signature"],
        capture_output=True,
        check=True,
        cwd=str(git_repo.work_tree),
        env={**environment, **{"GIT_DIR": str(git_repo.upstream)}},
    )
    stdout = completed_process.stdout.decode("utf-8")
    LOGGER.debug(stdout)
    assert "Good signature from" in stdout
    assert git_repo.commit_message in stdout
  • Tested with python 3.8

Installation

From pypi.org

$ pip install pytest_git_fixtures

From source code

$ git clone https://github.com/crashvb/pytest-git-fixtures
$ cd pytest-git-fixtures
$ virtualenv env
$ source env/bin/activate
$ python -m pip install --editable .[dev]

Fixtures

gitconfig

Provides the path to a templated GIT configuration file that is used to initialize the repository. If a user-defined script (tests/gitconfig) can be located, it is used. Otherwise, an embedded configuration template is copied to temporary location and returned. This fixture is used by the git_repo fixture.

TheGIT_USER_EMAIL, GIT_USER_NAME, and GIT_SIGNINGKEY variables will be populated within the template during generation of the repository.

git_commit_message

Provides the commit message of the initial commit to the repository. This fixture is used by the git_repo fixture.

git_init_script

Provides the path to a GIT initialization script that is used to create repository structure. If a user-defined script (tests/git-init.sh) can be located, it is used. Otherwise, an embedded script is copied to temporary location and returned. This fixture is used by the git_repo fixture.

The GIT_COMMIT_MESSAGE, GIT_INITIAL_BRANCH_NAME, GIT_PATH_CLONE, GIT_PATH_FORK, GIT_PATH_UPSTREAM, GIT_PATH_WORK_TREE, GIT_REMOTE_NAME_FORK, GIT_REMOTE_NAME_UPSTREAM, GIT_TEST_FILENAME, GIT_WORK_TREE_BRANCH_NAME, GNUPGHOME, GNUPG_PASSPHRASE, and HOME environment variables will be populated during invocation of the script.

git_initial_branch_name.

Provides the name of the initial branch to use. This fixture is used by the git_repo fixture.

git_remote_name_fork

Provides the name of the remote used to reference the fork repository. This fixture is used by the git_repo fixture.

git_remote_name_upstream

Provides the name of the remote used to reference the upstream repository. This fixture is used by the git_repo fixture.

git_repo

Initializes a temporary GIT repository with a bare upstream, fork, and separate work tree.

NamedTuple Fields

The following fields are defined in the tuple provided by this fixture:

  • clone_git_dir - Path to the "clone" git-dir.
  • clone_work_tree - Path to the "clone" work tree.
  • commit_message - Commit message of the initial commit, created from git_commit_message
  • fork - Path to the "fork" git-dir.
  • gitconfig - Path to the fully instantiated git configuration, created from gitconfig
  • gnupg - The GnuPG keypair used to sign commits, from gnupg_keypair
  • homedir - Path to the home directory used to initialize the repo.
  • initial_branch - Name of the initial branch, created from git_initial_branch_name
  • remote_fork - Name of the remote for the "fork", created from git_remote_name_fork
  • remote_upstream - Name of the remote for the "upstream", created from git_remote_name_upstream
  • test_filename - Name of the test file, created from git_test_filename
  • upstream - Path to the "upstream" git-dir.
  • work_tree - Path to the separate "work tree" work tree.
  • work_tree_branch - Name of the "work tree" branch, created from git_work_tree_branch_name
  • work_tree_git_dir - Path to the "work tree" git-dir.

Typing is provided by pytest_git_fixtures.GITRepo.

git_test_filename

Provides the name of the test file committed into the repository. This fixture is used by the git_repo fixture.

git_work_tree_branch_name

Provides the name of the branch used to create the git work tree. This fixture is used by the git_repo fixture.

Limitations

  1. This has been coded to work with git-scm >= 2.6.
  2. The generated repository is very simple. TBD if this will be expanded to support a more realistic configuration.
  3. The embedded GIT configuration is configured to sign commits and tags by default. This can cause complications with externally configured instances of GnuPG, unless they are configured to use loopback for pinentry, or you like testing with interactive passphrase entry ;) . It is recommended that pytest-gnupg-fixtures be used. This package provides a gpg-wrapper script that can be used in conjunction with the git gpg.program configuration value as follows:
import subprocess
from pytest_git_fixtures import GITRepo  # Optional, for typing
def test_something_with_gnupg(git_repo: GITRepo):
    subprocess.run(
        [
            "git",
            "-c",
            f"gpg.program={git_repo.gnupg_keypair.gnupg_home}/gpg-wrapper",
            "commit",
            "--message",
            "Look Ma, no hands!",
        ],
        check=True,
        cwd=str(git_repo.clone_work_tree),
        env={"GNUPG_PASSPHRASE": git_repo.gnupg_keypair.passphrase},
    )

Environment Variables

Variable Default Value Description
PGITF_DEBUG Adds additional debug logging.

Development

Source Control

Metadata

Release files for pytest-git-fixtures 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-git-fixtures 0.1.7
File Size Uploaded
pytest_git_fixtures-0.1.7.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-git-fixtures 0.1.7
File Interpreter ABI Platform
pytest_git_fixtures-0.1.7-py3-none-any.whl Python 3 none any Details

Total release size: 22.9 kB

Release files / pytest_git_fixtures-0.1.7.tar.gz

Download URL pytest_git_fixtures-0.1.7.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6217aaecf54eeb2567c45720147d4b6493e32b12a798adcdcc8d880ac59c5a14
BLAKE2b-256 checksum
How to use checksums
bd08d2a0461b481cb42244ad66123a26296a0334faaf5349f8e53e1b8419d570
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.3

Release files / pytest_git_fixtures-0.1.7-py3-none-any.whl

Download URL pytest_git_fixtures-0.1.7-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
39f3279c54f6c4af9c25792aa313502a6421f03c1872a107f029d16f61b26839
BLAKE2b-256 checksum
How to use checksums
2e0fdd8df2c2c97fda71490eaa466a6ad278ec419727da53fe3316174d2f107a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page