Repository: pre-commit/pre-commit
Pull request #1148 (2019-09-24 16:35:05)

Fix up some newlines in output


Patch:
diff --git a/pre_commit/error_handler.py b/pre_commit/error_handler.py
index 5b09a01..0fa8768 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 eb5411f..5aee0b0 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):


Test patch:
diff --git a/tests/error_handler_test.py b/tests/error_handler_test.py
index e94b320..ff311a2 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 c9838c5..867969c 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)'
     )