5
5
import json
6
6
import os
7
7
import pathlib
8
- import shutil
8
+ import subprocess
9
9
import sys
10
10
11
11
from cookiecutter .main import cookiecutter
22
22
with open (cookie_json_path .resolve (), 'r' ) as cookie_file :
23
23
cookie_json = json .load (cookie_file )
24
24
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
32
27
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
33
33
34
34
def compare_template_dirs (* , library_name = 'test' , library_prefix = None ):
35
35
""" Helper function to compare the results of generated files,
@@ -78,9 +78,6 @@ def test_new_cookiecutter_only_required_entries():
78
78
required fields (cookiecutter.json values of 'null').
79
79
"""
80
80
81
- # delete testing output directory if exists
82
- overwrite_workaround ()
83
-
84
81
test_context = {}
85
82
for key , value in cookie_json .items ():
86
83
if not key .startswith ('_' ):
@@ -95,18 +92,16 @@ def test_new_cookiecutter_only_required_entries():
95
92
extra_context = test_context
96
93
)
97
94
98
- assert os . listdir ( output_dir )[ 0 ] == 'Adafruit_CircuitPython_test'
95
+ assert new_cookie . rpartition ( os . sep )[ 2 ] == 'Adafruit_CircuitPython_test'
99
96
assert compare_template_dirs (library_prefix = "Adafruit" )
97
+ assert is_pre_commit_clean (new_cookie )
100
98
101
99
def test_new_cookiecutter_all_entries ():
102
100
""" Basic test of running cookiecutter, supplying info for all fields.
103
101
All fields will have 'test' except where specific values are required, so this is only a
104
102
minimal & cursory test.
105
103
"""
106
104
107
- # delete testing output directory if exists
108
- overwrite_workaround ()
109
-
110
105
test_context = {}
111
106
for key in cookie_json :
112
107
if not key .startswith ('_' ):
@@ -120,5 +115,6 @@ def test_new_cookiecutter_all_entries():
120
115
extra_context = test_context
121
116
)
122
117
123
- assert os . listdir ( output_dir )[ 0 ] == 'Test_CircuitPython_test'
118
+ assert new_cookie . rpartition ( os . sep )[ 2 ] == 'Test_CircuitPython_test'
124
119
assert compare_template_dirs (library_prefix = 'Test' )
120
+ assert is_pre_commit_clean (new_cookie )
0 commit comments