| Total Complexity | 28 |
| Total Lines | 94 |
| Duplicated Lines | 15.96 % |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | #!/usr/bin/env python |
||
| 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(self): |
||
| 114 | """Execute a hook script, independently of project generation""" |
||
| 115 | hooks.run_script(os.path.join(self.hooks_path, self.post_hook)) |
||
| 116 | assert os.path.isfile('shell_post.txt') |
||
| 117 | |||
| 118 | def test_run_script_cwd(self): |
||
| 119 | """Change directory before running hook""" |
||
| 120 | hooks.run_script( |
||
| 121 | os.path.join(self.hooks_path, self.post_hook), |
||
| 122 | 'tests' |
||
| 123 | ) |
||
| 124 | assert os.path.isfile('tests/shell_post.txt') |
||
| 125 | assert 'tests' not in os.getcwd() |
||
| 126 | |||
| 127 | def test_run_script_with_context(self): |
||
| 128 | """Execute a hook script, passing a context""" |
||
| 129 | |||
| 130 | hook_path = os.path.join(self.hooks_path, 'post_gen_project.sh') |
||
| 131 | |||
| 132 | View Code Duplication | if sys.platform.startswith('win'): |
|
| 133 | post = 'post_gen_project.bat' |
||
| 134 | with open(os.path.join(self.hooks_path, post), 'w') as f: |
||
| 135 | f.write("@echo off\n") |
||
| 136 | f.write("\n") |
||
| 137 | f.write("echo post generation hook\n") |
||
| 138 | f.write("echo. >{{cookiecutter.file}}\n") |
||
| 139 | else: |
||
| 140 | with open(hook_path, 'w') as fh: |
||
| 141 | fh.write("#!/bin/bash\n") |
||
| 142 | fh.write("\n") |
||
| 143 | fh.write("echo 'post generation hook';\n") |
||
| 144 | fh.write("touch 'shell_post.txt'\n") |
||
| 145 | fh.write("touch '{{cookiecutter.file}}'\n") |
||
| 146 | os.chmod(hook_path, os.stat(hook_path).st_mode | stat.S_IXUSR) |
||
| 147 | |||
| 148 | hooks.run_script_with_context( |
||
| 149 | os.path.join(self.hooks_path, self.post_hook), |
||
| 150 | 'tests', |
||
| 151 | { |
||
| 152 | 'cookiecutter': { |
||
| 153 | 'file': 'context_post.txt' |
||
| 154 | } |
||
| 155 | }) |
||
| 156 | assert os.path.isfile('tests/context_post.txt') |
||
| 157 | assert 'tests' not in os.getcwd() |
||
| 158 | |||
| 159 | def test_run_hook(self): |
||
| 160 | """Execute hook from specified template in specified output |
||
| 161 | directory. |
||
| 162 | """ |
||
| 163 | tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') |
||
| 164 | with utils.work_in(self.repo_path): |
||
| 165 | hooks.run_hook('pre_gen_project', tests_dir, {}) |
||
| 166 | assert os.path.isfile(os.path.join(tests_dir, 'python_pre.txt')) |
||
| 167 | |||
| 168 | hooks.run_hook('post_gen_project', tests_dir, {}) |
||
| 169 | assert os.path.isfile(os.path.join(tests_dir, 'shell_post.txt')) |
||
| 170 | |||
| 171 | def test_run_failing_hook(self): |
||
| 172 | hook_path = os.path.join(self.hooks_path, 'pre_gen_project.py') |
||
| 173 | tests_dir = os.path.join(self.repo_path, 'input{{hooks}}') |
||
| 174 | |||
| 175 | with open(hook_path, 'w') as f: |
||
| 176 | f.write("#!/usr/bin/env python\n") |
||
| 177 | f.write("import sys; sys.exit(1)\n") |
||
| 178 | |||
| 179 | with utils.work_in(self.repo_path): |
||
| 180 | with pytest.raises(exceptions.FailedHookException) as excinfo: |
||
| 181 | hooks.run_hook('pre_gen_project', tests_dir, {}) |
||
| 182 | assert 'Hook script failed' in str(excinfo.value) |
||
| 183 |