-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cookiecutter.py
More file actions
executable file
·503 lines (422 loc) · 16.2 KB
/
test_cookiecutter.py
File metadata and controls
executable file
·503 lines (422 loc) · 16.2 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/usr/bin/env python3
"""
Test ai-native-python
"""
import copy
import itertools
import json
import os
import platform as plat
import re
import subprocess
import sys
from pathlib import Path
import git
import pytest
import yaml
from jinja2 import Template
LOCAL_PLATFORM = f"{plat.system().lower()}/{plat.machine()}"
def get_config() -> dict:
"""Generate all the config keys"""
config_file = Path("./cookiecutter.json")
with config_file.open(encoding="utf-8") as f:
config = json.load(f)
return config
def render_config(*, config: dict) -> dict:
"""Render the provided config"""
rendered_config: dict[str, str | list] = {}
for key, value in config.items():
if isinstance(value, str):
# Sanitize by removing the "cookiecutter." prefix
sanitized_template = value.replace("cookiecutter.", "")
template = Template(sanitized_template)
rendered_config[key] = str(template.render(config))
elif isinstance(value, list):
rendered_config[key] = value
else:
sys.exit(1)
return rendered_config
def get_supported_combinations() -> list:
"""Generate all supported combinations of options"""
config = get_config()
base_config = render_config(config=config)
combinations = copy.deepcopy(base_config)
# Make every str a list[str]
for key, value in base_config.items():
if isinstance(value, str):
combinations[key] = [value]
# Return all combinations of the config
all_combinations: list[dict[str, list[str]]] = [
dict(zip(combinations, v, strict=False)) for v in itertools.product(*combinations.values())
]
# Remove unwanted keys (_copy_without_render is not currently used but may be in the future)
supported_combinations: list[dict[str, list[str]]] = [
{k: v for k, v in d.items() if k != "_copy_without_render"} for d in all_combinations
]
return supported_combinations
@pytest.fixture
def context():
"""pytest fixture for context"""
# Use the rendered defaults
return get_supported_combinations()[0]
def _fixture_id(ctx):
"""Helper to get a user friendly test name from the parametrized context."""
return ",".join(f"{key}:{value}" for key, value in ctx.items())
def build_files_list(root_dir):
"""
Build a list containing absolute paths to the generated files, ignoring
files under .git/
"""
root_path = Path(root_dir)
files = [str(file.absolute()) for file in root_path.glob("**/*") if file.is_file()]
return list(filter(lambda f: ".git/" not in f, files))
def check_files(files):
"""Method to check all files have correct substitutions."""
# Assert that no match is found in any of the files
pattern = r"{{(\s?cookiecutter)[.](.*?)}}"
re_obj = re.compile(pattern)
for file in files:
for line in open(file):
match = re_obj.search(line)
assert match is None, f"cookiecutter variable not replaced in {file}"
@pytest.mark.unit
@pytest.mark.parametrize(
"context_override",
get_supported_combinations(),
ids=_fixture_id,
)
def test_supported_options(cookies, context_override):
"""
Test all supported answer combinations
"""
# Turn off the post generation hooks
os.environ["RUN_POST_HOOK"] = "false"
result = cookies.bake(extra_context=context_override)
assert result.exit_code == 0
assert result.exception is None
assert result.project_path.name == context_override["project_name"]
assert result.project_path.is_dir()
files = build_files_list(str(result.project_path))
assert files
check_files(files)
@pytest.mark.integration
def test_update(cookies):
"""
Test task update
"""
# Turn on the post generation hooks but skip git push
os.environ["RUN_POST_HOOK"] = "true"
os.environ["SKIP_GIT_PUSH"] = "true"
result = cookies.bake()
project = result.project_path
try:
# First init the project just in case
# Clean environment to avoid VIRTUAL_ENV conflicts
env = os.environ.copy()
env.pop("VIRTUAL_ENV", None)
subprocess.run(
["task", "init"],
capture_output=True,
check=True,
cwd=project,
env=env,
)
# And then run a task update
subprocess.run(
["task", "update"],
capture_output=True,
check=True,
cwd=project,
env=env,
)
except subprocess.CalledProcessError as error:
pytest.fail(f"stdout: {error.stdout.decode('utf-8')}, stderr: {error.stderr.decode('utf-8')}")
@pytest.mark.integration
def test_autofix_hook(cookies, context):
"""
Test the post-generation pre-commit autofix hook
"""
# Turn on the post generation hooks but skip git push
os.environ["RUN_POST_HOOK"] = "true"
os.environ["SKIP_GIT_PUSH"] = "true"
# If both work, autofix is expected (but not definitively proven) to be working
for project_slug in ["aaaaaaaaaa", "zzzzzzzzzz"]:
context["project_slug"] = project_slug
result = cookies.bake(extra_context=context)
project = result.project_path
try:
# First init the project
# Clean environment to avoid VIRTUAL_ENV conflicts
env = os.environ.copy()
env.pop("VIRTUAL_ENV", None)
subprocess.run(
["task", "init"],
capture_output=True,
check=True,
cwd=project,
env=env,
)
# Run linting (which will run pre-commit with auto-fixing allowed)
# This may fail the first time if files need fixing
process = subprocess.run(
["task", "lint"],
capture_output=True,
cwd=project,
env=env,
)
# If it failed, run it again to verify fixes were applied
if process.returncode != 0:
subprocess.run(
["task", "lint"],
capture_output=True,
check=True,
cwd=project,
env=env,
)
except subprocess.CalledProcessError as error:
pytest.fail(f"stdout: {error.stdout.decode('utf-8')}, stderr: {error.stderr.decode('utf-8')}")
@pytest.mark.unit
def test_gitattributes_exists(cookies):
"""
Test that generated projects include a .gitattributes file
to enforce LF line endings for shell scripts and Dockerfiles.
"""
os.environ["RUN_POST_HOOK"] = "false"
result = cookies.bake()
assert result.exit_code == 0
assert result.exception is None
gitattributes = result.project_path / ".gitattributes"
assert gitattributes.is_file(), ".gitattributes file must exist in generated project"
content = gitattributes.read_text(encoding="utf-8")
assert "*.sh" in content, ".gitattributes must enforce line endings for shell scripts"
assert "Dockerfile" in content, ".gitattributes must enforce line endings for Dockerfiles"
@pytest.mark.unit
def test_shell_scripts_have_lf_line_endings(cookies):
"""
Test that all shell scripts in generated projects have LF line endings,
not CRLF. CRLF line endings break bash on Windows with errors like:
': invalid option namesh: line 2: set: pipefail'
"""
os.environ["RUN_POST_HOOK"] = "false"
result = cookies.bake()
assert result.exit_code == 0
assert result.exception is None
sh_files = list(result.project_path.glob("**/*.sh"))
assert sh_files, "Expected at least one .sh file in generated project"
for sh_file in sh_files:
raw_content = sh_file.read_bytes()
assert b"\r\n" not in raw_content, f"{sh_file.name} contains CRLF line endings — this breaks bash on Windows"
@pytest.mark.unit
def test_dockerfile_has_lf_line_endings(cookies):
"""
Test that the Dockerfile in generated projects has LF line endings.
CRLF line endings cause Docker build failures.
"""
os.environ["RUN_POST_HOOK"] = "false"
result = cookies.bake()
assert result.exit_code == 0
assert result.exception is None
dockerfile = result.project_path / "Dockerfile"
assert dockerfile.is_file(), "Dockerfile must exist in generated project"
raw_content = dockerfile.read_bytes()
assert b"\r\n" not in raw_content, "Dockerfile contains CRLF line endings — this breaks Docker builds"
@pytest.mark.unit
def test_no_dead_shell_scripts(cookies):
"""
Test that all shell scripts in the generated project are referenced
by at least one other file (Taskfile.yml, CI workflows, etc.).
"""
os.environ["RUN_POST_HOOK"] = "false"
result = cookies.bake()
assert result.exit_code == 0
assert result.exception is None
sh_files = list(result.project_path.glob("scripts/*.sh"))
assert sh_files, "Expected at least one .sh file in generated project"
# Collect all non-.sh file content to search for references
all_content = ""
for f in result.project_path.rglob("*"):
if f.is_file() and f.suffix != ".sh" and ".git/" not in str(f):
try:
all_content += f.read_text(encoding="utf-8", errors="ignore")
except (IsADirectoryError, PermissionError):
pass
for sh_file in sh_files:
assert sh_file.name in all_content, (
f"scripts/{sh_file.name} is dead code — not referenced by any other file in the project"
)
@pytest.mark.unit
@pytest.mark.parametrize(
"invalid_name",
[
"123invalid", # starts with number
"_invalid", # starts with underscore
"-invalid", # starts with dash
"9project", # starts with number
"!invalid", # starts with special character
"My Project", # contains space
"has space", # contains space
],
)
def test_invalid_project_name_validation(cookies, invalid_name):
"""
Test that project names with invalid characters are rejected
"""
result = cookies.bake(extra_context={"project_name": invalid_name})
# The generation should fail due to validation
assert result.exit_code != 0, f"Expected validation failure for project name: {invalid_name}"
@pytest.mark.unit
@pytest.mark.parametrize(
"valid_name",
[
"ValidProject", # starts with uppercase
"validproject", # starts with lowercase
"My-Project", # starts with uppercase, has hyphen
"a1234", # starts with lowercase, has numbers
"Z_project", # starts with uppercase, has underscore
],
)
def test_valid_project_name_validation(cookies, valid_name):
"""
Test that valid project names starting with alphabetical characters are accepted
"""
# Turn off the post generation hooks for faster testing
os.environ["RUN_POST_HOOK"] = "false"
result = cookies.bake(extra_context={"project_name": valid_name})
# The generation should succeed
assert result.exit_code == 0, f"Expected validation success for project name: {valid_name}"
assert result.exception is None
assert result.project_path.is_dir()
@pytest.mark.integration
@pytest.mark.slow
def test_default_project(cookies):
"""
Test a default project thoroughly
"""
# Turn on the post generation hooks but skip git push
os.environ["RUN_POST_HOOK"] = "true"
os.environ["SKIP_GIT_PUSH"] = "true"
result = cookies.bake()
project = result.project_path
repo = git.Repo(project)
if repo.is_dirty(untracked_files=True):
pytest.fail("Something went wrong with the project's post-generation hook")
# Extract project_name and project_slug from cookiecutter.json
config_file = Path("./cookiecutter.json")
with config_file.open(encoding="utf-8") as f:
generated_config = json.load(f)
github_org = generated_config.get("github_org")
project_name = generated_config.get("project_name")
project_name_lower = project_name.lower()
# Keep this logic aligned with the template's README.md
# It's important that this has -s in the name to test the docker hub image name sanitization
default_image_name = f"{github_org}/{project_name_lower}"
default_image_name_and_tag = f"{default_image_name}:latest"
try:
env = os.environ.copy()
env.pop("VIRTUAL_ENV", None) # Clean VIRTUAL_ENV to avoid conflicts
# Bootstrap the project and run the simplest checks first to optimize for a fast feedback loop
subprocess.run(
[
"task",
"-v",
"init",
"lint",
"validate",
],
capture_output=True,
check=True,
cwd=project,
env=env,
)
# Ensure the project.yml is generated, and is valid YAML
config_path = project / ".github" / "project.yml"
with config_path.open(encoding="utf-8") as yaml_data:
project_context = yaml.safe_load(yaml_data)
assert project_context["origin"]["generated"]
# Build and test each supported architecture individually
for platform in ("linux/arm64", "linux/amd64"):
env["PLATFORM"] = platform
subprocess.run(
["task", "-v", "build", "test", "sbom", "vulnscan", "license-check"],
capture_output=True,
check=True,
cwd=project,
env=env,
)
# Do two releases to ensure they work; do not push the releases though
for _ in range(2):
subprocess.run(
["task", "-v", "release", "--", "--no-push", "--no-commit", "--no-tag", "--no-vcs-release"],
capture_output=True,
check=True,
cwd=project,
env=env,
)
# Ensure that --help exits 0
subprocess.run(
["docker", "run", "--rm", default_image_name_and_tag, "--help"],
capture_output=True,
check=True,
cwd=project,
)
# Ensure that --debug --verbose (mutually exclusive arguments) exits 2
command: list[str] = [
"docker",
"run",
"--rm",
default_image_name_and_tag,
"--debug",
"--verbose",
]
expected_exit: int = 2
process = subprocess.run(
command,
capture_output=True,
cwd=project,
)
if process.returncode != expected_exit:
pytest.fail(
f"Unexpected exit code when running {command}; expected {expected_exit}, received {process.returncode}"
)
# Clean the repo, perform a multi-platform build, and then run the sbom / vulnscan / license check tasks to ensure it all works
# We cannot functionally test a multi-platform image without pushing it to a registry and then pulling it back down because they can't directly be
# loaded into the docker daemon
env["PLATFORM"] = "all"
subprocess.run(
[
"task",
"-v",
"clean",
"build",
"sbom",
"vulnscan",
"license-check",
],
capture_output=True,
check=True,
cwd=project,
env=env,
)
except subprocess.CalledProcessError as error:
print(f"\n=== STDOUT ===\n{error.stdout.decode('utf-8')}")
print(f"\n=== STDERR ===\n{error.stderr.decode('utf-8')}")
pytest.fail(f"Command failed with exit code {error.returncode}. See output above.")
except (
yaml.YAMLError,
FileNotFoundError,
PermissionError,
IsADirectoryError,
OSError,
) as exception:
pytest.fail(exception)
# Validate CI
for filename in ["ci.yml"]:
file_path = project / ".github" / "workflows" / filename
with file_path.open(encoding="utf-8") as file:
try:
github_config = yaml.safe_load(file)
# Ensure that the expected jobs exist
for job in ["build", "test", "lint"]:
assert job in github_config["jobs"]
except yaml.YAMLError as exception:
pytest.fail(exception)