-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_package_generation.py
More file actions
180 lines (150 loc) · 5.34 KB
/
test_package_generation.py
File metadata and controls
180 lines (150 loc) · 5.34 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""Checks that the cookiecutter works."""
import difflib
import os
import pathlib
import shutil
import subprocess
import typing
import pytest
import pytest_venv # type: ignore[import-not-found]
def get_all_files_folders(root_path: pathlib.Path) -> set[pathlib.Path]:
"""
Get all files and folders under a directory.
The paths are returned relative to the root path given.
__pycache__ directories and .DS_Store files are ignored.
"""
file_set: set[pathlib.Path] = set()
for dirpath, _, filenames in os.walk(root_path):
dirpath_path = pathlib.Path(dirpath).relative_to(root_path)
if dirpath_path.name in ["__pycache__"]:
continue
# Add this directory
file_set.update((dirpath_path,))
# Add any files in it
for filename in filenames:
if filename in [".DS_Store"]:
continue
file_set.update((dirpath_path / filename,))
return file_set
def test_package_generation(
default_config_with: typing.Callable,
generate_package: typing.Callable,
) -> None:
"""Test package generation."""
# Not having a git repo makes it easier to check in/out reference
# data files to the main python-tooling git repository
config = default_config_with(initialise_git_repository="False")
_, test_project_dir = generate_package(config=config)
expected_package_dir = (
pathlib.Path(__file__).parent / "data" / "test_package_generation"
)
assert test_project_dir.exists(), "Project directory does not exist."
actual_files = get_all_files_folders(test_project_dir)
expected_files = get_all_files_folders(expected_package_dir)
assert actual_files == expected_files
# Check diff between actual and expected file contents
diff = ""
for file in actual_files:
actual_file = test_project_dir / file
expected_file = expected_package_dir / file
if actual_file.is_dir():
continue
with actual_file.open() as f1, expected_file.open() as f2:
diff += "".join(
difflib.unified_diff(
f1.readlines(),
f2.readlines(),
fromfile=str(actual_file),
tofile=str(expected_file),
),
)
if diff:
shutil.rmtree(expected_package_dir)
shutil.move(test_project_dir, expected_package_dir)
msg = (
"Non-zero diff between generated files and expected files.\n"
"Test data files have been modified with new content.\n"
"Diff is:\n"
f"{diff}"
)
raise RuntimeError(msg)
def test_pip_installable(
venv: pytest_venv.VirtualEnvironment,
generate_package: typing.Callable,
) -> None:
"""Test generated package is pip installable."""
_, test_project_dir = generate_package()
# Try to install package in virtual environment with pip
pipinstall = subprocess.run( # noqa: S603
[
venv.python,
"-m",
"pip",
"install",
"-e",
test_project_dir,
],
capture_output=True,
check=False,
)
assert pipinstall.returncode == 0, (
f"Something went wrong with installation: {pipinstall.stderr!r}"
)
@pytest.mark.parametrize("funder", ["", "STFC", "UKRI", "Wellcome Trust"])
def test_optional_funder(
funder: str,
default_config_with: typing.Callable,
generate_package: typing.Callable,
) -> None:
"""Test specifying funder or not in package generation."""
config = default_config_with(funder=funder)
_, test_project_dir = generate_package(config)
with (test_project_dir / "README.md").open() as f:
readme_text = "".join(f.readlines())
if funder == "":
assert "## Acknowledgements" not in readme_text
else:
assert (
f"## Acknowledgements\n\nThis work was funded by {funder}." in readme_text
), readme_text
def test_docs_build(
venv: pytest_venv.VirtualEnvironment,
generate_package: typing.Callable,
) -> None:
"""Test documentation build from package created from template."""
_, test_project_dir = generate_package()
venv.install("tox")
tox_docs_process = subprocess.run( # noqa: S603
[
pathlib.Path(venv.bin) / "tox",
"-e",
"docs",
],
cwd=test_project_dir,
capture_output=True,
check=False,
)
assert tox_docs_process.returncode == 0, (
f"Something went wrong with building docs: {tox_docs_process.stderr!r}"
)
def test_package_tests_tox(
venv: pytest_venv.VirtualEnvironment,
generate_package: typing.Callable,
) -> None:
"""
Test that the package tests pass in all tox environments.
...and that no warnings are raised (e.g. coverage).
"""
_, test_project_dir = generate_package()
venv.install("tox")
tox_multienv_test_process = subprocess.run( # noqa: S603
[pathlib.Path(venv.bin) / "tox"],
check=False,
cwd=test_project_dir,
capture_output=True,
)
tests_pass = tox_multienv_test_process.returncode == 0
assert tests_pass, "Template tests failed in one or more tox environments."
return
output = tox_multienv_test_process.stdout.decode()
assert "WARNING:" not in output, f"Warnings raised during tests: {output}"