From 30ef02153f56593e1904997ef2265dd63c457e8a Mon Sep 17 00:00:00 2001 From: dinopetrone Date: Mon, 4 Nov 2013 14:52:53 -0800 Subject: [PATCH 1/4] adding ability for any pre/post hooks to be run in same virtualenv as the current session --- cookiecutter/hooks.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cookiecutter/hooks.py b/cookiecutter/hooks.py index 1b8a60b0a..939b111c7 100755 --- a/cookiecutter/hooks.py +++ b/cookiecutter/hooks.py @@ -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, From 963691b81ea806fdf629737d10e73401b95d606e Mon Sep 17 00:00:00 2001 From: dinopetrone Date: Wed, 13 Nov 2013 09:12:44 -0800 Subject: [PATCH 2/4] updating with new unittest for ensuring that sys executable is added when running Popen --- tests/test_hooks.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_hooks.py b/tests/test_hooks.py index e7e210f2a..8bd162436 100755 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -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): @@ -71,6 +81,13 @@ 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 obj: + 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') + obj.assert_called_with(script_path, cwd='.', shell=run_thru_shell) if __name__ == '__main__': unittest.main() From b53997a60c4f6cf7a44432cc1509eb850672e9ee Mon Sep 17 00:00:00 2001 From: dinopetrone Date: Wed, 13 Nov 2013 11:45:34 -0800 Subject: [PATCH 3/4] adding a test for the .sh extension and ensuring .py isnt conflicting --- tests/test_hooks.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_hooks.py b/tests/test_hooks.py index 8bd162436..afb8f1843 100755 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -89,5 +89,13 @@ def test_run_hook_python_executable(self): run_thru_shell = sys.platform.startswith('win') obj.assert_called_with(script_path, cwd='.', shell=run_thru_shell) + def test_run_hook_shell_executable(self): + with patch('subprocess.Popen') as obj: + 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') + obj.assert_called_with(script_path, cwd='.', shell=run_thru_shell) + if __name__ == '__main__': unittest.main() From 327a302dc3360af6afa2ecee01fdbb7cba7ec9e5 Mon Sep 17 00:00:00 2001 From: dinopetrone Date: Thu, 14 Nov 2013 04:54:34 -0800 Subject: [PATCH 4/4] updating variable name in test based on suggestion --- tests/test_hooks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_hooks.py b/tests/test_hooks.py index afb8f1843..93749f15a 100755 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -82,20 +82,20 @@ def test_public_run_hook(self): 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 obj: + 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') - obj.assert_called_with(script_path, cwd='.', shell=run_thru_shell) + subprocess.assert_called_with(script_path, cwd='.', shell=run_thru_shell) def test_run_hook_shell_executable(self): - with patch('subprocess.Popen') as obj: + 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') - obj.assert_called_with(script_path, cwd='.', shell=run_thru_shell) + subprocess.assert_called_with(script_path, cwd='.', shell=run_thru_shell) if __name__ == '__main__': unittest.main()