| 1 |  |  | #!/usr/bin/env python | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # -*- coding: utf-8 -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | test_hooks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | ------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | Tests for `cookiecutter.hooks` module. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | import sys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | import stat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | import pytest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | from cookiecutter import hooks, utils, exceptions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | def make_test_repo(name): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     """Helper function which is called in the test setup methods.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     hook_dir = os.path.join(name, 'hooks') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     template = os.path.join(name, 'input{{hooks}}') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     os.mkdir(name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     os.mkdir(hook_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     os.mkdir(template) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     with open(os.path.join(template, 'README.rst'), 'w') as f: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         f.write("foo\n===\n\nbar\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     with open(os.path.join(hook_dir, 'pre_gen_project.py'), 'w') as f: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         f.write("#!/usr/bin/env python\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         f.write("# -*- coding: utf-8 -*-\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         f.write("from __future__ import print_function\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         f.write("\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         f.write("print('pre generation hook')\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         f.write("f = open('python_pre.txt', 'w')\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         f.write("f.close()\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 39 |  | View Code Duplication |     if sys.platform.startswith('win'): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         post = 'post_gen_project.bat' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         with open(os.path.join(hook_dir, post), 'w') as f: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             f.write("@echo off\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             f.write("\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             f.write("echo post generation hook\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             f.write("echo. >shell_post.txt\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         post = 'post_gen_project.sh' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         filename = os.path.join(hook_dir, post) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         with open(filename, 'w') as f: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             f.write("#!/bin/bash\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             f.write("\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             f.write("echo 'post generation hook';\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             f.write("touch 'shell_post.txt'\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         # Set the execute bit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         os.chmod(filename, os.stat(filename).st_mode | stat.S_IXUSR) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     return post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | class TestFindHooks(object): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     repo_path = 'tests/test-hooks' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     def setup_method(self, method): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         self.post_hook = make_test_repo(self.repo_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     def teardown_method(self, method): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         utils.rmtree(self.repo_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     def test_find_hook(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         """Finds the specified hook.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         with utils.work_in(self.repo_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             expected = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 'pre_gen_project': os.path.abspath('hooks/pre_gen_project.py'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 'post_gen_project': os.path.abspath( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     os.path.join('hooks', self.post_hook) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             assert expected == hooks.find_hooks() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     def test_no_hooks(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         """find_hooks should return None if the hook could not be found.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         with utils.work_in('tests/fake-repo'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             assert {} == hooks.find_hooks() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | class TestExternalHooks(object): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     repo_path = os.path.abspath('tests/test-hooks/') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     hooks_path = os.path.abspath('tests/test-hooks/hooks') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     def setup_method(self, method): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         self.post_hook = make_test_repo(self.repo_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     def teardown_method(self, method): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         utils.rmtree(self.repo_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if os.path.exists('python_pre.txt'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             os.remove('python_pre.txt') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if os.path.exists('shell_post.txt'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             os.remove('shell_post.txt') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         if os.path.exists('tests/shell_post.txt'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             os.remove('tests/shell_post.txt') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if os.path.exists('tests/test-hooks/input{{hooks}}/python_pre.txt'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             os.remove('tests/test-hooks/input{{hooks}}/python_pre.txt') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if os.path.exists('tests/test-hooks/input{{hooks}}/shell_post.txt'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             os.remove('tests/test-hooks/input{{hooks}}/shell_post.txt') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if os.path.exists('tests/context_post.txt'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             os.remove('tests/context_post.txt') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     def test_run_script_with_context(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         """Execute a hook script, passing a context""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         hook_path = os.path.join(self.hooks_path, 'post_gen_project.sh') | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         if sys.platform.startswith('win'): | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |             post = 'post_gen_project.bat' | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             with open(os.path.join(self.hooks_path, post), 'w') as f: | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |                 f.write("@echo off\n") | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |                 f.write("\n") | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |                 f.write("echo post generation hook\n") | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |                 f.write("echo. >{{cookiecutter.file}}\n") | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             with open(hook_path, 'w') as fh: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 fh.write("#!/bin/bash\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 fh.write("\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 fh.write("echo 'post generation hook';\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 fh.write("touch 'shell_post.txt'\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 fh.write("touch '{{cookiecutter.file}}'\n") | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 132 |  | View Code Duplication |                 os.chmod(hook_path, os.stat(hook_path).st_mode | stat.S_IXUSR) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         hooks.run_script_with_context( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             os.path.join(self.hooks_path, self.post_hook), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             'tests', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 'cookiecutter': { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                     'file': 'context_post.txt' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         assert os.path.isfile('tests/context_post.txt') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         assert 'tests' not in os.getcwd() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     def test_run_hook(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         """Execute hook from specified template in specified output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         directory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         with utils.work_in(self.repo_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             hooks.run_hook('pre_gen_project', tests_dir, {}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             assert os.path.isfile(os.path.join(tests_dir, 'python_pre.txt')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             hooks.run_hook('post_gen_project', tests_dir, {}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             assert os.path.isfile(os.path.join(tests_dir, 'shell_post.txt')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 157 |  | View Code Duplication |     def test_run_failing_hook(self): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         hook_path = os.path.join(self.hooks_path, 'pre_gen_project.py') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         with open(hook_path, 'w') as f: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             f.write("#!/usr/bin/env python\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             f.write("import sys; sys.exit(1)\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         with utils.work_in(self.repo_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             with pytest.raises(exceptions.FailedHookException) as excinfo: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 hooks.run_hook('pre_gen_project', tests_dir, {}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             assert 'Hook script failed' in str(excinfo.value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 170 |  | View Code Duplication |     def test_run_failing_hook_with_stderr_message(self): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         hook_path = os.path.join(self.hooks_path, 'pre_gen_project.py') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         expected = "Expected error message" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         with open(hook_path, 'w') as f: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             f.write("#!/usr/bin/env python\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             f.write( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 "import sys; sys.stderr.write(%s) sys.exit(1)\n" % expected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         with utils.work_in(self.repo_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             with pytest.raises(exceptions.FailedHookException) as excinfo: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |                 hooks.run_hook('pre_gen_project', tests_dir, {}) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 183 |  |  |             assert expected in str(excinfo.value) | 
            
                                                        
            
                                    
            
            
                | 184 |  |  |  |