8000 Ignore backup files of hooks by hackebrot · Pull Request #768 · cookiecutter/cookiecutter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Ignore backup files of hooks #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
10000
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions cookiecutter/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ def find_hooks():
Missing scripts will not be included in the returned dict.
"""
hooks_dir = 'hooks'
r = {}
hooks = {}
logging.debug('hooks_dir is {0}'.format(hooks_dir))

if not os.path.isdir(hooks_dir):
logging.debug('No hooks/ dir in template_dir')
return r
return hooks

for f in os.listdir(hooks_dir):
basename = os.path.splitext(os.path.basename(f))[0]
if basename in _HOOKS:
r[basename] = os.path.abspath(os.path.join(hooks_dir, f))
return r
filename = os.path.basename(f)
basename = os.path.splitext(filename)[0]

if basename in _HOOKS and not filename.endswith('~'):
hooks[basename] = os.path.abspath(os.path.join(hooks_dir, f))
return hooks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really a fan of this existing implementation- the caller knows which hook it's looking for, so it's weird that we iterate through all the hook files on pre- and post-...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% agreed!



def run_script(script_path, cwd='.'):
Expand Down
7 changes: 7 additions & 0 deletions tests/hooks-backup-files/hooks/post_gen_project.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('post_gen_project')
logger.info('post_gen_project.py~')
8 changes: 8 additions & 0 deletions tests/hooks-backup-files/hooks/pre_gen_project.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('pre_gen_project')
logger.info('pre_gen_project.py~')
24 changes: 22 additions & 2 deletions tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
Tests for `cookiecutter.hooks` module.
"""

import sys
import os
import stat
import pytest
import stat
import sys

from cookiecutter import hooks, utils, exceptions

Expand Down Expand Up @@ -179,3 +179,23 @@ def test_run_failing_hook(self):
with pytest.raises(exceptions.FailedHookException) as excinfo:
hooks.run_hook('pre_gen_project', tests_dir, {})
assert 'Hook script failed' in str(excinfo.value)


@pytest.fixture
def hook_backup_files():
"""Return basenames of all files in the hooks dir."""
return sorted([
os.path.basename(f) for f in
os.listdir('tests/hooks-backup-files/hooks')
])


def test_ignore_hook_backup_files(monkeypatch, hook_backup_files):
# This makes sure that the files are actually in the directory
assert hook_backup_files == [
'post_gen_project.py~',
'pre_gen_project.py~',
]

monkeypatch.chdir('tests/hooks-backup-files/')
assert hooks.find_hooks() == {}
0