跳转到主要内容

提供Git配置沙箱进行测试

项目描述

pytest-gitconfig

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

提供Git配置沙箱进行测试

入门

安装 pytest-gitconfig

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

然后,会话范围的 default_gitconfig fixture 将自动加载,为会话提供与全局用户定义值的隔离。

如果您想自定义或依赖它

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

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 Git config file
    assert str(default_gitconfig) == str(default_gitconfig.path)
    return whatever

请注意,default_gitconfig fixture 是会话范围的(避免为每个测试创建Git配置文件的性能影响),所以设置的值在整个会话中是持久的,应该只定义一次,最好在您的 conftest.py 中定义。但如果你需要临时覆盖某些值,你可以使用接受与 set() 相同参数的 override() 上下文管理器。

这允许在测试期间直接覆盖它

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

但要测试某些特定测试中的某些值,最好依赖于函数范围的 gitconfig fixture,它提供Git配置

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

一个经典的设置是

  • 默认使用会话范围的 default_gitconfig 以确保隔离。
  • 一些特定的测试用例依赖于函数范围的 gitconfig fixture 上设置的一些特定设置。

这有以下好处

  • 会话隔离只进行一次。
  • 具有特定设置的测试不会影响其他测试。
  • 具有特定设置的测试可以并行运行。

提供的fixture

函数范围

gitconfig -> pytest_gitconfig.GitConfig

这是主要的fixture,它为测试函数创建一个新的干净的Git配置文件。

它继承自 default_gitconfig(意味着在 default_gitconfig 上设置的值都将设置在 gitconfig 上)。

它通过猴子补丁(monkeypatching)GIT_CONFIG_GLOBAL 环境变量来实现。因此,如果你在一个忽略 os.environ 的上下文中依赖它,你应该使用此修复程序自己修补它。

git_user_name -> str | None

gitconfig 提供初始的 user.name 设置。如果为 None,则 user.name 将继承其值来自 default_config,因此如果没有覆盖,则很可能来自 default_git_user_name

git_user_email -> str | None

gitconfig 提供初始的 user.email 设置。如果为 None,则 user.email 将继承其值来自 default_config,因此如果没有覆盖,则很可能来自 default_git_user_email

git_init_default_branch -> str | None

gitconfig 提供初始的 init.defaultBranch 设置。如果为 None,则 init.defaultBranch 将继承其值来自 default_config,因此如果没有覆盖,则很可能来自 default_git_init_default_branch

会话作用域

default_gitconfig -> pytest_gitconfig.GitConfig

这是主要的修复程序,它为测试会话创建一个新的干净 Git 配置文件。如果你已安装 pytest-gitconfig,它将自动加载。

默认情况下,它将设置 3 个设置

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

它通过猴子补丁(monkeypatching)GIT_CONFIG_GLOBAL 环境变量来实现。因此,如果你在一个忽略 os.environ 的上下文中依赖它,你应该使用此修复程序自己修补它。

default_git_user_name -> str | UnsetType

提供初始的 user.name 设置。默认为 pytest_gitconfig.DEFAULT_GIT_USER_NAMEPytest)。通过覆盖提供不同的初始值。

default_git_user_email -> str | UnsetType

提供初始的 user.email 设置。默认为 pytest_gitconfig.DEFAULT_GIT_USER_EMAILpytest@local.dev)。通过覆盖提供不同的初始值。

default_git_init_default_branch -> str | UnsetType

提供初始的 init.defaultBranch 设置。默认为 pytest_gitconfig.DEFAULT_GIT_BRANCHmain)。通过覆盖提供不同的初始值。

sessionpatch -> pytest.MonkeyPatch

一个 pytest.MonkeyPatch 会话实例。

API

pytest_gitconfig.GitConfig

一个表示给定 Git 配置文件的对象。

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

写入一些 Git 配置设置。它接受解析后的数据部分的字典作为(嵌套)字典或点分隔键值。

get(self, key: str, default: Any = UNSET) -> str

根据点分隔键获取设置。如果设置不存在,则获取用户提供的默认值。如果设置不存在且未提供默认值,则抛出 KeyError

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

一个上下文管理器,它覆盖 Git 配置设置并在退出时恢复它们。接受与 set() 方法相同的格式。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。

源分发

pytest_gitconfig-0.7.0.tar.gz (9.0 kB 查看哈希

上传时间

构建分发

pytest_gitconfig-0.7.0-py3-none-any.whl (6.6 kB 查看散列值)

上传时间: Python 3

由以下支持