55import json
66import os
77import pathlib
8- import shutil
8+ import subprocess
99import sys
1010
1111from cookiecutter .main import cookiecutter
2222with 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
3434def 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
10199def 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 )
0 commit comments