|
5 | 5 | import os.path |
6 | 6 | import pipes |
7 | 7 | import sys |
| 8 | +import time |
8 | 9 |
|
9 | 10 | import mock |
10 | 11 | import pytest |
|
25 | 26 | from testing.fixtures import modify_config |
26 | 27 | from testing.fixtures import read_config |
27 | 28 | from testing.fixtures import sample_meta_config |
| 29 | +from testing.fixtures import write_config |
28 | 30 | from testing.util import cmd_output_mocked_pre_commit_home |
29 | 31 | from testing.util import cwd |
30 | 32 | from testing.util import git_commit |
@@ -163,36 +165,55 @@ def test_exclude_types_hook_repository(cap_out, store, tempdir_factory): |
163 | 165 | assert b'exe' not in printed |
164 | 166 |
|
165 | 167 |
|
166 | | -def test_global_exclude(cap_out, store, tempdir_factory): |
167 | | - git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') |
168 | | - with cwd(git_path): |
169 | | - with modify_config() as config: |
170 | | - config['exclude'] = '^foo.py$' |
171 | | - open('foo.py', 'a').close() |
172 | | - open('bar.py', 'a').close() |
173 | | - cmd_output('git', 'add', '.') |
174 | | - opts = run_opts(verbose=True) |
175 | | - ret, printed = _do_run(cap_out, store, git_path, opts) |
176 | | - assert ret == 0 |
177 | | - # Does not contain foo.py since it was excluded |
178 | | - expected = b'- hook id: bash_hook\n\nbar.py\nHello World\n\n' |
179 | | - assert printed.endswith(expected) |
| 168 | +def test_global_exclude(cap_out, store, in_git_dir): |
| 169 | + config = { |
| 170 | + 'exclude': r'^foo\.py$', |
| 171 | + 'repos': [{'repo': 'meta', 'hooks': [{'id': 'identity'}]}], |
| 172 | + } |
| 173 | + write_config('.', config) |
| 174 | + open('foo.py', 'a').close() |
| 175 | + open('bar.py', 'a').close() |
| 176 | + cmd_output('git', 'add', '.') |
| 177 | + opts = run_opts(verbose=True) |
| 178 | + ret, printed = _do_run(cap_out, store, str(in_git_dir), opts) |
| 179 | + assert ret == 0 |
| 180 | + # Does not contain foo.py since it was excluded |
| 181 | + assert printed.startswith(b'identity' + b'.' * 65 + b'Passed\n') |
| 182 | + assert printed.endswith(b'\n\n.pre-commit-config.yaml\nbar.py\n\n') |
180 | 183 |
|
181 | 184 |
|
182 | | -def test_global_files(cap_out, store, tempdir_factory): |
183 | | - git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') |
184 | | - with cwd(git_path): |
185 | | - with modify_config() as config: |
186 | | - config['files'] = '^bar.py$' |
187 | | - open('foo.py', 'a').close() |
188 | | - open('bar.py', 'a').close() |
189 | | - cmd_output('git', 'add', '.') |
190 | | - opts = run_opts(verbose=True) |
191 | | - ret, printed = _do_run(cap_out, store, git_path, opts) |
192 | | - assert ret == 0 |
193 | | - # Does not contain foo.py since it was not included |
194 | | - expected = b'- hook id: bash_hook\n\nbar.py\nHello World\n\n' |
195 | | - assert printed.endswith(expected) |
| 185 | +def test_global_files(cap_out, store, in_git_dir): |
| 186 | + config = { |
| 187 | + 'files': r'^bar\.py$', |
| 188 | + 'repos': [{'repo': 'meta', 'hooks': [{'id': 'identity'}]}], |
| 189 | + } |
| 190 | + write_config('.', config) |
| 191 | + open('foo.py', 'a').close() |
| 192 | + open('bar.py', 'a').close() |
| 193 | + cmd_output('git', 'add', '.') |
| 194 | + opts = run_opts(verbose=True) |
| 195 | + ret, printed = _do_run(cap_out, store, str(in_git_dir), opts) |
| 196 | + assert ret == 0 |
| 197 | + # Does not contain foo.py since it was excluded |
| 198 | + assert printed.startswith(b'identity' + b'.' * 65 + b'Passed\n') |
| 199 | + assert printed.endswith(b'\n\nbar.py\n\n') |
| 200 | + |
| 201 | + |
| 202 | +@pytest.mark.parametrize( |
| 203 | + ('t1', 't2', 'expected'), |
| 204 | + ( |
| 205 | + (1.234, 2., b'\n- duration: 0.77s\n'), |
| 206 | + (1., 1., b'\n- duration: 0s\n'), |
| 207 | + ), |
| 208 | +) |
| 209 | +def test_verbose_duration(cap_out, store, in_git_dir, t1, t2, expected): |
| 210 | + write_config('.', {'repo': 'meta', 'hooks': [{'id': 'identity'}]}) |
| 211 | + cmd_output('git', 'add', '.') |
| 212 | + opts = run_opts(verbose=True) |
| 213 | + with mock.patch.object(time, 'time', side_effect=(t1, t2)): |
| 214 | + ret, printed = _do_run(cap_out, store, str(in_git_dir), opts) |
| 215 | + assert ret == 0 |
| 216 | + assert expected in printed |
196 | 217 |
|
197 | 218 |
|
198 | 219 | @pytest.mark.parametrize( |
|
0 commit comments