summaryrefslogtreecommitdiffstats
path: root/tests/error_handler_test.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/error_handler_test.py')
-rw-r--r--tests/error_handler_test.py47
1 files changed, 29 insertions, 18 deletions
diff --git a/tests/error_handler_test.py b/tests/error_handler_test.py
index d066e57..6b0bb86 100644
--- a/tests/error_handler_test.py
+++ b/tests/error_handler_test.py
@@ -1,12 +1,13 @@
import os.path
-import re
import stat
import sys
from unittest import mock
import pytest
+import re_assert
from pre_commit import error_handler
+from pre_commit.errors import FatalError
from pre_commit.store import Store
from pre_commit.util import CalledProcessError
from testing.util import cmd_output_mocked_pre_commit_home
@@ -26,27 +27,28 @@ def test_error_handler_no_exception(mocked_log_and_exit):
def test_error_handler_fatal_error(mocked_log_and_exit):
- exc = error_handler.FatalError('just a test')
+ exc = FatalError('just a test')
with error_handler.error_handler():
raise exc
mocked_log_and_exit.assert_called_once_with(
'An error has occurred',
+ 1,
exc,
# Tested below
mock.ANY,
)
- assert re.match(
+ pattern = re_assert.Matches(
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],
+ r'(pre_commit\.errors\.)?FatalError: just a test\n',
)
+ pattern.assert_matches(mocked_log_and_exit.call_args[0][3])
def test_error_handler_uncaught_error(mocked_log_and_exit):
@@ -56,11 +58,12 @@ def test_error_handler_uncaught_error(mocked_log_and_exit):
mocked_log_and_exit.assert_called_once_with(
'An unexpected error has occurred',
+ 3,
exc,
# Tested below
mock.ANY,
)
- assert re.match(
+ pattern = re_assert.Matches(
r'Traceback \(most recent call last\):\n'
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
r' yield\n'
@@ -68,8 +71,8 @@ def test_error_handler_uncaught_error(mocked_log_and_exit):
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],
)
+ pattern.assert_matches(mocked_log_and_exit.call_args[0][3])
def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
@@ -79,11 +82,12 @@ def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
mocked_log_and_exit.assert_called_once_with(
'Interrupted (^C)',
+ 130,
exc,
# Tested below
mock.ANY,
)
- assert re.match(
+ pattern = re_assert.Matches(
r'Traceback \(most recent call last\):\n'
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
r' yield\n'
@@ -91,15 +95,20 @@ def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
r'in test_error_handler_keyboardinterrupt\n'
r' raise exc\n'
r'KeyboardInterrupt\n',
- mocked_log_and_exit.call_args[0][2],
)
+ pattern.assert_matches(mocked_log_and_exit.call_args[0][3])
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",
- )
+ tb = (
+ 'Traceback (most recent call last):\n'
+ ' File "<stdin>", line 2, in <module>\n'
+ 'pre_commit.errors.FatalError: hai\n'
+ )
+
+ with pytest.raises(SystemExit) as excinfo:
+ error_handler._log_and_exit('msg', 1, FatalError('hai'), tb)
+ assert excinfo.value.code == 1
printed = cap_out.get()
log_file = os.path.join(mock_store_dir, 'pre-commit.log')
@@ -108,7 +117,7 @@ def test_log_and_exit(cap_out, mock_store_dir):
assert os.path.exists(log_file)
with open(log_file) as f:
logged = f.read()
- expected = (
+ pattern = re_assert.Matches(
r'^### version information\n'
r'\n'
r'```\n'
@@ -127,10 +136,12 @@ def test_log_and_exit(cap_out, mock_store_dir):
r'```\n'
r'\n'
r'```\n'
- r"I'm a stacktrace\n"
- r'```\n'
+ r'Traceback \(most recent call last\):\n'
+ r' File "<stdin>", line 2, in <module>\n'
+ r'pre_commit\.errors\.FatalError: hai\n'
+ r'```\n',
)
- assert re.match(expected, logged)
+ pattern.assert_matches(logged)
def test_error_handler_non_ascii_exception(mock_store_dir):
@@ -163,7 +174,7 @@ def test_error_handler_no_tty(tempdir_factory):
'from pre_commit.error_handler import error_handler\n'
'with error_handler():\n'
' raise ValueError("\\u2603")\n',
- retcode=1,
+ retcode=3,
tempdir_factory=tempdir_factory,
pre_commit_home=pre_commit_home,
)