mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Fix printing of non-ascii in error handler
This commit is contained in:
parent
6b005cff0d
commit
2463738af4
2 changed files with 38 additions and 11 deletions
|
|
@ -7,7 +7,9 @@ import io
|
|||
import os.path
|
||||
import traceback
|
||||
|
||||
from pre_commit import five
|
||||
from pre_commit.errors import FatalError
|
||||
from pre_commit.output import sys_stdout_write_wrapper
|
||||
from pre_commit.store import Store
|
||||
|
||||
|
||||
|
|
@ -16,15 +18,15 @@ class PreCommitSystemExit(SystemExit):
|
|||
pass
|
||||
|
||||
|
||||
def _log_and_exit(msg, exc, formatted, print_fn=print):
|
||||
error_msg = '{0}: {1}: {2}'.format(msg, type(exc).__name__, exc)
|
||||
print_fn(error_msg)
|
||||
print_fn('Check the log at ~/.pre-commit/pre-commit.log')
|
||||
def _log_and_exit(msg, exc, formatted, write_fn=sys_stdout_write_wrapper):
|
||||
error_msg = '{0}: {1}: {2}\n'.format(msg, type(exc).__name__, exc)
|
||||
write_fn(error_msg)
|
||||
write_fn('Check the log at ~/.pre-commit/pre-commit.log\n')
|
||||
store = Store()
|
||||
store.require_created()
|
||||
with io.open(os.path.join(store.directory, 'pre-commit.log'), 'w') as log:
|
||||
log.write(error_msg + '\n')
|
||||
log.write(formatted + '\n')
|
||||
with io.open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
|
||||
log.write(five.to_bytes(error_msg))
|
||||
log.write(five.to_bytes(formatted) + b'\n')
|
||||
raise PreCommitSystemExit(1)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,18 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import io
|
||||
import os.path
|
||||
import re
|
||||
import sys
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from pre_commit import error_handler
|
||||
from pre_commit.errors import FatalError
|
||||
from pre_commit.util import cmd_output
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
|
|
@ -72,17 +75,17 @@ def test_error_handler_uncaught_error(mocked_log_and_exit):
|
|||
|
||||
|
||||
def test_log_and_exit(mock_out_store_directory):
|
||||
mocked_print = mock.Mock()
|
||||
mocked_write = mock.Mock()
|
||||
with pytest.raises(error_handler.PreCommitSystemExit):
|
||||
error_handler._log_and_exit(
|
||||
'msg', FatalError('hai'), "I'm a stacktrace",
|
||||
print_fn=mocked_print,
|
||||
write_fn=mocked_write,
|
||||
)
|
||||
|
||||
printed = '\n'.join(call[0][0] for call in mocked_print.call_args_list)
|
||||
printed = ''.join(call[0][0] for call in mocked_write.call_args_list)
|
||||
assert printed == (
|
||||
'msg: FatalError: hai\n'
|
||||
'Check the log at ~/.pre-commit/pre-commit.log'
|
||||
'Check the log at ~/.pre-commit/pre-commit.log\n'
|
||||
)
|
||||
|
||||
log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
|
||||
|
|
@ -92,3 +95,25 @@ def test_log_and_exit(mock_out_store_directory):
|
|||
'msg: FatalError: hai\n'
|
||||
"I'm a stacktrace\n"
|
||||
)
|
||||
|
||||
|
||||
def test_error_handler_non_ascii_exception(mock_out_store_directory):
|
||||
with pytest.raises(error_handler.PreCommitSystemExit):
|
||||
with error_handler.error_handler():
|
||||
raise ValueError('☃')
|
||||
|
||||
|
||||
def test_error_handler_no_tty(tempdir_factory):
|
||||
output = cmd_output(
|
||||
sys.executable, '-c',
|
||||
'from __future__ import unicode_literals\n'
|
||||
'from pre_commit.error_handler import error_handler\n'
|
||||
'with error_handler():\n'
|
||||
' raise ValueError("\\u2603")\n',
|
||||
env=dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get()),
|
||||
retcode=1,
|
||||
)
|
||||
assert output[1].replace('\r', '') == (
|
||||
'An unexpected error has occurred: ValueError: ☃\n'
|
||||
'Check the log at ~/.pre-commit/pre-commit.log\n'
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue