-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_package_creation.py
More file actions
232 lines (190 loc) · 7.49 KB
/
Copy pathtest_package_creation.py
File metadata and controls
232 lines (190 loc) · 7.49 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
"""Verify package creation using `pytest-copie`"""
import os
import subprocess
import pytest
os.environ["SKIP"] = "no-commit-to-branch"
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"
# install the `example_package` into the existing python environment.
build_results = subprocess.run(
["python", "-m", "pip", "install", "--no-deps", "."], cwd=result.project_dir, check=False
)
assert build_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
## Initialize local git repository and add ALL new files to it.
git_results = subprocess.run(["git", "init", "."], cwd=result.project_dir, check=False)
assert git_results.returncode == 0
git_results = subprocess.run(["git", "add", "."], cwd=result.project_dir, check=False)
assert git_results.returncode == 0
## This will run ALL of the relevant pre-commits (excludes only "no-commit-to-branch,check-added-large-files")
precommit_results = subprocess.run(["pre-commit", "run", "-a"], cwd=result.project_dir, check=False)
assert precommit_results.returncode == 0
return result
def test_all_defaults(copie, default_answers):
"""Test that the default values are used when no arguments are given.
Ensure that the project is created and that the basic files exist.
"""
result = create_project_with_basic_checks(copie, default_answers)
# uses ruff instead of (black/isort/pylint)
assert not (result.project_dir / "src/.pylintrc").is_file()
assert not (result.project_dir / "tests/.pylintrc").is_file()
# 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, default_answers):
"""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.
"""
extra_answers = default_answers | {
"enforce_style": ["black", "pylint", "isort"],
"create_example_module": False,
}
result = create_project_with_basic_checks(copie, extra_answers)
assert (result.project_dir / "src/.pylintrc").is_file()
assert (result.project_dir / "tests/.pylintrc").is_file()
# 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
@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, default_answers):
"""Test that various combinations of code style enforcement will
still result in a valid project being created."""
extra_answers = default_answers | {
"enforce_style": enforce_style,
}
result = create_project_with_basic_checks(copie, extra_answers)
@pytest.mark.parametrize(
"notification",
[
[],
["slack"],
["email"],
["email", "slack"],
],
)
def test_smoke_test_notification(copie, notification, default_answers):
"""Confirm we can generate a "smoke_test.yaml" file, with all
notification mechanisms selected."""
extra_answers = default_answers | {
"failure_notification": notification,
}
result = create_project_with_basic_checks(copie, extra_answers)
@pytest.mark.parametrize(
"license",
[
[],
["MIT"],
["BSD"],
["GPL3"],
["none"],
],
)
def test_license(copie, license, default_answers):
"""Confirm we get a valid project for different license options."""
extra_answers = default_answers | {"license": license}
result = create_project_with_basic_checks(copie, extra_answers)
@pytest.mark.parametrize(
"doc_answers",
[
{
"include_docs": True,
"include_notebooks": True,
},
{
"include_docs": True,
"include_notebooks": False,
},
],
)
def test_doc_combinations(copie, doc_answers, default_answers):
"""Confirm the docs directory is well-formed, when including docs."""
extra_answers = default_answers | doc_answers
result = create_project_with_basic_checks(copie, extra_answers)
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, default_answers):
"""Confirm there is no 'docs' directory, if not including docs."""
extra_answers = default_answers | doc_answers
result = create_project_with_basic_checks(copie, extra_answers)
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, default_answers):
"""Confirm we can generate a "testing_and_coverage.yaml" file, with all
test_lowest_version mechanisms selected."""
extra_answers = default_answers | {
"test_lowest_version": test_lowest_version,
}
result = create_project_with_basic_checks(copie, extra_answers)
def test_github_workflows_schema(copie, default_answers):
"""Confirm the current GitHub workflows have valid schemas."""
extra_answers = default_answers | {
"include_benchmarks": True,
"include_docs": True,
}
result = create_project_with_basic_checks(copie, extra_answers)