-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_package_creation.py
More file actions
310 lines (247 loc) · 9.17 KB
/
Copy pathtest_package_creation.py
File metadata and controls
310 lines (247 loc) · 9.17 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Verify package creation using `pytest-copie`"""
import subprocess
import pytest
def create_project_with_basic_checks(copie, extra_answers, package_name="example_package"):
"""Create the project using copier. Perform a handful of basic checks on the created directory."""
# run copier to hydrate a temporary project
result = copie.copy(extra_answers=extra_answers)
## Initializes local git repository (required to run pre-commit)
subprocess.call(["git", "init", "."], cwd=result.project_dir)
# successfully_created_project
assert (
result.exit_code == 0 and result.exception is None and result.project_dir.is_dir()
), "Did not successfully create project"
# pyproject_toml_is_valid
precommit_results = subprocess.run(
["pre-commit", "run", "validate-pyproject"], cwd=result.project_dir, check=False
)
assert precommit_results.returncode == 0
# directory_structure_is_correct
assert (result.project_dir / f"src/{package_name}").is_dir() and (
result.project_dir / f"tests/{package_name}"
).is_dir(), "Directory structure is incorrect"
# contains_required_files
required_files = [
".copier-answers.yml",
".git_archival.txt",
".gitattributes",
".gitignore",
".pre-commit-config.yaml",
".setup_dev.sh",
"LICENSE",
"pyproject.toml",
"README.md",
]
all_found = True
for file in required_files:
if not (result.project_dir / file).is_file():
all_found = False
print("Required file not generated:", file)
assert all_found
return result
def black_runs_successfully(result):
"""Test to ensure that the black linter runs successfully on the project"""
# run black with `--check` to look for lint errors, but don't fix them.
black_results = subprocess.run(
["python", "-m", "black", "--check", (result.project_dir / "src")],
cwd=result.project_dir,
check=False,
)
return black_results.returncode == 0
def pylint_runs_successfully(result):
"""Test to ensure that the pylint linter runs successfully on the project"""
# run pylint to ensure that the hydrated files are linted correctly
pylint_results = subprocess.run(
[
"python",
"-m",
"pylint",
"--recursive=y",
"--rcfile=./src/.pylintrc",
(result.project_dir / "src"),
],
cwd=result.project_dir,
check=False,
)
return pylint_results.returncode == 0
def docs_build_successfully(result):
"""Test that we can build the doc tree.
!!! NOTE - This doesn't currently work because we need to `pip install` the hydrated
project before running the tests. And we don't have a way to create a temporary
virtual environment for the project.
"""
required_files = [
".readthedocs.yml",
]
all_found = True
for file in required_files:
if not (result.project_dir / file).is_file():
all_found = False
print("Required file not generated:", file)
return all_found
# sphinx_results = subprocess.run(
# ["make", "html"],
# cwd=(result.project_dir / "docs"),
# )
# return sphinx_results.returncode == 0
def github_workflows_are_valid(result):
"""Test to ensure that the GitHub workflows are valid"""
workflows_results = subprocess.run(
["pre-commit", "run", "check-github-workflows"], cwd=result.project_dir, check=False
)
return workflows_results.returncode == 0
def test_all_defaults(copie):
"""Test that the default values are used when no arguments are given.
Ensure that the project is created and that the basic files exist.
"""
# run copier to hydrate a temporary project
result = create_project_with_basic_checks(copie, {})
assert not pylint_runs_successfully(result)
# check to see if the README file was hydrated with copier answers.
found_line = False
with open(result.project_dir / "README.md", encoding="utf-8") as f:
for line in f:
if "example_project" in line:
found_line = True
break
assert found_line
def test_use_black_and_no_example_modules(copie):
"""We want to provide non-default arguments for the linter and example modules
copier questions and ensure that the pyproject.toml file is created with Black
and that no example modules are created.
"""
# provide a dictionary of the non-default answers to use
extra_answers = {
"enforce_style": ["black", "pylint", "isort"],
"create_example_module": False,
}
result = create_project_with_basic_checks(copie, extra_answers)
assert pylint_runs_successfully(result)
# make sure that the files that were not requested were not created
assert not (result.project_dir / "src/example_package/example_module.py").is_file()
# check to see if the pyproject.toml file has the expected dependencies
found_line = False
with open(result.project_dir / "pyproject.toml", encoding="utf-8") as f:
for line in f:
if '"black", # Used for static linting of files' in line:
found_line = True
break
assert found_line
assert black_runs_successfully(result)
@pytest.mark.parametrize(
"enforce_style",
[
[],
["ruff_lint"],
["ruff_format"],
["ruff_lint", "pylint"],
["ruff_format", "black"],
["black", "pylint", "isort"],
["ruff_lint", "ruff_format"],
["black", "pylint", "isort", "ruff_lint", "ruff_format"],
],
)
def test_code_style_combinations(copie, enforce_style):
"""Test that various combinations of code style enforcement will
still result in a valid project being created."""
# provide a dictionary of the non-default answers to use
extra_answers = {
"enforce_style": enforce_style,
}
result = create_project_with_basic_checks(copie, extra_answers)
# black would still run successfully.
assert black_runs_successfully(result)
@pytest.mark.parametrize(
"notification",
[
[],
["slack"],
["email"],
["email", "slack"],
],
)
def test_smoke_test_notification(copie, notification):
"""Confirm we can generate a "smoke_test.yaml" file, with all
notification mechanisms selected."""
# provide a dictionary of the non-default answers to use
extra_answers = {
"failure_notification": notification,
}
# run copier to hydrate a temporary project
result = create_project_with_basic_checks(copie, extra_answers)
assert black_runs_successfully(result)
@pytest.mark.parametrize(
"license",
[
[],
["MIT"],
["BSD"],
["GPL3"],
["none"],
],
)
def test_license(copie, license):
"""Confirm we get a valid project for different license options."""
# provide a dictionary of the non-default answers to use
extra_answers = {"license": license}
# run copier to hydrate a temporary project
result = create_project_with_basic_checks(copie, extra_answers)
assert black_runs_successfully(result)
@pytest.mark.parametrize(
"doc_answers",
[
{
"include_docs": True,
"include_notebooks": True,
},
{
"include_docs": True,
"include_notebooks": False,
},
],
)
def test_doc_combinations(copie, doc_answers):
"""Confirm the docs directory is well-formed, when including docs."""
# run copier to hydrate a temporary project
result = create_project_with_basic_checks(copie, doc_answers)
assert black_runs_successfully(result)
assert docs_build_successfully(result)
assert (result.project_dir / "docs").is_dir()
@pytest.mark.parametrize(
"doc_answers",
[
{
"include_docs": False,
"include_notebooks": False,
},
{
"include_docs": False,
"include_notebooks": True,
},
],
)
def test_doc_combinations_no_docs(copie, doc_answers):
"""Confirm there is no 'docs' directory, if not including docs."""
# run copier to hydrate a temporary project
result = create_project_with_basic_checks(copie, doc_answers)
assert black_runs_successfully(result)
assert not (result.project_dir / "docs").is_dir()
@pytest.mark.parametrize("test_lowest_version", ["none", "direct", "all"])
def test_test_lowest_version(copie, test_lowest_version):
"""Confirm we can generate a "testing_and_coverage.yaml" file, with all
test_lowest_version mechanisms selected."""
# provide a dictionary of the non-default answers to use
extra_answers = {
"test_lowest_version": test_lowest_version,
}
# run copier to hydrate a temporary project
result = create_project_with_basic_checks(copie, extra_answers)
assert black_runs_successfully(result)
def test_github_workflows_schema(copie):
"""Confirm the current GitHub workflows have valid schemas."""
extra_answers = {
"include_benchmarks": True,
"include_docs": True,
}
result = create_project_with_basic_checks(copie, extra_answers)
assert github_workflows_are_valid(result)