-
Notifications
You must be signed in to change notification settings - Fork 13
/
conftest.py
94 lines (71 loc) · 2.5 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Conftest.py (root-level).
We keep this in root pytest fixtures in pytest's doctest plugin to be available, as well
as avoiding conftest.py from being included in the wheel, in addition to pytest_plugin
for pytester only being available via the root directory.
See "pytest_plugins in non-top-level conftest files" in
https://docs.pytest.org/en/stable/deprecations.html
"""
import pathlib
import shutil
import typing as t
import pytest
@pytest.fixture(autouse=True)
def add_doctest_fixtures(
request: pytest.FixtureRequest,
doctest_namespace: dict[str, t.Any],
) -> None:
"""Harness pytest fixtures to doctests namespace."""
from _pytest.doctest import DoctestItem
if isinstance(request._pyfuncitem, DoctestItem):
request.getfixturevalue("add_doctest_fixtures")
request.getfixturevalue("set_home")
@pytest.fixture(autouse=True)
def setup(
request: pytest.FixtureRequest,
gitconfig: pathlib.Path,
set_home: pathlib.Path,
xdg_config_path: pathlib.Path,
) -> None:
"""Automatically load the pytest fixtures in the parameters."""
@pytest.fixture(autouse=True)
def cwd_default(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path) -> None:
"""Change the current directory to a temporary directory."""
monkeypatch.chdir(tmp_path)
@pytest.fixture(autouse=True)
def xdg_config_path(
user_path: pathlib.Path,
set_home: pathlib.Path,
) -> pathlib.Path:
"""Create and return path to use for XDG Config Path."""
p = user_path / ".config"
if not p.exists():
p.mkdir()
return p
@pytest.fixture
def config_path(
xdg_config_path: pathlib.Path,
request: pytest.FixtureRequest,
) -> pathlib.Path:
"""Ensure and return vcspull configuration path."""
conf_path = xdg_config_path / "vcspull"
conf_path.mkdir(exist_ok=True)
def clean() -> None:
shutil.rmtree(conf_path)
request.addfinalizer(clean)
return conf_path
@pytest.fixture(autouse=True)
def set_xdg_config_path(
monkeypatch: pytest.MonkeyPatch,
xdg_config_path: pathlib.Path,
) -> None:
"""Set XDG_CONFIG_HOME environment variable."""
monkeypatch.setenv("XDG_CONFIG_HOME", str(xdg_config_path))
@pytest.fixture
def repos_path(user_path: pathlib.Path, request: pytest.FixtureRequest) -> pathlib.Path:
"""Return temporary directory for repository checkout guaranteed unique."""
path = user_path / "repos"
path.mkdir(exist_ok=True)
def clean() -> None:
shutil.rmtree(path)
request.addfinalizer(clean)
return path