From 795506a486178fee890cd254045bd040144093f6 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 24 Sep 2019 09:32:10 -0700 Subject: [PATCH] Fix up some newlines in output --- pre_commit/error_handler.py | 57 ++++++++++++++++++------------------- pre_commit/util.py | 2 +- tests/error_handler_test.py | 12 ++++++-- tests/util_test.py | 4 +-- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/pre_commit/error_handler.py b/pre_commit/error_handler.py index 5b09a017..0fa87686 100644 --- a/pre_commit/error_handler.py +++ b/pre_commit/error_handler.py @@ -30,42 +30,39 @@ def _log_and_exit(msg, exc, formatted): error_msg = b''.join(( five.to_bytes(msg), b': ', five.to_bytes(type(exc).__name__), b': ', - _to_bytes(exc), b'\n', + _to_bytes(exc), )) - output.write(error_msg) + output.write_line(error_msg) store = Store() log_path = os.path.join(store.directory, 'pre-commit.log') output.write_line('Check the log at {}'.format(log_path)) with open(log_path, 'wb') as log: - output.write_line( - '### version information\n```', stream=log, - ) - output.write_line( - 'pre-commit.version: {}'.format(C.VERSION), stream=log, - ) - output.write_line( - 'sys.version:\n{}'.format( - '\n'.join( - [ - ' {}'.format(line) - for line in sys.version.splitlines() - ], - ), - ), - stream=log, - ) - output.write_line( - 'sys.executable: {}'.format(sys.executable), stream=log, - ) - output.write_line('os.name: {}'.format(os.name), stream=log) - output.write_line( - 'sys.platform: {}\n```'.format(sys.platform), stream=log, - ) - output.write_line('### error information\n```', stream=log) - output.write(error_msg, stream=log) - output.write_line(formatted, stream=log) - output.write('\n```\n', stream=log) + def _log_line(*s): # type: (*str) -> None + output.write_line(*s, stream=log) + + _log_line('### version information') + _log_line() + _log_line('```') + _log_line('pre-commit version: {}'.format(C.VERSION)) + _log_line('sys.version:') + for line in sys.version.splitlines(): + _log_line(' {}'.format(line)) + _log_line('sys.executable: {}'.format(sys.executable)) + _log_line('os.name: {}'.format(os.name)) + _log_line('sys.platform: {}'.format(sys.platform)) + _log_line('```') + _log_line() + + _log_line('### error information') + _log_line() + _log_line('```') + _log_line(error_msg) + _log_line('```') + _log_line() + _log_line('```') + _log_line(formatted) + _log_line('```') raise SystemExit(1) diff --git a/pre_commit/util.py b/pre_commit/util.py index eb5411fd..5aee0b08 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -103,7 +103,7 @@ class CalledProcessError(RuntimeError): ), ), b'Output: ', output[0], b'\n', - b'Errors: ', output[1], b'\n', + b'Errors: ', output[1], )) def to_text(self): diff --git a/tests/error_handler_test.py b/tests/error_handler_test.py index e94b3206..ff311a24 100644 --- a/tests/error_handler_test.py +++ b/tests/error_handler_test.py @@ -113,19 +113,25 @@ def test_log_and_exit(cap_out, mock_store_dir): logged = f.read() expected = ( r'^### version information\n' + r'\n' r'```\n' - r'pre-commit.version: \d+\.\d+\.\d+\n' - r'sys.version:\n( .*\n)*' + r'pre-commit version: \d+\.\d+\.\d+\n' + r'sys.version:\n' + r'( .*\n)*' r'sys.executable: .*\n' r'os.name: .*\n' r'sys.platform: .*\n' r'```\n' + r'\n' r'### error information\n' + r'\n' r'```\n' r'msg: FatalError: hai\n' - r"I'm a stacktrace\n" + r'```\n' r'\n' r'```\n' + r"I'm a stacktrace\n" + r'```\n' ) assert re.match(expected, logged) diff --git a/tests/util_test.py b/tests/util_test.py index c9838c55..867969c3 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -24,7 +24,7 @@ def test_CalledProcessError_str(): 'Output: \n' ' stdout\n' 'Errors: \n' - ' stderr\n' + ' stderr' ) @@ -37,7 +37,7 @@ def test_CalledProcessError_str_nooutput(): 'Return code: 1\n' 'Expected return code: 0\n' 'Output: (none)\n' - 'Errors: (none)\n' + 'Errors: (none)' )