Skip to content

Commit 2e4f5e5

Browse files
authored
Merge pull request #245 from adafruit/test-cookie-pylint-clean
Add test that the resulting repo is pre-commit clean
2 parents 7c2638f + 55f6d04 commit 2e4f5e5

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

tests/test_make_cookies.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import json
66
import os
77
import pathlib
8-
import shutil
8+
import subprocess
99
import sys
1010

1111
from cookiecutter.main import cookiecutter
@@ -22,14 +22,14 @@
2222
with open(cookie_json_path.resolve(), 'r') as cookie_file:
2323
cookie_json = json.load(cookie_file)
2424

25-
def overwrite_workaround():
26-
""" cookiecutter's `overwrite_if_exists=True` is currently broken.
27-
There is an accepted PR slated for release 1.8.0, but until
28-
it is released, we'll need to work around it.
29-
"""
30-
if output_dir.exists():
31-
shutil.rmtree(str(output_dir.resolve()))
25+
def is_pre_commit_clean(cookie_dir):
26+
"""Check that the directory is pre-commit clean
3227
28+
side effect: creates a git repository in the cookie_dir"""
29+
subprocess.check_call(["git", "init"], cwd=cookie_dir)
30+
subprocess.check_call(["git", "add", "."], cwd=cookie_dir)
31+
subprocess.check_call(["pre-commit", "run", "--all-files", "--show-diff-on-failure"], cwd=cookie_dir)
32+
return True
3333

3434
def compare_template_dirs(*, library_name='test', library_prefix=None):
3535
""" Helper function to compare the results of generated files,
@@ -78,9 +78,6 @@ def test_new_cookiecutter_only_required_entries():
7878
required fields (cookiecutter.json values of 'null').
7979
"""
8080

81-
# delete testing output directory if exists
82-
overwrite_workaround()
83-
8481
test_context = {}
8582
for key, value in cookie_json.items():
8683
if not key.startswith('_'):
@@ -95,18 +92,16 @@ def test_new_cookiecutter_only_required_entries():
9592
extra_context=test_context
9693
)
9794

98-
assert os.listdir(output_dir)[0] == 'Adafruit_CircuitPython_test'
95+
assert new_cookie.rpartition(os.sep)[2] == 'Adafruit_CircuitPython_test'
9996
assert compare_template_dirs(library_prefix="Adafruit")
97+
assert is_pre_commit_clean(new_cookie)
10098

10199
def test_new_cookiecutter_all_entries():
102100
""" Basic test of running cookiecutter, supplying info for all fields.
103101
All fields will have 'test' except where specific values are required, so this is only a
104102
minimal & cursory test.
105103
"""
106104

107-
# delete testing output directory if exists
108-
overwrite_workaround()
109-
110105
test_context = {}
111106
for key in cookie_json:
112107
if not key.startswith('_'):
@@ -120,5 +115,6 @@ def test_new_cookiecutter_all_entries():
120115
extra_context=test_context
121116
)
122117

123-
assert os.listdir(output_dir)[0] == 'Test_CircuitPython_test'
118+
assert new_cookie.rpartition(os.sep)[2] == 'Test_CircuitPython_test'
124119
assert compare_template_dirs(library_prefix='Test')
120+
assert is_pre_commit_clean(new_cookie)

tests_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
# SPDX-License-Identifier: Unlicense
44

55
cookiecutter >= 2.1.0
6+
pre-commit
67
pytest
78
setuptools

0 commit comments

Comments
 (0)