Skip to main content

Provide a gitconfig sandbox for testing

Project description

pytest-gitconfig

CI pre-commit.ci status PyPI PyPI - License codecov

Provide a gitconfig sandbox for testing

Getting started

Install pytest-gitconfig:

# pip
pip install pytest-gitconfig
# pipenv
pipenv install pytest-gitconfig
# PDM
pdm add pytest-gitconfig

Then in your conftest.py:

# All tests are using the sandboxed gitconfig
pytestmark = pytest.mark.usefixtures("gitconfig")

or if you want to customize or depend on it

from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
  from pytest_gitconfig import GitConfig

@pytest.fixture
def git_user_name() -> str:
  return "John Doe"

@pytest.fixture
def fixture_depending_on_gitconfig(gitconfig: GitConfig) -> Whatever:
    # You can set values, the 4 following statements are equivalents
    gitconfig.set({"some": {key: value}}) # nested dicts form
    gitconfig.set(some={key: value})      # dicts as kwargs form
    gitconfig.set({"some.key": value})    # dict with dotted keys form
    gitconfig.set(**{"some.key": value})  # kwargs with dotted keys form
    # Or read them
    data = gitconfig.get("some.key")
    # If you need the path to the gitconfig file
    GIT_CONFIG_GLOBAL = str(gitconfig)
    return whatever

Provided fixtures

All fixtures are session-scoped.

gitconfig -> pytest_gitconfig.GitConfig

This is the main fixture which is creating a new and clean git config file for the test session.

By default, it will set 3 settings:

  • user.name
  • user.email
  • init.defaultBranch

The fixture when required provide a pytest_gitconfig.GitConfig object with the following methods:

  • gitconfig.set() accepting either a dict or kwargs, as parsed data sections as dict or dotted-key-values.
  • gitconfig.get() to get a setting given its dotted key.

It works by monkeypatching the GIT_CONFIG_GLOBAL environment variable. So, if you rely on this in a context where os.environ is ignored, you should patch it yourself using this fixture.

git_user_name -> str

Provide the initial user.name setting. By default pytest_gitconfig.DEFAULT_GIT_USER_NAME. Override to provide a different initial value.

git_user_email -> str

Provide the initial user.email setting. By default pytest_gitconfig.DEFAULT_GIT_USER_EMAIL. Override to provide a different initial value.

git_init_default_branch -> str

Provide the initial init.defaultBranch setting. By default pytest_gitconfig.DEFAULT_GIT_BRANCH (main). Override to provide a different initial value.

sessionpatch -> pytest.MonkeyPatch

A pytest.MonkeyPatch session instance.

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_gitconfig-0.5.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

pytest_gitconfig-0.5.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest_gitconfig-0.5.0.tar.gz.

File metadata

  • Download URL: pytest_gitconfig-0.5.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.9.3 CPython/3.10.12

File hashes

Hashes for pytest_gitconfig-0.5.0.tar.gz
Algorithm Hash digest
SHA256 439de276076354f4cbbf5f0e2eaff2d0f0fe491043cded634cb29f7376c6cc9a
MD5 aaee24063970267e3d794352b85db6fd
BLAKE2b-256 fdc4f8ad8202ddf52a8fa0f6439e912d5bf1861263e3b83e6fea278cba4024a2

See more details on using hashes here.

File details

Details for the file pytest_gitconfig-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_gitconfig-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da8eb084ce05af106ff4de800c5ff95c5e13f9bf466425aa5e5cdf82aa0cf3c9
MD5 3085a5b405435ef8f77e297d5735787f
BLAKE2b-256 80b6f0c2f1f1f734249e897ace0822613fa822366147f48525825c42411ddd4f

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