1+ # -*- coding: utf-8 -*-
12from __future__ import absolute_import
23from __future__ import unicode_literals
34
45import io
56import os .path
67import re
8+ import sys
79
810import mock
911import pytest
1012
1113from pre_commit import error_handler
1214from pre_commit .errors import FatalError
15+ from pre_commit .util import cmd_output
1316
1417
1518@pytest .yield_fixture
@@ -72,17 +75,17 @@ def test_error_handler_uncaught_error(mocked_log_and_exit):
7275
7376
7477def test_log_and_exit (mock_out_store_directory ):
75- mocked_print = mock .Mock ()
78+ mocked_write = mock .Mock ()
7679 with pytest .raises (error_handler .PreCommitSystemExit ):
7780 error_handler ._log_and_exit (
7881 'msg' , FatalError ('hai' ), "I'm a stacktrace" ,
79- print_fn = mocked_print ,
82+ write_fn = mocked_write ,
8083 )
8184
82- printed = '\n ' .join (call [0 ][0 ] for call in mocked_print .call_args_list )
85+ printed = '' .join (call [0 ][0 ] for call in mocked_write .call_args_list )
8386 assert printed == (
8487 'msg: FatalError: hai\n '
85- 'Check the log at ~/.pre-commit/pre-commit.log'
88+ 'Check the log at ~/.pre-commit/pre-commit.log\n '
8689 )
8790
8891 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):
9295 'msg: FatalError: hai\n '
9396 "I'm a stacktrace\n "
9497 )
98+
99+
100+ def test_error_handler_non_ascii_exception (mock_out_store_directory ):
101+ with pytest .raises (error_handler .PreCommitSystemExit ):
102+ with error_handler .error_handler ():
103+ raise ValueError ('☃' )
104+
105+
106+ def test_error_handler_no_tty (tempdir_factory ):
107+ output = cmd_output (
108+ sys .executable , '-c' ,
109+ 'from __future__ import unicode_literals\n '
110+ 'from pre_commit.error_handler import error_handler\n '
111+ 'with error_handler():\n '
112+ ' raise ValueError("\\ u2603")\n ' ,
113+ env = dict (os .environ , PRE_COMMIT_HOME = tempdir_factory .get ()),
114+ retcode = 1 ,
115+ )
116+ assert output [1 ].replace ('\r ' , '' ) == (
117+ 'An unexpected error has occurred: ValueError: ☃\n '
118+ 'Check the log at ~/.pre-commit/pre-commit.log\n '
119+ )
0 commit comments