Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c7ec7a

Browse files
committed
Merge pull request #79 from pre-commit/skip_if_no_files
Skip if no files
2 parents e297b21 + de24712 commit 2c7ec7a

2 files changed

Lines changed: 57 additions & 72 deletions

File tree

pre_commit/commands.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,26 @@ def _run_single_hook(runner, repository, hook_id, args, write):
151151

152152
hook = repository.hooks[hook_id]
153153

154+
filenames = get_filenames(hook['files'], hook['exclude'])
155+
if not filenames:
156+
no_files_msg = '(no files to check) '
157+
skipped_msg = 'Skipped'
158+
write(
159+
'{0}{1}{2}{3}\n'.format(
160+
hook['name'],
161+
'.' * (
162+
COLS -
163+
len(hook['name']) -
164+
len(no_files_msg) -
165+
len(skipped_msg) -
166+
6
167+
),
168+
no_files_msg,
169+
color.format_color(skipped_msg, color.TURQUOISE, args.color),
170+
)
171+
)
172+
return 0
173+
154174
# Print the hook and the dots first in case the hook takes hella long to
155175
# run.
156176
write(
@@ -164,7 +184,7 @@ def _run_single_hook(runner, repository, hook_id, args, write):
164184
retcode, stdout, stderr = repository.run_hook(
165185
runner.cmd_runner,
166186
hook_id,
167-
get_filenames(hook['files'], hook['exclude']),
187+
filenames,
168188
)
169189

170190
if retcode != repository.hooks[hook_id]['expected_return_value']:

tests/commands_test.py

Lines changed: 36 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -192,86 +192,51 @@ def get_write_mock_output(write_mock):
192192
return ''.join(call[0][0] for call in write_mock.call_args_list)
193193

194194

195-
def test_run_all_hooks_passing(repo_with_passing_hook):
196-
stage_a_file()
197-
runner = Runner(repo_with_passing_hook)
195+
def _test_run(repo, options, expected_outputs, expected_ret, stage):
196+
if stage:
197+
stage_a_file()
198+
runner = Runner(repo)
198199
args = auto_namedtuple(
199-
all_files=False, color=False, verbose=False, hook=None,
200-
)
201-
write_mock = mock.Mock()
202-
ret = commands.run(runner, args, write=write_mock)
203-
assert ret == 0
204-
printed = get_write_mock_output(write_mock)
205-
assert 'Bash hook' in printed
206-
assert 'Passed' in printed
207-
208-
209-
def test_verbose_prints_output(repo_with_passing_hook):
210-
stage_a_file()
211-
runner = Runner(repo_with_passing_hook)
212-
args = auto_namedtuple(
213-
all_files=False, color=False, verbose=True, hook=None,
200+
**dict(
201+
dict(all_files=False, color=False, verbose=False, hook=None),
202+
**options
203+
)
214204
)
215205
write_mock = mock.Mock()
216206
ret = commands.run(runner, args, write=write_mock)
217-
assert ret == 0
207+
assert ret == expected_ret
218208
printed = get_write_mock_output(write_mock)
219-
assert 'foo.py\nHello World\n' in printed
209+
for expected_output_part in expected_outputs:
210+
assert expected_output_part in printed
220211

221212

222213
def test_run_all_hooks_failing(repo_with_failing_hook):
223-
stage_a_file()
224-
runner = Runner(repo_with_failing_hook)
225-
args = auto_namedtuple(
226-
all_files=False, color=False, verbose=False, hook=None,
227-
)
228-
write_mock = mock.Mock()
229-
ret = commands.run(runner, args, write=write_mock)
230-
assert ret == 1
231-
printed = get_write_mock_output(write_mock)
232-
assert 'Failing hook' in printed
233-
assert 'Failed' in printed
234-
assert 'Fail\nfoo.py\n' in printed
235-
236-
237-
def test_run_a_specific_hook(repo_with_passing_hook):
238-
stage_a_file()
239-
runner = Runner(repo_with_passing_hook)
240-
args = auto_namedtuple(
241-
all_files=False, color=False, verbose=False, hook='bash_hook',
242-
)
243-
write_mock = mock.Mock()
244-
ret = commands.run(runner, args, write=write_mock)
245-
assert ret == 0
246-
printed = get_write_mock_output(write_mock)
247-
assert 'Bash hook' in printed
248-
assert 'Passed' in printed
249-
250-
251-
def test_run_a_non_existing_hook(repo_with_passing_hook):
252-
stage_a_file()
253-
runner = Runner(repo_with_passing_hook)
254-
args = auto_namedtuple(
255-
all_files=False, color=False, verbose=False, hook='nope',
214+
_test_run(
215+
repo_with_failing_hook,
216+
{},
217+
('Failing hook', 'Failed', 'Fail\nfoo.py\n'),
218+
1,
219+
True,
256220
)
257-
write_mock = mock.Mock()
258-
ret = commands.run(runner, args, write=write_mock)
259-
assert ret == 1
260-
printed = get_write_mock_output(write_mock)
261-
assert 'No hook with id `nope`' in printed
262221

263222

264-
def test_run_all_files(repo_with_passing_hook):
265-
stage_a_file()
266-
runner = Runner(repo_with_passing_hook)
267-
args = auto_namedtuple(
268-
all_files=True, color=False, verbose=True, hook=None,
223+
@pytest.mark.parametrize(
224+
('options', 'outputs', 'expected_ret', 'stage'),
225+
(
226+
({}, ('Bash hook', 'Passed'), 0, True),
227+
({'verbose': True}, ('foo.py\nHello World',), 0, True),
228+
({'hook': 'bash_hook'}, ('Bash hook', 'Passed'), 0, True),
229+
({'hook': 'nope'}, ('No hook with id `nope`',), 1, True),
230+
# All the files in the repo.
231+
# This seems kind of weird but it is beacuse py.test reuses fixtures
232+
(
233+
{'all_files': True, 'verbose': True},
234+
('hooks.yaml', 'bin/hook.sh', 'foo.py', 'dummy'),
235+
0,
236+
True,
237+
),
238+
({}, ('Bash hook', '(no files to check)', 'Skipped'), 0, False),
269239
)
270-
write_mock = mock.Mock()
271-
ret = commands.run(runner, args, write=write_mock)
272-
assert ret == 0
273-
printed = get_write_mock_output(write_mock)
274-
# These are all the files checked into the repo.
275-
# This seems kind of weird but it is because py.test reuses fixtures
276-
for filename in 'hooks.yaml bin/hook.sh foo.py dummy'.split():
277-
assert filename in printed
240+
)
241+
def test_run(repo_with_passing_hook, options, outputs, expected_ret, stage):
242+
_test_run(repo_with_passing_hook, options, outputs, expected_ret, stage)

0 commit comments

Comments
 (0)