Merge pull request #310 from pre-commit/fix_printing_of_non_ascii_in_error_handler

Fix printing of non-ascii in error handler
This commit is contained in:
Anthony Sottile 2015-12-01 14:16:53 -08:00
commit c8f0da00b8
2 changed files with 38 additions and 11 deletions

View file

@ -7,7 +7,9 @@ import io
import os.path import os.path
import traceback import traceback
from pre_commit import five
from pre_commit.errors import FatalError from pre_commit.errors import FatalError
from pre_commit.output import sys_stdout_write_wrapper
from pre_commit.store import Store from pre_commit.store import Store
@ -16,15 +18,15 @@ class PreCommitSystemExit(SystemExit):
pass pass
def _log_and_exit(msg, exc, formatted, print_fn=print): def _log_and_exit(msg, exc, formatted, write_fn=sys_stdout_write_wrapper):
error_msg = '{0}: {1}: {2}'.format(msg, type(exc).__name__, exc) error_msg = '{0}: {1}: {2}\n'.format(msg, type(exc).__name__, exc)
print_fn(error_msg) write_fn(error_msg)
print_fn('Check the log at ~/.pre-commit/pre-commit.log') write_fn('Check the log at ~/.pre-commit/pre-commit.log\n')
store = Store() store = Store()
store.require_created() store.require_created()
with io.open(os.path.join(store.directory, 'pre-commit.log'), 'w') as log: with io.open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
log.write(error_msg + '\n') log.write(five.to_bytes(error_msg))
log.write(formatted + '\n') log.write(five.to_bytes(formatted) + b'\n')
raise PreCommitSystemExit(1) raise PreCommitSystemExit(1)

View file

@ -1,15 +1,18 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import io import io
import os.path import os.path
import re import re
import sys
import mock import mock
import pytest import pytest
from pre_commit import error_handler from pre_commit import error_handler
from pre_commit.errors import FatalError from pre_commit.errors import FatalError
from pre_commit.util import cmd_output
@pytest.yield_fixture @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): def test_log_and_exit(mock_out_store_directory):
mocked_print = mock.Mock() mocked_write = mock.Mock()
with pytest.raises(error_handler.PreCommitSystemExit): with pytest.raises(error_handler.PreCommitSystemExit):
error_handler._log_and_exit( error_handler._log_and_exit(
'msg', FatalError('hai'), "I'm a stacktrace", '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 == ( assert printed == (
'msg: FatalError: hai\n' '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') 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' 'msg: FatalError: hai\n'
"I'm a stacktrace\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'
)