|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +import io |
| 5 | +import os.path |
| 6 | +import mock |
| 7 | +import pytest |
| 8 | +import re |
| 9 | + |
| 10 | +from pre_commit import error_handler |
| 11 | +from pre_commit.errors import FatalError |
| 12 | + |
| 13 | + |
| 14 | +@pytest.yield_fixture |
| 15 | +def mocked_log_and_exit(): |
| 16 | + with mock.patch.object(error_handler, '_log_and_exit') as log_and_exit: |
| 17 | + yield log_and_exit |
| 18 | + |
| 19 | + |
| 20 | +def test_error_handler_no_exception(mocked_log_and_exit): |
| 21 | + with error_handler.error_handler(): |
| 22 | + pass |
| 23 | + assert mocked_log_and_exit.call_count == 0 |
| 24 | + |
| 25 | + |
| 26 | +def test_error_handler_fatal_error(mocked_log_and_exit): |
| 27 | + exc = FatalError('just a test') |
| 28 | + with error_handler.error_handler(): |
| 29 | + raise exc |
| 30 | + |
| 31 | + mocked_log_and_exit.assert_called_once_with( |
| 32 | + 'An error has occurred', |
| 33 | + exc, |
| 34 | + # Tested below |
| 35 | + mock.ANY, |
| 36 | + ) |
| 37 | + |
| 38 | + assert re.match( |
| 39 | + 'Traceback \(most recent call last\):\n' |
| 40 | + ' File ".+/pre_commit/error_handler.py", line \d+, in error_handler\n' |
| 41 | + ' yield\n' |
| 42 | + ' File ".+/tests/error_handler_test.py", line \d+, ' |
| 43 | + 'in test_error_handler_fatal_error\n' |
| 44 | + ' raise exc\n' |
| 45 | + '(pre_commit\.errors\.)?FatalError: just a test\n', |
| 46 | + mocked_log_and_exit.call_args[0][2], |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def test_error_handler_uncaught_error(mocked_log_and_exit): |
| 51 | + exc = ValueError('another test') |
| 52 | + with error_handler.error_handler(): |
| 53 | + raise exc |
| 54 | + |
| 55 | + mocked_log_and_exit.assert_called_once_with( |
| 56 | + 'An unexpected error has occurred', |
| 57 | + exc, |
| 58 | + # Tested below |
| 59 | + mock.ANY, |
| 60 | + ) |
| 61 | + assert re.match( |
| 62 | + 'Traceback \(most recent call last\):\n' |
| 63 | + ' File ".+/pre_commit/error_handler.py", line \d+, in error_handler\n' |
| 64 | + ' yield\n' |
| 65 | + ' File ".+/tests/error_handler_test.py", line \d+, ' |
| 66 | + 'in test_error_handler_uncaught_error\n' |
| 67 | + ' raise exc\n' |
| 68 | + 'ValueError: another test\n', |
| 69 | + mocked_log_and_exit.call_args[0][2], |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def test_log_and_exit(mock_out_store_directory): |
| 74 | + mocked_print = mock.Mock() |
| 75 | + with pytest.raises(error_handler.PreCommitSystemExit): |
| 76 | + error_handler._log_and_exit( |
| 77 | + 'msg', FatalError('hai'), "I'm a stacktrace", |
| 78 | + print_fn=mocked_print, |
| 79 | + ) |
| 80 | + |
| 81 | + printed = '\n'.join(call[0][0] for call in mocked_print.call_args_list) |
| 82 | + assert printed == ( |
| 83 | + 'msg: FatalError: hai\n' |
| 84 | + 'Check the log at ~/.pre-commit/pre-commit.log' |
| 85 | + ) |
| 86 | + |
| 87 | + log_file = os.path.join(mock_out_store_directory, 'pre-commit.log') |
| 88 | + assert os.path.exists(log_file) |
| 89 | + contents = io.open(log_file).read() |
| 90 | + assert contents == ( |
| 91 | + 'msg: FatalError: hai\n' |
| 92 | + "I'm a stacktrace\n" |
| 93 | + ) |
0 commit comments