Skip to main content

Pytest fixtures for testing with git.

Project description

pytest-git-fixtures

Overview

Pytest fixtures to dynamically create GIT repositories for testing.

Getting Started

Update setup.py to include:

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

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

import logging
import subprocess
import pytest
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 "Initial commit." 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 "Initial commit." 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.

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

gitrepo

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:

  • fork - Path to the "fork" git-dir.
  • homedir - Path to the home directory used to initialize the repo.
  • gitconfig - Path to the fully instantiated git configuration, created from gitconfig
  • gnupg - The GnuPG keypair used to sign commits, from gnupg_keypair
  • clone_git_dir - Path to the "clone" git-dir.
  • clone_work_tree - Path to the "clone" work tree.
  • upstream - Path to the "upstream" git-dir.
  • work_tree - Path to the separate "work tree" work tree.

Typing is provided by pytest_git_fixtures.GITRepo.

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_PATH_CLONE, $GIT_PATH_FORK, $GIT_PATH_UPSTREAM, $GIT_PATH_WORK_TREE, GNUPGHOME, GNUPG_PASSPHRASE, and HOME environment variables will be populated during invocation of the script.

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 packages provides a gpg-wrapper script that can be used in conjuction with the git gpg.program configuration value as follows:
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={**environment, **{"GNUPG_PASSPHRASE": git_repo.gnupg_keypair.passphrase}},
    )

Changelog

0.1.0 (2021-01-11)

  • Initial release.

Development

Source Control

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_git_fixtures-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

pytest_git_fixtures-0.1.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest_git_fixtures-0.1.0.tar.gz.

File metadata

  • Download URL: pytest_git_fixtures-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.3

File hashes

Hashes for pytest_git_fixtures-0.1.0.tar.gz
Algorithm Hash digest
SHA256 41a83505bb0e113dd1aefddebaeef75a582e7233aef3258cac9c344ac66e5b34
MD5 2bb9e7913b37d78cecc5e34a008bcabf
BLAKE2b-256 59a1287eff363db412a8275a9ce6655c96f5718fc1055cc099ede177bf4356df

See more details on using hashes here.

File details

Details for the file pytest_git_fixtures-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_git_fixtures-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.3

File hashes

Hashes for pytest_git_fixtures-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca39ab5757349ce26552b7081782dbe4c7d44f6fc70af0b776e6f24359b0c44d
MD5 55330628ffbddf444eace6e738ab60be
BLAKE2b-256 31a7270a60c564d37ce5dd70781f4abe4645473e7b95510798a6e9d14c8594bf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page