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

Skip to content

Commit cce97cc

Browse files
committed
Include hook id in output with --verbose. Closes #88.
1 parent edb0442 commit cce97cc

4 files changed

Lines changed: 177 additions & 36 deletions

File tree

pre_commit/commands.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import pkg_resources
66
import shutil
77
import stat
8-
import subprocess
98
import sys
109
from asottile.ordereddict import OrderedDict
1110
from asottile.yaml import ordered_dump
@@ -19,17 +18,14 @@
1918
from pre_commit.clientlib.validate_config import load_config
2019
from pre_commit.jsonschema_extensions import remove_defaults
2120
from pre_commit.logging_handler import LoggingHandler
21+
from pre_commit.output import get_hook_message
2222
from pre_commit.repository import Repository
2323
from pre_commit.staged_files_only import staged_files_only
2424
from pre_commit.util import noop_context
2525

2626

2727
logger = logging.getLogger('pre_commit')
2828

29-
COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0])
30-
31-
PASS_FAIL_LENGTH = 6
32-
3329

3430
def install(runner):
3531
"""Install the pre-commit hooks."""
@@ -107,7 +103,8 @@ def autoupdate(runner):
107103
)
108104

109105
for repo_config in input_configs:
110-
print('Updating {0}...'.format(repo_config['repo']), end='')
106+
sys.stdout.write('Updating {0}...'.format(repo_config['repo']))
107+
sys.stdout.flush()
111108
try:
112109
new_repo_config = _update_repository(repo_config)
113110
except RepositoryCannotBeUpdatedError as error:
@@ -149,34 +146,30 @@ def _get_skips(environ):
149146
return set(skip.strip() for skip in skips.split(',') if skip.strip())
150147

151148

152-
def _print_no_files_skipped(hook, write, args):
153-
no_files_msg = '(no files to check) '
154-
skipped_msg = 'Skipped'
155-
write(
156-
'{0}{1}{2}{3}\n'.format(
157-
hook['name'],
158-
'.' * (
159-
COLS -
160-
len(hook['name']) -
161-
len(no_files_msg) -
162-
len(skipped_msg) -
163-
1
164-
),
165-
no_files_msg,
166-
color.format_color(skipped_msg, color.TURQUOISE, args.color),
167-
)
149+
def _hook_msg_start(hook, verbose):
150+
return '{0}{1}'.format(
151+
'[{0}] '.format(hook['id']) if verbose else '',
152+
hook['name'],
168153
)
169154

170155

156+
def _print_no_files_skipped(hook, write, args):
157+
write(get_hook_message(
158+
_hook_msg_start(hook, args.verbose),
159+
postfix='(no files to check) ',
160+
end_msg='Skipped',
161+
end_color=color.TURQUOISE,
162+
use_color=args.color,
163+
))
164+
165+
171166
def _print_user_skipped(hook, write, args):
172-
skipped_msg = 'Skipped'
173-
write(
174-
'{0}{1}{2}\n'.format(
175-
hook['name'],
176-
'.' * (COLS - len(hook['name']) - len(skipped_msg) - 1),
177-
color.format_color(skipped_msg, color.YELLOW, args.color),
178-
),
179-
)
167+
write(get_hook_message(
168+
_hook_msg_start(hook, args.verbose),
169+
end_msg='Skipped',
170+
end_color=color.YELLOW,
171+
use_color=args.color,
172+
))
180173

181174

182175
def _run_single_hook(runner, repository, hook_id, args, write, skips=set()):
@@ -199,12 +192,7 @@ def _run_single_hook(runner, repository, hook_id, args, write, skips=set()):
199192

200193
# Print the hook and the dots first in case the hook takes hella long to
201194
# run.
202-
write(
203-
'{0}{1}'.format(
204-
hook['name'],
205-
'.' * (COLS - len(hook['name']) - PASS_FAIL_LENGTH - 1),
206-
),
207-
)
195+
write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6))
208196
sys.stdout.flush()
209197

210198
retcode, stdout, stderr = repository.run_hook(

pre_commit/output.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import subprocess
2+
3+
from pre_commit import color
4+
5+
6+
# TODO: smell: import side-effects
7+
COLS = int(
8+
subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0]
9+
)
10+
11+
12+
def get_hook_message(
13+
start,
14+
postfix='',
15+
end_msg=None,
16+
end_len=0,
17+
end_color=None,
18+
use_color=None,
19+
cols=COLS,
20+
):
21+
"""Prints a message for running a hook.
22+
23+
This currently supports three approaches:
24+
25+
# Print `start` followed by dots, leaving 6 characters at the end
26+
>>> print_hook_message('start', end_len=6)
27+
start...............................................................
28+
29+
# Print `start` followed by dots with the end message colored if coloring is
30+
# specified and a newline afterwards
31+
>>> print_hook_message(
32+
'start',
33+
end_msg='end',
34+
end_color=color.RED,
35+
use_color=True,
36+
)
37+
start...................................................................end
38+
39+
# Print `start` followed by dots, followed by the `postfix` message
40+
# uncolored, followed by the `end_msg` colored if specified and a newline
41+
# afterwards
42+
>>> print_hook_message(
43+
'start',
44+
postfix='postfix ',
45+
end_msg='end',
46+
end_color=color.RED,
47+
use_color=True,
48+
)
49+
start...........................................................postfix end
50+
"""
51+
if bool(end_msg) == bool(end_len):
52+
raise ValueError('Expected one of (`end_msg`, `end_len`)')
53+
if end_msg is not None and (end_color is None or use_color is None):
54+
raise ValueError(
55+
'`end_color` and `use_color` are required with `end_msg`'
56+
)
57+
58+
if end_len:
59+
return start + '.' * (cols - len(start) - end_len - 1)
60+
else:
61+
return '{0}{1}{2}{3}\n'.format(
62+
start,
63+
'.' * (cols - len(start) - len(postfix) - len(end_msg) - 1),
64+
postfix,
65+
color.format_color(end_msg, end_color, use_color),
66+
)

tests/commands_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,13 @@ def test_skip_hook(repo_with_passing_hook):
330330
)
331331
for msg in ('Bash hook', 'Skipped'):
332332
assert msg in printed
333+
334+
335+
def test_hook_id_not_in_non_verbose_output(repo_with_passing_hook):
336+
ret, printed = _do_run(repo_with_passing_hook, _get_opts(verbose=False))
337+
assert '[bash_hook]' not in printed
338+
339+
340+
def test_hook_id_in_verbose_output(repo_with_passing_hook):
341+
ret, printed = _do_run(repo_with_passing_hook, _get_opts(verbose=True))
342+
assert '[bash_hook] Bash hook' in printed

tests/output_test.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import pytest
2+
3+
from pre_commit import color
4+
from pre_commit.output import get_hook_message
5+
6+
7+
@pytest.mark.parametrize(
8+
'kwargs',
9+
(
10+
# both end_msg and end_len
11+
{'end_msg': 'end', 'end_len': 1, 'end_color': '', 'use_color': True},
12+
# Neither end_msg nor end_len
13+
{},
14+
# Neither color option for end_msg
15+
{'end_msg': 'end'},
16+
# No use_color for end_msg
17+
{'end_msg': 'end', 'end_color': ''},
18+
# No end_color for end_msg
19+
{'end_msg': 'end', 'use_color': ''},
20+
),
21+
)
22+
def test_get_hook_message_raises(kwargs):
23+
with pytest.raises(ValueError):
24+
get_hook_message('start', **kwargs)
25+
26+
27+
def test_case_with_end_len():
28+
ret = get_hook_message('start', end_len=5, cols=15)
29+
assert ret == 'start' + '.' * 4
30+
31+
32+
def test_case_with_end_msg():
33+
ret = get_hook_message(
34+
'start',
35+
end_msg='end',
36+
end_color='',
37+
use_color=False,
38+
cols=15,
39+
)
40+
assert ret == 'start' + '.' * 6 + 'end' + '\n'
41+
42+
43+
def test_case_with_end_msg_using_color():
44+
ret = get_hook_message(
45+
'start',
46+
end_msg='end',
47+
end_color=color.RED,
48+
use_color=True,
49+
cols=15,
50+
)
51+
assert ret == 'start' + '.' * 6 + color.RED + 'end' + color.NORMAL + '\n'
52+
53+
54+
def test_case_with_postfix_message():
55+
ret = get_hook_message(
56+
'start',
57+
postfix='post ',
58+
end_msg='end',
59+
end_color='',
60+
use_color=False,
61+
cols=20,
62+
)
63+
assert ret == 'start' + '.' * 6 + 'post ' + 'end' + '\n'
64+
65+
66+
def test_make_sure_postfix_is_not_colored():
67+
ret = get_hook_message(
68+
'start',
69+
postfix='post ',
70+
end_msg='end',
71+
end_color=color.RED,
72+
use_color=True,
73+
cols=20,
74+
)
75+
assert ret == (
76+
'start' + '.' * 6 + 'post ' + color.RED + 'end' + color.NORMAL + '\n'
77+
)

0 commit comments

Comments
 (0)