Skip to content
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

adding ability for pre/post hooks to be run in same virtualenv if is .py #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cookiecutter/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def _run_hook(script_path, cwd='.'):
absolute).
If `cwd` is provided, the script will be run from that directory.
'''

extension = os.path.splitext(script_path)[1]
if '.py' in extension:
script_path = [sys.executable, script_path]

run_thru_shell = sys.platform.startswith('win')
proc = subprocess.Popen(
script_path,
Expand Down
25 changes: 25 additions & 0 deletions tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@

from cookiecutter import hooks, utils

PY3 = sys.version > '3'
if PY3:
from unittest.mock import patch
input_str = 'builtins.input'
else:
import __builtin__
from mock import patch
input_str = '__builtin__.raw_input'
from cStringIO import StringIO


class TestFindHooks(unittest.TestCase):

Expand Down Expand Up @@ -71,6 +81,21 @@ def test_public_run_hook(self):
hooks.run_hook('post_gen_project', tests_dir)
self.assertTrue(os.path.isfile(os.path.join(tests_dir, 'shell_post.txt')))

def test_run_hook_python_executable(self):
with patch('subprocess.Popen') as subprocess:
hook_path = os.path.join(self.hooks_path, 'pre_gen_project.py')
hooks._run_hook(hook_path)
script_path = [sys.executable, hook_path]
run_thru_shell = sys.platform.startswith('win')
subprocess.assert_called_with(script_path, cwd='.', shell=run_thru_shell)

def test_run_hook_shell_executable(self):
with patch('subprocess.Popen') as subprocess:
hook_path = os.path.join(self.hooks_path, 'pre_gen_project.sh')
hooks._run_hook(hook_path)
script_path = hook_path
run_thru_shell = sys.platform.startswith('win')
subprocess.assert_called_with(script_path, cwd='.', shell=run_thru_shell)

if __name__ == '__main__':
unittest.main()