-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_template.py
More file actions
185 lines (163 loc) Β· 5.51 KB
/
test_template.py
File metadata and controls
185 lines (163 loc) Β· 5.51 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
import datetime
import os
import subprocess
from typing import Any
import pytest
import yaml
from .readme_utils import ReadmeCases
TEMPLATE_ONLY_DATA = "cookiecutter_template_data"
def _bake(cookies: Any, extra_context: dict[str, Any]) -> Any:
result = cookies.bake(extra_context=extra_context)
assert not result.exception
assert result.exit_code == 0
assert result.project_path.name == "test-baked-cookie"
assert result.project_path.is_dir()
assert os.path.isdir(
os.path.join(result.project_path, "test_baked_cookie")
)
return result
@pytest.mark.parametrize(
"project_license",
[
"GPL-3.0-or-later",
"GPL-3.0-only",
"LGPL-3.0-or-later",
"LGPL-3.0-only",
"BSD-3-Clause",
"MIT",
],
)
def test_project_license(cookies: Any, project_license: str) -> None:
result = _bake(
cookies,
extra_context=dict(
author_name="Ness",
author_email="pk-fire@onett.example.com",
project_name="test-baked-cookie",
project_description=(
'This is a test project called "test-baked-cookie"'
),
project_license=project_license,
),
)
print(result.project_path)
print(os.listdir(result.project_path))
with open(os.path.join(result.project_path, "LICENSE")) as f:
license_data = f.read()
with open(
os.path.join(
"{{cookiecutter.project_name}}",
TEMPLATE_ONLY_DATA,
"licenses",
project_license,
)
) as f:
expected_license_data = f.read()
if project_license in ("MIT", "BSD-3-Clause"):
# Compare copyright line separately
if project_license == "MIT":
expected_copyright = "Copyright (c) {} Ness".format(
datetime.date.today().year
)
elif project_license == "BSD-3-Clause":
expected_copyright = "Copyright (c) {}, Ness".format(
datetime.date.today().year
)
assert license_data.splitlines()[0] == expected_copyright
license_data = os.linesep.join(license_data.split(os.linesep)[1:])
expected_license_data = os.linesep.join(
expected_license_data.split(os.linesep)[1:]
)
if project_license == "BSD-3-Clause":
expected_license_data = expected_license_data.replace(
"{{ cookiecutter.project_name }}", "test-baked-cookie"
)
assert license_data == expected_license_data
@pytest.mark.parametrize(
"enable_coverage", [True, False], ids=["coverage", "no coverage"]
)
@pytest.mark.parametrize(
"enable_pypi_publish", [True, False], ids=["pypi", "no pypi"]
)
@pytest.mark.parametrize(
"enable_container_publish",
[True, False],
ids=["container", "no container"],
)
def test_rendered_project(
cookies: Any,
enable_coverage: bool,
enable_pypi_publish: bool,
enable_container_publish: bool,
) -> None:
extra_context = dict(
author_name="Ness",
author_email="pk-fire@onett.example.com",
project_name="test-baked-cookie",
project_description=(
'This is a test project called "test-baked-cookie"'
),
github_user="ness",
)
if not enable_coverage:
extra_context["enable_coverage"] = "no"
if enable_pypi_publish:
extra_context["enable_pypi_publish"] = "yes"
result = _bake(cookies, extra_context=extra_context)
# Validate CI/CD configuration
with open(
os.path.join(result.project_path, ".github", "workflows", "ci.yml")
) as f:
ci_data = yaml.safe_load(f.read())
with open(
os.path.join(result.project_path, ".github", "workflows", "cd.yml")
) as f:
cd_data = yaml.safe_load(f.read())
assert ci_data["env"]["ENABLE_COVERAGE"] == enable_coverage
assert cd_data["env"]["ENABLE_PYPI_PUBLISH"] == enable_pypi_publish
assert cd_data["env"]["ENABLE_TEST_PYPI_PUBLISH"] is False
assert cd_data["jobs"]["Publish"]["environment"] == {
"name": "pypi",
"url": "https://pypi.org/p/test-baked-cookie",
}
assert not (
subprocess.check_output(
["git", "status", "--porcelain=v1"], cwd=result.project_path
)
.decode("utf-8")
.strip()
), "Untracked files present in template-rendered project"
@pytest.mark.parametrize(**ReadmeCases.all_cases().__dict__)
def test_rendered_readme(
cookies: Any,
github_user: str,
enable_coverage: bool,
enable_pypi_publish: bool,
enable_container_publish: bool,
expected_content_file: str,
opt_update_expected_outputs: bool,
) -> None:
result = _bake(
cookies,
extra_context=dict(
author_name="Ness",
author_email="pk-fire@onett.example.com",
project_name="test-baked-cookie",
project_description=(
'This is a test project called "test-baked-cookie"'
),
github_user=github_user,
enable_coverage=("yes" if enable_coverage else "no"),
enable_pypi_publish=("yes" if enable_pypi_publish else "no"),
enable_container_publish=(
"yes" if enable_container_publish else "no"
),
),
)
with open(os.path.join(result.project_path, "README.md")) as f:
readme = f.read()
if opt_update_expected_outputs:
with open(expected_content_file, "w") as f:
f.write(readme)
with open(expected_content_file) as f:
assert readme == f.read()