<commit_before>"""Tests for letsencrypt.error_handler."""
import signal
import sys
import unittest

import mock


class ErrorHandlerTest(unittest.TestCase):
    """Tests for letsencrypt.error_handler."""

    def setUp(self):
        from letsencrypt import error_handler

        self.init_func = mock.MagicMock()
        self.handler = error_handler.ErrorHandler(self.init_func)
        # pylint: disable=protected-access
        self.signals = error_handler._SIGNALS

    def test_context_manager(self):
        try:
            with self.handler:
                raise ValueError
        except ValueError:
            pass
        self.init_func.assert_called_once_with()

    @mock.patch('letsencrypt.error_handler.os')
    @mock.patch('letsencrypt.error_handler.signal')
    def test_signal_handler(self, mock_signal, mock_os):
        # pylint: disable=protected-access
        mock_signal.getsignal.return_value = signal.SIG_DFL
        self.handler.set_signal_handlers()
        signal_handler = self.handler._signal_handler
        for signum in self.signals:
            mock_signal.signal.assert_any_call(signum, signal_handler)

        signum = self.signals[0]
        signal_handler(signum, None)
        self.init_func.assert_called_once_with()
        mock_os.kill.assert_called_once_with(mock_os.getpid(), signum)

        self.handler.reset_signal_handlers()
        for signum in self.signals:
            mock_signal.signal.assert_any_call(signum, signal.SIG_DFL)

    def test_bad_recovery(self):
        bad_func = mock.MagicMock(side_effect=[ValueError])
        self.handler.register(bad_func)
        self.handler.call_registered()
        self.init_func.assert_called_once_with()
        bad_func.assert_called_once_with()

    def test_sysexit_ignored(self):
        try:
            with self.handler:
                sys.exit(0)
        except SystemExit:
            pass
        self.assertFalse(self.init_func.called)


if __name__ == "__main__":
    unittest.main()  # pragma: no cover
<commit_msg>Updated error_handler tests<commit_after>"""Tests for letsencrypt.error_handler."""
import signal
import sys
import unittest

import mock


class ErrorHandlerTest(unittest.TestCase):
    """Tests for letsencrypt.error_handler."""

    def setUp(self):
        from letsencrypt import error_handler

        self.init_func = mock.MagicMock()
        self.init_args = {42}
        self.init_kwargs = {'foo': 'bar'}
        self.handler = error_handler.ErrorHandler(self.init_func,
                                                  *self.init_args,
                                                  **self.init_kwargs)
        # pylint: disable=protected-access
        self.signals = error_handler._SIGNALS

    def test_context_manager(self):
        try:
            with self.handler:
                raise ValueError
        except ValueError:
            pass
        self.init_func.assert_called_once_with(*self.init_args,
                                               **self.init_kwargs)

    @mock.patch('letsencrypt.error_handler.os')
    @mock.patch('letsencrypt.error_handler.signal')
    def test_signal_handler(self, mock_signal, mock_os):
        # pylint: disable=protected-access
        mock_signal.getsignal.return_value = signal.SIG_DFL
        self.handler.set_signal_handlers()
        signal_handler = self.handler._signal_handler
        for signum in self.signals:
            mock_signal.signal.assert_any_call(signum, signal_handler)

        signum = self.signals[0]
        signal_handler(signum, None)
        self.init_func.assert_called_once_with(*self.init_args,
                                               **self.init_kwargs)
        mock_os.kill.assert_called_once_with(mock_os.getpid(), signum)

        self.handler.reset_signal_handlers()
        for signum in self.signals:
            mock_signal.signal.assert_any_call(signum, signal.SIG_DFL)

    def test_bad_recovery(self):
        bad_func = mock.MagicMock(side_effect=[ValueError])
        self.handler.register(bad_func)
        self.handler.call_registered()
        self.init_func.assert_called_once_with(*self.init_args,
                                               **self.init_kwargs)
        bad_func.assert_called_once_with()

    def test_sysexit_ignored(self):
        try:
            with self.handler:
                sys.exit(0)
        except SystemExit:
            pass
        self.assertFalse(self.init_func.called)


if __name__ == "__main__":
    unittest.main()  # pragma: no cover

{'fraction_non_alphanumeric': 0.08732083792723264, 'fraction_numerical': 0.001323042998897464, 'mean_word_length': 2.335294117647059, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '236674', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1297, 'n_words': 210}