Skip to content

Commit

Permalink
fix: hook test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
christokur committed Feb 3, 2023
1 parent fe37978 commit cc69716
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions cookiecutter/hooks.py
Expand Up @@ -117,6 +117,7 @@ def run_script_with_context(script_path, cwd, context):
debug_hooks_path = pathlib.Path(debug_hooks_path)
if not debug_hooks_path.exists():
debug_hooks_path = tempfile.gettempdir()
os.environ['COOKIECUTTER_DEBUG_HOOKS'] = debug_hooks_path
with tempfile.NamedTemporaryFile(
delete=False,
mode='wb',
Expand Down
24 changes: 13 additions & 11 deletions tests/test_hooks.py
Expand Up @@ -231,17 +231,19 @@ def test_run_hook_debug(self):
if os.path.isdir(debug_hooks_dir):
shutil.rmtree(debug_hooks_dir)
os.mkdir(debug_hooks_dir)
os.environ['COOKIECUTTER_DEBUG_HOOKS'] = debug_hooks_dir
with utils.work_in(self.repo_path):
hooks.run_hook('pre_gen_project', tests_dir, {})
assert os.path.isfile(os.path.join(tests_dir, 'python_pre.txt'))
assert os.path.isfile(os.path.join(tests_dir, 'shell_pre.txt'))

hooks.run_hook('post_gen_project', tests_dir, {})
assert os.path.isfile(os.path.join(tests_dir, 'shell_post.txt'))
del os.environ['COOKIECUTTER_DEBUG_HOOKS']
hook_traces = list(pathlib.Path(debug_hooks_dir).glob('*'))
assert len(hook_traces) > 2, os.system("ls -l " + debug_hooks_dir)
for location in (debug_hooks_dir, '1'):
os.environ['COOKIECUTTER_DEBUG_HOOKS'] = location
with utils.work_in(self.repo_path):
hooks.run_hook('pre_gen_project', tests_dir, {})
assert os.path.isfile(os.path.join(tests_dir, 'python_pre.txt'))
assert os.path.isfile(os.path.join(tests_dir, 'shell_pre.txt'))

hooks.run_hook('post_gen_project', tests_dir, {})
assert os.path.isfile(os.path.join(tests_dir, 'shell_post.txt'))
hook_traces = list(pathlib.Path(os.environ['COOKIECUTTER_DEBUG_HOOKS']).glob('*'))
assert len(hook_traces) > 2, os.system("ls -l " + os.environ['COOKIECUTTER_DEBUG_HOOKS'])
del os.environ['COOKIECUTTER_DEBUG_HOOKS']
shutil.rmtree(debug_hooks_dir)

def test_run_failing_hook(self):
"""Test correct exception raise if hook exit code is not zero."""
Expand Down

0 comments on commit cc69716

Please sign in to comment.