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 the default_gitconfig session fixture will be automatically loaded for the session providing isolation from gloval user defined values.

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 default_git_user_name() -> str:
  return "John Doe"

@pytest.fixture(scope="session", autouse=True)
def fixture_depending_on_default_gitconfig(default_gitconfig: GitConfig) -> Whatever:
    # You can set values, the following statements are equivalents
    default_gitconfig.set({"some": {key: value}}) # nested dicts form
    default_gitconfig.set({"some.key": value})    # dict with dotted keys form
    # Or read them
    data = default_gitconfig.get("some.key")
    data = default_gitconfig.get("some.key", "fallback")
    # If you need the path to the gitconfig file
    assert str(default_gitconfig) == str(default_gitconfig.path)
    return whatever

Note that the default_gitconfig fixture being session-scoped (avoiding the performance hit of creating a gitconfig file for each test), set values are persistent for the whole session and should be defined once preferably in your conftest.py. But if you need to temporarily override some value, you can use the override() context manager which is accepting the same parameters as set().

This allows to override it directly during a test:

from __future__ import annotations
from typing import TYPE_CHECKING, Iterator

if TYPE_CHECKING:
  from pytest_gitconfig import GitConfig


def test_something(default_gitconfig):
    with gitconfig.override({"other.key": value}):
        # Do something depending on those overridden values

But to test some value in some specific tests, its best to rely on gitconfig which is providing a function-scoped GitConfig:

from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
  from pytest_gitconfig import GitConfig


def test_something(gitconfig: GitConfig):
    gitconfig.set({"other.key": value})  # Only valid for this test
    # Do something depending on those overridden values

A classical setup being:

  • default to using the session-scoped default_gitconfig to ensure isolation.
  • some specific test cases relying on some specific settings set on the gitconfig function-scoped fixture

This has the following benefits:

  • session isolation is done only once
  • test having specific settings does not impact other tests
  • test having specific settings can be run in parallel

Provided fixtures

Function-scoped

gitconfig -> pytest_gitconfig.GitConfig

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

It inherit from default_gitconfig (meaning that all values set on default_gitconfig will be set on gitconfig)

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 | None

Provide the initial user.name setting for gitconfig. If None, user.name will inherit its value from default_config, so most probably from default_git_user_name if not overridden.

git_user_email -> str | None

Provide the initial user.email setting for gitconfig. If None, user.email will inherit its value from default_config, so most probably from default_git_user_email if not overridden.

git_init_default_branch -> str | None

Provide the initial init.defaultBranch setting for gitconfig. If None, init.defaultBranch will inherit its value from default_config, so most probably default_git_init_default_branch if not overridden.

Session-scoped

defalt_gitconfig -> pytest_gitconfig.GitConfig

This is the main fixture which is creating a new and clean git config file for the test session. It is loaded automatically if you have pytest-gitconfig installed.

By default, it will set 3 settings:

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

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.

default_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.

default_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.

default_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.

API

pytest_gitconfig.GitConfig

An object materializing a given gitconfig file.

set(self, data: Mapping[str, Any])

Write some gitconfig values. It accepts a dict of parsed data sections as dict or dotted-key-values.

get(self, key: str, default: Any = _UNSET)

Get a setting given its dotted key. Get a 2nd default value. Raise KeyError if config does not exists and default is not provided

override(self, data: Mapping[str, Any]) -> Iterator[GitConfig]

A context manager setting the values and restoring them on exit. Accept the same format a the set() method.

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.6.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

pytest_gitconfig-0.6.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_gitconfig-0.6.0.tar.gz
  • Upload date:
  • Size: 8.9 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.6.0.tar.gz
Algorithm Hash digest
SHA256 ee06963e24c5a79f1b4bbcf86e071d54a147508a1a1a19269316531df4382cb5
MD5 811046898906fce004e4fcd5220b63d9
BLAKE2b-256 ef41f87c98b4596cf1160649e3d511fd968c470ea1b1fb1240a89611b3d3f7ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_gitconfig-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d1e9aadd4bca9fdd13a9940191d3d2487c42b87bd325c5eebf2f36f4919d55f
MD5 38793015e221e96274a106470dfeb335
BLAKE2b-256 3d2b3f21412dea350b5241947d4c6c1ada69031e7b590b6422eb1ad3110fd7e8

See more details on using hashes here.

Supported by

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