diff --git a/pre_commit/error_handler.py b/pre_commit/error_handler.py index 6b6f8edf..d723aa6e 100644 --- a/pre_commit/error_handler.py +++ b/pre_commit/error_handler.py @@ -28,11 +28,6 @@ def _to_bytes(exc): def _log_and_exit(msg, exc, formatted): error_msg = b''.join(( - _to_bytes('### version information\n'), - _to_bytes('pre-commit.version={}\n'.format(C.VERSION)), - _to_bytes('sys.version={}\n'.format(sys.version.replace('\n', ' '))), - _to_bytes('sys.executable={}\n'.format(sys.executable)), - _to_bytes('### error information\n'), five.to_bytes(msg), b': ', five.to_bytes(type(exc).__name__), b': ', _to_bytes(exc), b'\n', @@ -41,9 +36,26 @@ def _log_and_exit(msg, exc, formatted): store = Store() log_path = os.path.join(store.directory, 'pre-commit.log') output.write_line('Check the log at {}'.format(log_path)) + + meta_info_msg = '### version information\n```\n' + meta_info_msg += 'pre-commit.version: {}\n'.format(C.VERSION) + meta_info_msg += 'sys.version: \n{}\n'.format( + '\n'.join( + [ + '\t{}'.format(line) + for line in sys.version.splitlines() + ], + ), + ) + meta_info_msg += 'sys.executable: {}\n'.format(sys.executable) + meta_info_msg += 'os.name: {}\n'.format(os.name) + meta_info_msg += 'sys.platform: {}\n```\n'.format(sys.platform) + meta_info_msg += '### error information\n```\n' with open(log_path, 'wb') as log: + output.write(meta_info_msg, stream=log) output.write(error_msg, stream=log) output.write_line(formatted, stream=log) + output.write('\n```\n', stream=log) raise SystemExit(1) diff --git a/tests/error_handler_test.py b/tests/error_handler_test.py index e6820936..99edfdb3 100644 --- a/tests/error_handler_test.py +++ b/tests/error_handler_test.py @@ -104,30 +104,30 @@ def test_log_and_exit(cap_out, mock_store_dir): printed = cap_out.get() log_file = os.path.join(mock_store_dir, 'pre-commit.log') - printed_lines = printed.splitlines() - print(printed_lines) - assert len(printed_lines) == 7 - assert printed_lines[0] == '### version information' - assert re.match(r'^pre-commit.version=\d+\.\d+\.\d+$', printed_lines[1]) - assert printed_lines[2].startswith('sys.version=') - assert printed_lines[3].startswith('sys.executable=') - assert printed_lines[4] == '### error information' - assert printed_lines[5] == 'msg: FatalError: hai' - assert printed_lines[6] == 'Check the log at {}'.format(log_file) + assert printed == ( + 'msg: FatalError: hai\n' 'Check the log at {}\n'.format(log_file) + ) assert os.path.exists(log_file) with io.open(log_file) as f: - logged_lines = f.read().splitlines() - assert len(logged_lines) == 7 - assert printed_lines[0] == '### version information' - assert re.match( - r'^pre-commit.version=\d+\.\d+\.\d+$', - printed_lines[1], + logged = f.read() + expected = ( + r'^### version information\n' + r'```\n' + r'pre-commit.version: \d+\.\d+\.\d+\n' + r'sys.version: (.*\n)*' + r'sys.executable: .*\n' + r'os.name: .*\n' + r'sys.platform: .*\n' + r'```\n' + r'### error information\n' + r'```\n' + r'msg: FatalError: hai\n' + r"I'm a stacktrace\n" + r'\n' + r'```\n' ) - assert logged_lines[2].startswith('sys.version=') - assert logged_lines[3].startswith('sys.executable=') - assert logged_lines[5] == 'msg: FatalError: hai' - assert logged_lines[6] == "I'm a stacktrace" + assert re.match(expected, logged) def test_error_handler_non_ascii_exception(mock_store_dir): @@ -139,7 +139,8 @@ def test_error_handler_non_ascii_exception(mock_store_dir): def test_error_handler_no_tty(tempdir_factory): pre_commit_home = tempdir_factory.get() output = cmd_output_mocked_pre_commit_home( - sys.executable, '-c', + sys.executable, + '-c', 'from __future__ import unicode_literals\n' 'from pre_commit.error_handler import error_handler\n' 'with error_handler():\n'