Provide a Git config sandbox for testing
Project description
pytest-gitconfig
Provide a Git config 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 session-scoped default_gitconfig fixture will be automatically loaded for the session
providing isolation from global user-defined values.
If you want to customize or depend on it:
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
if TYPE_CHECKING:
from pytest_gitconfig import GitConfig
@pytest.fixture(scope="session")
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 Git config file
assert str(default_gitconfig) == str(default_gitconfig.path)
return whatever
Note that the default_gitconfig fixture is Session-scoped
(avoiding the performance hit of creating a Git config file for each test),
so 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 accepts 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: GitConfig):
with gitconfig.override({"other.key": value}):
# Do something depending on those overridden values
But to test some value in some specific tests,
it's best to rely on the function-scoped gitconfig fixture providing the Git config:
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 is:
- Default to using the session-scoped
default_gitconfigto ensure isolation. - Some specific test cases relying on some specific settings set on the function-scoped
gitconfigfixture.
This has the following benefits:
- Session isolation is done only once.
- Tests having specific settings do not impact other tests.
- Tests having specific settings can be run in parallel.
Provided fixtures
Function-scoped
gitconfig -> pytest_gitconfig.GitConfig
This is the main fixture which creates a new and clean Git config file for the tested function.
It inherits 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.
set_gitconfig -> Mapping[str, str | UnsetType]
Set some git config settings for gitconfig.
Session-scoped
default_gitconfig -> pytest_gitconfig.GitConfig
This is the main fixture which creates 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.nameuser.emailinit.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 | UnsetType
Provide the initial user.name setting. By default pytest_gitconfig.DEFAULT_GIT_USER_NAME (Pytest).
Override to provide a different initial value.
default_git_user_email -> str | UnsetType
Provide the initial user.email setting. By default pytest_gitconfig.DEFAULT_GIT_USER_EMAIL (pytest@local.dev).
Override to provide a different initial value.
default_git_init_default_branch -> str | UnsetType
Provide the initial init.defaultBranch setting. By default pytest_gitconfig.DEFAULT_GIT_BRANCH (main).
Override to provide a different initial value.
set_default_gitconfig -> Mapping[str, str | UnsetType]
Set some git config settings for default_gitconfig.
sessionpatch -> pytest.MonkeyPatch
A pytest.MonkeyPatch session instance.
API
pytest_gitconfig.GitConfig
An object materializing a given Git config file.
set(self, data: Mapping[str, Any])
Write some Git config settings.
It accepts a dict of parsed data sections as (nested) dicts or dotted-key-values.
get(self, key: str, default: Any = UNSET) -> str
Get a setting given its dotted key.
Get a user-provided default value if the setting does not exist.
Raise KeyError if setting does not exists and default is not provided.
override(self, data: Mapping[str, Any]) -> Iterator[pytest_gitconfig.GitConfig]
A context manager that overrides Git config settings and restores them on exit.
Accepts the same format as the set() method.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_gitconfig-0.9.0.tar.gz.
File metadata
- Download URL: pytest_gitconfig-0.9.0.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f9e8a29b4a8e55ddd740216ddb0a8a5e97f1c9c7f6bfdac91863473a8c60f9c
|
|
| MD5 |
1e0f06f83ae978dbe13dd22a4532a779
|
|
| BLAKE2b-256 |
f5c739ad46d239bf49e6c1c8b4f6b99e75c8bee7bfc1e83cd8542d8701190508
|
Provenance
The following attestation bundles were made for pytest_gitconfig-0.9.0.tar.gz:
Publisher:
release.yml on noirbizarre/pytest-gitconfig
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_gitconfig-0.9.0.tar.gz -
Subject digest:
5f9e8a29b4a8e55ddd740216ddb0a8a5e97f1c9c7f6bfdac91863473a8c60f9c - Sigstore transparency entry: 780596769
- Sigstore integration time:
-
Permalink:
noirbizarre/pytest-gitconfig@3a411dafe394da69600ebfb8d2d2ed39d2bc1487 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/noirbizarre
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3a411dafe394da69600ebfb8d2d2ed39d2bc1487 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pytest_gitconfig-0.9.0-py3-none-any.whl.
File metadata
- Download URL: pytest_gitconfig-0.9.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d855a9dd5fb3906010dbb1f8161366d4d86b34df8c14021fa012f23de3e5354
|
|
| MD5 |
be715a45879fadc3bbd2c3aa7533e6df
|
|
| BLAKE2b-256 |
d978460084e968adb270fa1b15d3b125d3d6da25514293e05c811820a19bfc04
|
Provenance
The following attestation bundles were made for pytest_gitconfig-0.9.0-py3-none-any.whl:
Publisher:
release.yml on noirbizarre/pytest-gitconfig
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_gitconfig-0.9.0-py3-none-any.whl -
Subject digest:
3d855a9dd5fb3906010dbb1f8161366d4d86b34df8c14021fa012f23de3e5354 - Sigstore transparency entry: 780596770
- Sigstore integration time:
-
Permalink:
noirbizarre/pytest-gitconfig@3a411dafe394da69600ebfb8d2d2ed39d2bc1487 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/noirbizarre
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3a411dafe394da69600ebfb8d2d2ed39d2bc1487 -
Trigger Event:
workflow_dispatch
-
Statement type: