forked from pyOpenSci/pyos-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
104 lines (78 loc) · 2.84 KB
/
conftest.py
File metadata and controls
104 lines (78 loc) · 2.84 KB
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
95
96
97
98
99
100
101
102
103
104
"""Fixtures used in our test suite for the pyOpenSci Python package
template."""
import shutil
from pathlib import Path
from typing import TYPE_CHECKING, Generator
import pytest
from _pytest.monkeypatch import MonkeyPatch
from jinja2 import Environment, FileSystemLoader
from ruamel.yaml import YAML
if TYPE_CHECKING:
from _pytest.config.argparsing import Parser
from _pytest.tmpdir import TempPathFactory
COPIER_CONFIG_PATH = Path(__file__).parents[1] / "copier.yml"
INCLUDES_PATH = Path(__file__).parents[1] / "includes"
def _load_copier_config() -> dict:
yaml = YAML(typ="safe")
with COPIER_CONFIG_PATH.open("r") as yfile:
return yaml.load(yfile)
# don't mutate or else i'll have to get out the spray bottle (make it a fixture)
COPIER_CONFIG = _load_copier_config()
# --------------------------------------------------
# pytest hooks
# --------------------------------------------------
def pytest_addoption(parser: "Parser") -> None:
"""Add options to pytest."""
parser.addoption(
"--reuse-envs",
action="store_true",
help="After tests run, don't remove hatch environments created for "
"generated projects (not the test environments for the "
"pyos-package-template project itself).\n"
"otherwise, use a temporary directoy and remove it afterwards.",
)
# --------------------------------------------------
# Fixtures - autouse
# --------------------------------------------------
@pytest.fixture(scope="session", autouse=True)
def cleanup_hatch_envs(
pytestconfig: pytest.Config,
tmp_path_factory: "TempPathFactory",
monkeypatch_session: MonkeyPatch,
) -> None:
"""
Use a temporary directory for hatch envs & cleanup after session.
Unless --reuse-envs flag present.
"""
if pytestconfig.getoption("--reuse-envs"):
yield
return
hatch_dir = tmp_path_factory.mktemp("hatch")
monkeypatch_session.setenv("HATCH_DATA_DIR", str(hatch_dir))
try:
yield
finally:
shutil.rmtree(hatch_dir, ignore_errors=True)
# ---------------------------------------------
# Fixtures - exports
# ---------------------------------------------
@pytest.fixture(scope="session")
def monkeypatch_session() -> Generator[MonkeyPatch, None, None]:
"""Monkeypatch you can use with a session scoped fixture."""
mpatch = MonkeyPatch()
yield mpatch
mpatch.undo()
@pytest.fixture(scope="module")
def includes() -> Environment:
"""Jinja environment loaded with the partials dir."""
return Environment(
loader=FileSystemLoader(searchpath=str(INCLUDES_PATH.resolve())),
autoescape=True,
)
@pytest.fixture(
scope="module",
params=COPIER_CONFIG["license"]["choices"].values(),
)
def license(request: pytest.FixtureRequest) -> str:
"""Provide a recognized license classification."""
return request.param