forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handler_test.py
More file actions
163 lines (138 loc) · 4.69 KB
/
error_handler_test.py
File metadata and controls
163 lines (138 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- 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 testing.util import cmd_output_mocked_pre_commit_home
@pytest.fixture
def mocked_log_and_exit():
with mock.patch.object(error_handler, '_log_and_exit') as log_and_exit:
yield log_and_exit
def test_error_handler_no_exception(mocked_log_and_exit):
with error_handler.error_handler():
pass
assert mocked_log_and_exit.call_count == 0
def test_error_handler_fatal_error(mocked_log_and_exit):
exc = error_handler.FatalError('just a test')
with error_handler.error_handler():
raise exc
mocked_log_and_exit.assert_called_once_with(
'An error has occurred',
exc,
# Tested below
mock.ANY,
)
assert re.match(
r'Traceback \(most recent call last\):\n'
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
r' yield\n'
r' File ".+tests.error_handler_test.py", line \d+, '
r'in test_error_handler_fatal_error\n'
r' raise exc\n'
r'(pre_commit\.error_handler\.)?FatalError: just a test\n',
mocked_log_and_exit.call_args[0][2],
)
def test_error_handler_uncaught_error(mocked_log_and_exit):
exc = ValueError('another test')
with error_handler.error_handler():
raise exc
mocked_log_and_exit.assert_called_once_with(
'An unexpected error has occurred',
exc,
# Tested below
mock.ANY,
)
assert re.match(
r'Traceback \(most recent call last\):\n'
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
r' yield\n'
r' File ".+tests.error_handler_test.py", line \d+, '
r'in test_error_handler_uncaught_error\n'
r' raise exc\n'
r'ValueError: another test\n',
mocked_log_and_exit.call_args[0][2],
)
def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
exc = KeyboardInterrupt()
with error_handler.error_handler():
raise exc
mocked_log_and_exit.assert_called_once_with(
'Interrupted (^C)',
exc,
# Tested below
mock.ANY,
)
assert re.match(
r'Traceback \(most recent call last\):\n'
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
r' yield\n'
r' File ".+tests.error_handler_test.py", line \d+, '
r'in test_error_handler_keyboardinterrupt\n'
r' raise exc\n'
r'KeyboardInterrupt\n',
mocked_log_and_exit.call_args[0][2],
)
def test_log_and_exit(cap_out, mock_store_dir):
with pytest.raises(SystemExit):
error_handler._log_and_exit(
'msg', error_handler.FatalError('hai'), "I'm a stacktrace",
)
printed = cap_out.get()
log_file = os.path.join(mock_store_dir, 'pre-commit.log')
assert printed == (
'msg: FatalError: hai\n' 'Check the log at {}\n'.format(log_file)
)
assert os.path.exists(log_file)
with io.open(log_file) as f:
logged = f.read()
expected = (
r'^### version information\n'
r'\n'
r'```\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'```\n'
r'\n'
r'```\n'
r"I'm a stacktrace\n"
r'```\n'
)
assert re.match(expected, logged)
def test_error_handler_non_ascii_exception(mock_store_dir):
with pytest.raises(SystemExit):
with error_handler.error_handler():
raise ValueError('☃')
def test_error_handler_no_tty(tempdir_factory):
pre_commit_home = tempdir_factory.get()
output = cmd_output_mocked_pre_commit_home(
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',
retcode=1,
tempdir_factory=tempdir_factory,
pre_commit_home=pre_commit_home,
)
log_file = os.path.join(pre_commit_home, 'pre-commit.log')
output_lines = output[1].replace('\r', '').splitlines()
assert (
output_lines[-2] == 'An unexpected error has occurred: ValueError: ☃'
)
assert output_lines[-1] == 'Check the log at {}'.format(log_file)