Skip to content

Commit e0155fb

Browse files
committed
removed meta from stdout; replaced = with : ; handled sys.version newlines; stylized errorlog to md
1 parent de63b6a commit e0155fb

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

pre_commit/error_handler.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ def _to_bytes(exc):
2828

2929
def _log_and_exit(msg, exc, formatted):
3030
error_msg = b''.join((
31-
_to_bytes('### version information\n'),
32-
_to_bytes('pre-commit.version={}\n'.format(C.VERSION)),
33-
_to_bytes('sys.version={}\n'.format(sys.version.replace('\n', ' '))),
34-
_to_bytes('sys.executable={}\n'.format(sys.executable)),
35-
_to_bytes('### error information\n'),
3631
five.to_bytes(msg), b': ',
3732
five.to_bytes(type(exc).__name__), b': ',
3833
_to_bytes(exc), b'\n',
@@ -41,9 +36,26 @@ def _log_and_exit(msg, exc, formatted):
4136
store = Store()
4237
log_path = os.path.join(store.directory, 'pre-commit.log')
4338
output.write_line('Check the log at {}'.format(log_path))
39+
40+
meta_info_msg = '### version information\n```\n'
41+
meta_info_msg += 'pre-commit.version: {}\n'.format(C.VERSION)
42+
meta_info_msg += 'sys.version: \n{}\n'.format(
43+
'\n'.join(
44+
[
45+
'\t{}'.format(line)
46+
for line in sys.version.splitlines()
47+
],
48+
),
49+
)
50+
meta_info_msg += 'sys.executable: {}\n'.format(sys.executable)
51+
meta_info_msg += 'os.name: {}\n'.format(os.name)
52+
meta_info_msg += 'sys.platform: {}\n```\n'.format(sys.platform)
53+
meta_info_msg += '### error information\n```\n'
4454
with open(log_path, 'wb') as log:
55+
output.write(meta_info_msg, stream=log)
4556
output.write(error_msg, stream=log)
4657
output.write_line(formatted, stream=log)
58+
output.write('\n```\n', stream=log)
4759
raise SystemExit(1)
4860

4961

tests/error_handler_test.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,30 @@ def test_log_and_exit(cap_out, mock_store_dir):
104104

105105
printed = cap_out.get()
106106
log_file = os.path.join(mock_store_dir, 'pre-commit.log')
107-
printed_lines = printed.splitlines()
108-
print(printed_lines)
109-
assert len(printed_lines) == 7
110-
assert printed_lines[0] == '### version information'
111-
assert re.match(r'^pre-commit.version=\d+\.\d+\.\d+$', printed_lines[1])
112-
assert printed_lines[2].startswith('sys.version=')
113-
assert printed_lines[3].startswith('sys.executable=')
114-
assert printed_lines[4] == '### error information'
115-
assert printed_lines[5] == 'msg: FatalError: hai'
116-
assert printed_lines[6] == 'Check the log at {}'.format(log_file)
107+
assert printed == (
108+
'msg: FatalError: hai\n' 'Check the log at {}\n'.format(log_file)
109+
)
117110

118111
assert os.path.exists(log_file)
119112
with io.open(log_file) as f:
120-
logged_lines = f.read().splitlines()
121-
assert len(logged_lines) == 7
122-
assert printed_lines[0] == '### version information'
123-
assert re.match(
124-
r'^pre-commit.version=\d+\.\d+\.\d+$',
125-
printed_lines[1],
113+
logged = f.read()
114+
expected = (
115+
r'^### version information\n'
116+
r'```\n'
117+
r'pre-commit.version: \d+\.\d+\.\d+\n'
118+
r'sys.version: (.*\n)*'
119+
r'sys.executable: .*\n'
120+
r'os.name: .*\n'
121+
r'sys.platform: .*\n'
122+
r'```\n'
123+
r'### error information\n'
124+
r'```\n'
125+
r'msg: FatalError: hai\n'
126+
r"I'm a stacktrace\n"
127+
r'\n'
128+
r'```\n'
126129
)
127-
assert logged_lines[2].startswith('sys.version=')
128-
assert logged_lines[3].startswith('sys.executable=')
129-
assert logged_lines[5] == 'msg: FatalError: hai'
130-
assert logged_lines[6] == "I'm a stacktrace"
130+
assert re.match(expected, logged)
131131

132132

133133
def test_error_handler_non_ascii_exception(mock_store_dir):
@@ -139,7 +139,8 @@ def test_error_handler_non_ascii_exception(mock_store_dir):
139139
def test_error_handler_no_tty(tempdir_factory):
140140
pre_commit_home = tempdir_factory.get()
141141
output = cmd_output_mocked_pre_commit_home(
142-
sys.executable, '-c',
142+
sys.executable,
143+
'-c',
143144
'from __future__ import unicode_literals\n'
144145
'from pre_commit.error_handler import error_handler\n'
145146
'with error_handler():\n'

0 commit comments

Comments
 (0)