Fix up some newlines in output

This commit is contained in:
Anthony Sottile 2019-09-24 09:32:10 -07:00
parent cfc4910068
commit 795506a486
4 changed files with 39 additions and 36 deletions

View file

@ -30,42 +30,39 @@ def _log_and_exit(msg, exc, formatted):
error_msg = b''.join(( error_msg = b''.join((
five.to_bytes(msg), b': ', five.to_bytes(msg), b': ',
five.to_bytes(type(exc).__name__), 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() store = Store()
log_path = os.path.join(store.directory, 'pre-commit.log') log_path = os.path.join(store.directory, 'pre-commit.log')
output.write_line('Check the log at {}'.format(log_path)) output.write_line('Check the log at {}'.format(log_path))
with open(log_path, 'wb') as log: with open(log_path, 'wb') as log:
output.write_line( def _log_line(*s): # type: (*str) -> None
'### version information\n```', stream=log, output.write_line(*s, stream=log)
)
output.write_line( _log_line('### version information')
'pre-commit.version: {}'.format(C.VERSION), stream=log, _log_line()
) _log_line('```')
output.write_line( _log_line('pre-commit version: {}'.format(C.VERSION))
'sys.version:\n{}'.format( _log_line('sys.version:')
'\n'.join( for line in sys.version.splitlines():
[ _log_line(' {}'.format(line))
' {}'.format(line) _log_line('sys.executable: {}'.format(sys.executable))
for line in sys.version.splitlines() _log_line('os.name: {}'.format(os.name))
], _log_line('sys.platform: {}'.format(sys.platform))
), _log_line('```')
), _log_line()
stream=log,
) _log_line('### error information')
output.write_line( _log_line()
'sys.executable: {}'.format(sys.executable), stream=log, _log_line('```')
) _log_line(error_msg)
output.write_line('os.name: {}'.format(os.name), stream=log) _log_line('```')
output.write_line( _log_line()
'sys.platform: {}\n```'.format(sys.platform), stream=log, _log_line('```')
) _log_line(formatted)
output.write_line('### error information\n```', stream=log) _log_line('```')
output.write(error_msg, stream=log)
output.write_line(formatted, stream=log)
output.write('\n```\n', stream=log)
raise SystemExit(1) raise SystemExit(1)

View file

@ -103,7 +103,7 @@ class CalledProcessError(RuntimeError):
), ),
), ),
b'Output: ', output[0], b'\n', b'Output: ', output[0], b'\n',
b'Errors: ', output[1], b'\n', b'Errors: ', output[1],
)) ))
def to_text(self): def to_text(self):

View file

@ -113,19 +113,25 @@ def test_log_and_exit(cap_out, mock_store_dir):
logged = f.read() logged = f.read()
expected = ( expected = (
r'^### version information\n' r'^### version information\n'
r'\n'
r'```\n' r'```\n'
r'pre-commit.version: \d+\.\d+\.\d+\n' r'pre-commit version: \d+\.\d+\.\d+\n'
r'sys.version:\n( .*\n)*' r'sys.version:\n'
r'( .*\n)*'
r'sys.executable: .*\n' r'sys.executable: .*\n'
r'os.name: .*\n' r'os.name: .*\n'
r'sys.platform: .*\n' r'sys.platform: .*\n'
r'```\n' r'```\n'
r'\n'
r'### error information\n' r'### error information\n'
r'\n'
r'```\n' r'```\n'
r'msg: FatalError: hai\n' r'msg: FatalError: hai\n'
r"I'm a stacktrace\n" r'```\n'
r'\n' r'\n'
r'```\n' r'```\n'
r"I'm a stacktrace\n"
r'```\n'
) )
assert re.match(expected, logged) assert re.match(expected, logged)

View file

@ -24,7 +24,7 @@ def test_CalledProcessError_str():
'Output: \n' 'Output: \n'
' stdout\n' ' stdout\n'
'Errors: \n' 'Errors: \n'
' stderr\n' ' stderr'
) )
@ -37,7 +37,7 @@ def test_CalledProcessError_str_nooutput():
'Return code: 1\n' 'Return code: 1\n'
'Expected return code: 0\n' 'Expected return code: 0\n'
'Output: (none)\n' 'Output: (none)\n'
'Errors: (none)\n' 'Errors: (none)'
) )