-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bake_project.py
707 lines (578 loc) · 24 KB
/
test_bake_project.py
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
"""cookiecutter-py3-package test suite."""
import datetime
import importlib
import os
import shlex
import subprocess # nosec
import sys
from contextlib import contextmanager
import pytest
from click.testing import CliRunner
from cookiecutter.utils import rmtree
def post_gen_setup(*args, supress_exception=False, cwd=None):
"""Helper to set up the package with the chosen options"""
cur_dir = os.getcwd()
try:
if cwd:
os.chdir(cwd)
with subprocess.Popen( # nosec
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE
) as proc:
out, err = proc.communicate()
out = out.decode("utf-8")
err = err.decode("utf-8")
if err and not supress_exception:
raise Exception(err)
if err and supress_exception:
return out
return out
# return None # This return fixes pylint R1710 see
# https://pycodequ.al/docs/pylint-messages/r1710-inconsistent-return-statements.html
finally:
os.chdir(cur_dir)
@contextmanager
def inside_dir(dirpath):
"""
Execute code from inside the given directory.
:param dirpath: String, path of the directory the command is being run.
"""
old_path = os.getcwd()
try:
os.chdir(dirpath)
yield
finally:
os.chdir(old_path)
@contextmanager
def bake_in_temp_dir(cookies, *args, **kwargs):
"""
Delete the temporal directory that is created when executing the tests.
:param cookies: pytest_cookies.Cookies,
cookie to be baked and its temporal files will be removed
"""
result = cookies.bake(*args, **kwargs)
try:
yield result
finally:
rmtree(str(result.project))
def run_inside_dir(command, dirpath):
"""
Run a command from inside a given directory, returning the exit status.
:param command: Command that will be executed
:param dirpath: String, path of the directory the command is being run.
"""
with inside_dir(dirpath):
return subprocess.check_call(shlex.split(command))
def check_output_inside_dir(command, dirpath):
"""
Run a command from inside a given directory, returning the command output.
"""
with inside_dir(dirpath):
return subprocess.check_output(shlex.split(command))
def test_year_compute_in_license_file(cookies):
"""
Check year exists in package license file.
"""
with bake_in_temp_dir(cookies) as result:
license_file_path = result.project.join("LICENSE")
now = datetime.datetime.now()
assert str(now.year) in license_file_path.read()
def project_info(result):
"""
Get toplevel dir, project_slug, and project dir from baked cookies.
"""
project_path = str(result.project)
project_slug = os.path.split(project_path)[-1]
project_dir = os.path.join(project_path, project_slug)
return project_path, project_slug, project_dir
def test_bake_with_defaults(cookies):
"""
Test cookiecutter created the package with default settings.
"""
with bake_in_temp_dir(cookies) as result:
assert result.project.isdir()
assert result.exit_code == 0
assert result.exception is None
found_toplevel_files = [f.basename for f in result.project.listdir()]
assert "setup.py" in found_toplevel_files
assert "python_3_package_boilerplate" in found_toplevel_files
assert "tox.ini" in found_toplevel_files
assert "tests" in found_toplevel_files
assert "CHANGELOG.md" in found_toplevel_files
assert "LICENSE" in found_toplevel_files
assert "AUTHORS.rst" in found_toplevel_files
assert "History.rst" not in found_toplevel_files
assert ".pre-commit-config.yaml" in found_toplevel_files
found_git_workflows = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
assert "semantic_release.yaml" in found_git_workflows
assert "test_contribution.yaml" in found_git_workflows
assert "update-changelog.yaml" not in found_git_workflows
found_git_templates = [
f.basename for f in result.project.join(".github/ISSUE_TEMPLATE").listdir()
]
assert "bug-report.md" in found_git_templates
assert "chore.md" in found_git_templates
assert "documentation-request.md" in found_git_templates
assert "feature-request.md" in found_git_templates
found_git_files = [f.basename for f in result.project.join(".github").listdir()]
assert ".git-commit-template.txt" in found_git_files
assert "semantic.yaml" in found_git_files
assert "ISSUE_TEMPLATE.md" not in found_git_files
def test_bake_and_run_tests(cookies):
"""
Test the package is set up correctly as a directory.
.. note::
A reminder if anything stops working. These lines are commented out due to
W0106: Expression "run_inside_dir('python setup.py test',
str(result.project)) == 0" is assigned to nothing
(expression-not-assigned) pre-commit pylint error.
"""
with bake_in_temp_dir(cookies) as result:
assert result.project.isdir()
# run_inside_dir('python setup.py test', str(result.project)) == 3
# print("test_bake_and_run_tests path", str(result.project))
def test_bake_with_apostrophe_and_run_tests(cookies):
"""
Ensure that a `full_name` with apostrophes does not break setup.py.
.. note::
A reminder if anything stops working. This line is commented out due to
W0106: Expression "run_inside_dir('python setup.py test',
str(result.project)) == 0" is assigned to nothing
(expression-not-assigned) pre-commit pylint error.
"""
with bake_in_temp_dir(
cookies,
extra_context={"full_name": "O'connor"},
) as result:
assert result.project.isdir()
# run_inside_dir("python setup.py test", str(result.project)) == 0
def test_bake_without_author_file(cookies):
"""
Test cookiecutter created the package without an author file.
"""
with bake_in_temp_dir(
cookies,
extra_context={"create_author_file": "n"},
) as result:
found_toplevel_files = [f.basename for f in result.project.listdir()]
assert "AUTHORS.rst" not in found_toplevel_files
doc_files = [f.basename for f in result.project.join("docs").listdir()]
assert "authors.rst" not in doc_files
# Assert there are no spaces in the toc tree
docs_index_path = result.project.join("docs/index.rst")
with open(str(docs_index_path)) as index_file:
assert "contributing\n history" in index_file.read()
# Check that
manifest_path = result.project.join("MANIFEST.in")
with open(str(manifest_path)) as manifest_file:
assert "AUTHORS.rst" not in manifest_file.read()
def test_make_help(cookies):
"""
Test make help.
"""
with bake_in_temp_dir(cookies) as result:
# The supplied Makefile does not support win32
if sys.platform != "win32":
output = check_output_inside_dir("make help", str(result.project))
assert b"check code coverage quickly with the default Python" in output
def test_bake_selecting_license(cookies):
"""
Test cookiecutter created the package with the correct license.
"""
license_strings = {
"MIT license": "MIT ",
"BSD license": "Redistributions of source code must retain the "
+ "above copyright notice, this",
"ISC license": "ISC License",
"Apache Software License 2.0": "Licensed under the Apache License, Version 2.0",
"GNU General Public License v3": "GNU GENERAL PUBLIC LICENSE",
}
for license, target_string in license_strings.items():
with bake_in_temp_dir(
cookies, extra_context={"open_source_license": license}
) as result:
assert target_string in result.project.join("LICENSE").read()
assert license in result.project.join("setup.py").read()
def test_bake_not_open_source(cookies):
"""
Test cookiecutter created the package as not open source.
"""
with bake_in_temp_dir(
cookies, extra_context={"open_source_license": "Not open source"}
) as result:
found_toplevel_files = [f.basename for f in result.project.listdir()]
assert "setup.py" in found_toplevel_files
assert "LICENSE" not in found_toplevel_files
assert "License" not in result.project.join("README.rst").read()
def test_using_pytest(cookies):
"""
Test cookiecutter created the package using pytest.
.. note::
A reminder if anything stops working. This line is commented out due to
W0106: Expression "run_inside_dir('python setup.py test',
str(result.project)) == 0" is assigned to nothing
(expression-not-assigned) pre-commit pylint error.
"""
with bake_in_temp_dir(
cookies,
extra_context={"use_pytest": "y"},
) as result:
assert result.project.isdir()
test_file_path = result.project.join(
"tests/test_python_3_package_boilerplate.py"
)
lines = test_file_path.readlines()
assert "import pytest" in "".join(lines)
# Test the new pytest target
# run_inside_dir('pytest', str(result.project)) == 0
def test_not_using_pytest(cookies):
"""
Test cookiecutter created the package without using pytest.
"""
with bake_in_temp_dir(cookies) as result:
assert result.project.isdir()
test_file_path = result.project.join(
"tests/test_python_3_package_boilerplate.py"
)
lines = test_file_path.readlines()
assert "import unittest" in "".join(lines)
assert "import pytest" not in "".join(lines)
def test_bake_with_no_console_script(cookies):
"""
Test cookiecutter created the package without console script files.
"""
context = {"command_line_interface": "No command-line interface"}
result = cookies.bake(extra_context=context)
project_path, project_slug, project_dir = project_info(result)
found_project_files = os.listdir(project_dir)
assert "cli.py" not in found_project_files
setup_path = os.path.join(project_path, "setup.py")
with open(setup_path, "r") as setup_file:
assert "entry_points" not in setup_file.read()
def test_bake_with_console_script_files(cookies):
"""
Test cookiecutter created the package with console script files.
"""
context = {"command_line_interface": "click"}
result = cookies.bake(extra_context=context)
project_path, project_slug, project_dir = project_info(result)
found_project_files = os.listdir(project_dir)
assert "cli.py" in found_project_files
setup_path = os.path.join(project_path, "setup.py")
with open(setup_path, "r") as setup_file:
assert "entry_points" in setup_file.read()
def test_bake_with_argparse_console_script_files(cookies):
"""
Test cookiecutter created the package with argparse console script files.
"""
context = {"command_line_interface": "argparse"}
result = cookies.bake(extra_context=context)
project_path, project_slug, project_dir = project_info(result)
found_project_files = os.listdir(project_dir)
assert "cli.py" in found_project_files
setup_path = os.path.join(project_path, "setup.py")
with open(setup_path, "r") as setup_file:
assert "entry_points" in setup_file.read()
def test_bake_with_console_script_cli(cookies):
"""
Test cookiecutter created the package with console script cli files.
"""
context = {"command_line_interface": "click"}
result = cookies.bake(extra_context=context)
project_path, project_slug, project_dir = project_info(result)
module_path = os.path.join(project_dir, "cli.py")
module_name = ".".join([project_slug, "cli"])
spec = importlib.util.spec_from_file_location(module_name, module_path)
cli = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cli)
runner = CliRunner()
noarg_result = runner.invoke(cli.main)
assert noarg_result.exit_code == 0
noarg_output = " ".join(
["Replace this message by putting your code into", project_slug]
)
assert noarg_output in noarg_result.output
help_result = runner.invoke(cli.main, ["--help"])
assert help_result.exit_code == 0
assert "Show this message" in help_result.output
def test_bake_with_argparse_console_script_cli(cookies):
"""
Test cookiecutter created the package with argparse console script files.
"""
context = {"command_line_interface": "argparse"}
result = cookies.bake(extra_context=context)
project_path, project_slug, project_dir = project_info(result)
module_path = os.path.join(project_dir, "cli.py")
module_name = ".".join([project_slug, "cli"])
spec = importlib.util.spec_from_file_location(module_name, module_path)
cli = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cli)
runner = CliRunner()
noarg_result = runner.invoke(cli.main)
assert noarg_result.exit_code == 0
noarg_output = " ".join(
["Replace this message by putting your code into", project_slug]
)
assert noarg_output in noarg_result.output
help_result = runner.invoke(cli.main, ["--help"])
assert help_result.exit_code == 0
assert "Show this message" in help_result.output
@pytest.mark.parametrize("use_black,expected", [("y", True), ("n", False)])
def test_black(cookies, use_black, expected):
"""
Test cookiecutter created the package with black configured.
"""
with bake_in_temp_dir(
cookies,
extra_context={"use_black": use_black},
) as result:
assert result.project.isdir()
requirements_path = result.project.join("requirements_dev.txt")
assert ("black" in requirements_path.read()) is expected
makefile_path = result.project.join("Makefile")
assert ("black --check" in makefile_path.read()) is expected
def test_bake_with_conventional_commits_message(cookies):
"""
Test cookiecutter created the package with a conventional commits message.
"""
with bake_in_temp_dir(
cookies,
extra_context={"create_conventional_commits_edit_message": "y"},
) as result:
git_with_files = [f.basename for f in result.project.join(".github").listdir()]
assert ".git-commit-template.txt" in git_with_files
def test_bake_with_git_config_use_conventional_commits_message(cookies):
"""
Test cookiecutter created the package with git config conventional commits.
"""
with bake_in_temp_dir(
cookies,
extra_context={"create_conventional_commits_edit_message": "y"},
) as result:
assert "commit.template=.github/.git-commit-template.txt" in post_gen_setup(
"git", "config", "--local", "--list", cwd=str(result.project)
)
def test_bake_without_conventional_commits_message(cookies):
"""
Test cookiecutter created the package without conventional commits message.
"""
with bake_in_temp_dir(
cookies, extra_context={"create_conventional_commits_edit_message": "n"}
) as result:
git_without_files = [
f.basename for f in result.project.join(".github").listdir()
]
assert ".git-commit-template.txt" not in git_without_files
def test_bake_with_repo_automatic_testing_suite(cookies):
"""
Test cookiecutter created the package with repo automatic testing.
"""
with bake_in_temp_dir(
cookies, extra_context={"create_repo_auto_test_workflow": "y"}
) as result:
test_workflow_with_files = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
assert "test_contribution.yaml" in test_workflow_with_files
def test_bake_without_repo_automatic_testing_suite(cookies):
"""
Test cookiecutter created the package without repo automatic testing.
"""
with bake_in_temp_dir(
cookies, extra_context={"create_repo_auto_test_workflow": "n"}
) as result:
test_workflow_without_files = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
assert "test_contribution.yaml" not in test_workflow_without_files
def test_bake_with_automatic_CHANGELOG(cookies):
"""
Test cookiecutter created the package with a auto changelog generation.
"""
with bake_in_temp_dir(
cookies, extra_context={"create_auto_CHANGELOG": "y"}
) as result:
change_log_with_files = [f.basename for f in result.project.listdir()]
assert "CHANGELOG.md" in change_log_with_files
assert "HISTORY.rst" not in change_log_with_files
auto_workflow_with_files = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
assert "update-changelog.yaml" in auto_workflow_with_files
def test_bake_without_automatic_CHANGELOG(cookies):
"""
Test cookiecutter created the package without auto changelog generation.
"""
with bake_in_temp_dir(
cookies, extra_context={"create_auto_CHANGELOG": "n"}
) as result:
auto_workflow_without_files = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
assert "update-changelog.yaml" not in auto_workflow_without_files
def test_bake_with_auto_semantic_version(cookies):
"""
Test cookiecutter created the package with auto semantic versioning.
"""
with bake_in_temp_dir(
cookies, extra_context={"use_GH_action_semantic_version": "y"}
) as result:
sem_ver_with_files = [
f.basename for f in result.project.join(".github").listdir()
]
assert "semantic.yaml" in sem_ver_with_files
sem_ver_workflow_with_files = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
assert "semantic_release.yaml" in sem_ver_workflow_with_files
def test_bake_without_auto_semantic_version(cookies):
"""
Test cookiecutter created the package without auto semantic versioning.
"""
with bake_in_temp_dir(
cookies, extra_context={"use_GH_action_semantic_version": "n"}
) as result:
sem_ver_without_files = [
f.basename for f in result.project.join(".github").listdir()
]
assert "semantic.yaml" not in sem_ver_without_files
sem_ver_workflow_without_files = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
assert "semantic_release.yaml" not in sem_ver_workflow_without_files
def test_bake_without_automatic_CHANGELOG_and_semantic_version(cookies):
"""
Test cookiecutter created the package without Changelog & Semantic Version.
"""
with bake_in_temp_dir(
cookies,
extra_context={
"create_auto_CHANGELOG": "n",
"use_GH_action_semantic_version": "n",
},
) as result:
auto_changelog_semantic_without_files = [
f.basename for f in result.project.listdir()
]
assert "CHANGELOG.md" not in auto_changelog_semantic_without_files
def test_bake_with_custom_issue_templates(cookies):
"""
Test cookiecutter created the package with custom issue templates.
"""
with bake_in_temp_dir(
cookies, extra_context={"use_GH_custom_issue_templates": "y"}
) as result:
templates_with_files = [
f.basename for f in result.project.join(".github/ISSUE_TEMPLATE").listdir()
]
assert "bug-report.md" in templates_with_files
assert "chore.md" in templates_with_files
assert "documentation-request.md" in templates_with_files
assert "feature-request.md" in templates_with_files
# Test the standard template has been removed.
standard_issue_template = [
f.basename for f in result.project.join(".github/").listdir()
]
assert "ISSUE_TEMPLATE.md" not in standard_issue_template
def test_bake_without_custom_issue_templates(cookies):
"""
Test cookiecutter created the package without custom issue templates.
"""
with bake_in_temp_dir(
cookies, extra_context={"use_GH_custom_issue_templates": "n"}
) as result:
templates_without_files = [
f.basename for f in result.project.join(".github/ISSUE_TEMPLATE").listdir()
]
assert "bug-report.md" not in templates_without_files
assert "chore.md" not in templates_without_files
assert "documentation-request.md" not in templates_without_files
assert "feature-request.md" not in templates_without_files
# Test the standard template has been removed.
standard_issue_template = [
f.basename for f in result.project.join(".github/").listdir()
]
assert "ISSUE_TEMPLATE.md" in standard_issue_template
def test_bake_with_pre_commit(cookies):
"""
Test cookiecutter created the package with pre-commit.
"""
with bake_in_temp_dir(cookies, extra_context={"use_pre_commit": "y"}) as result:
pre_commit_with_files = [f.basename for f in result.project.listdir()]
assert ".pre-commit-config.yaml" in pre_commit_with_files
def test_bake_without_pre_commit(cookies):
"""
Test cookiecutter created the package without pre-commit.
"""
with bake_in_temp_dir(cookies, extra_context={"use_pre_commit": "n"}) as result:
pre_commit_without_files = [f.basename for f in result.project.listdir()]
assert ".pre-commit-config.yaml" not in pre_commit_without_files
def test_bake_with_git_init_success(cookies):
"""
Test cookiecutter created the package with git init successfull.
"""
with bake_in_temp_dir(
cookies,
extra_context={"automatic_set_up_git_and_initial_commit": "y"},
) as result:
found_toplevel_files = [f.basename for f in result.project.listdir()]
assert ".git" in found_toplevel_files
assert "origin" in post_gen_setup(
"git", "remote", "-v", cwd=str(result.project)
)
assert "git@github.com:" in post_gen_setup(
"git", "remote", "-v", cwd=str(result.project)
)
assert "(fetch)" in post_gen_setup(
"git", "remote", "-v", cwd=str(result.project)
)
assert "(push)" in post_gen_setup(
"git", "remote", "-v", cwd=str(result.project)
)
def test_bake_without_git_init_success(cookies):
"""
Test cookiecutter created the package without git init.
"""
with bake_in_temp_dir(
cookies,
extra_context={"automatic_set_up_git_and_initial_commit": "n"},
) as result:
found_toplevel_files = [f.basename for f in result.project.listdir()]
assert ".git" not in found_toplevel_files
def test_bake_with_git_add_commit_success(cookies):
"""
Test cookiecutter created the package with git add and commit successfull.
"""
with bake_in_temp_dir(
cookies,
extra_context={"automatic_set_up_git_and_initial_commit": "y"},
) as result:
assert "chore(git): Initial Commit" in post_gen_setup(
"git", "reflog", cwd=str(result.project)
)
assert "HEAD@{0}: commit (initial):" in post_gen_setup(
"git", "reflog", cwd=str(result.project)
)
def test_bake_with_release_test_pypi(cookies):
"""
Test cookiecutter created the package with release to test pypi.
"""
with bake_in_temp_dir(
cookies, extra_context={"use_release_to_test_pypi_with_tags": "y"}
) as result:
test_pypi_with_files = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
assert "semantic_release_test_pypi.yaml" in test_pypi_with_files
def test_bake_without_release_test_pypi(cookies):
"""
Test cookiecutter created the package without release to test pypi.
"""
with bake_in_temp_dir(
cookies, extra_context={"use_release_to_test_pypi_with_tags": "n"}
) as result:
test_pypi_without_files = [
f.basename for f in result.project.join(".github/workflows").listdir()
]
assert "semantic_release_test_pypi.yaml" not in test_pypi_without_files