|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""The generated ``register`` overrides stay loud inside a quiet block. |
| 3 | +
|
| 4 | +GitHub issue #1916. :meth:`~pcapkit.corekit.enum.EnumRegistry.register` runs |
| 5 | +:meth:`~pcapkit.corekit.enum.EnumLookup._validate_value` inside a |
| 6 | +:class:`~pcapkit.utilities.exceptions._CaughtByCaller` block declaring that |
| 7 | +nothing is caught, so its rejection is logged even when an enclosing ``get`` |
| 8 | +has quieted :exc:`ValueError` (#1845). The overrides on |
| 9 | +:class:`~pcapkit.const.reg.apptype.apptype.AppType` and |
| 10 | +:class:`~pcapkit.const.pcapng.option_type.OptionType` called the hook bare, so |
| 11 | +inside such a block their rejection raised and logged nothing. |
| 12 | +
|
| 13 | +Each test opens the quiet block the way ``get`` does and checks that the |
| 14 | +rejection still leaves its record on the ``pcapkit`` logger. The record comes |
| 15 | +from :mod:`pcapkit.utilities.exceptions`'s own logger, a child of that one. |
| 16 | +
|
| 17 | +This module is unit-tier and reads no capture. |
| 18 | +
|
| 19 | +""" |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import logging |
| 23 | +import sys |
| 24 | +import threading |
| 25 | +import unittest |
| 26 | +from typing import TYPE_CHECKING |
| 27 | + |
| 28 | +from tests.utilities._harness import capture |
| 29 | + |
| 30 | +if TYPE_CHECKING: |
| 31 | + from typing import Callable |
| 32 | + |
| 33 | + |
| 34 | +class RegisterLoudInQuietBlock1916Tests(unittest.TestCase): |
| 35 | + """``register`` on a generated registry logs its rejection inside a quiet block.""" |
| 36 | + |
| 37 | + def setUp(self) -> 'None': |
| 38 | + # A loud error outside development mode replaces both hooks; put the |
| 39 | + # real ones back so this test does not leak pcapkit's into later ones. |
| 40 | + saved_sys, saved_threading = sys.excepthook, threading.excepthook |
| 41 | + |
| 42 | + def _restore() -> 'None': |
| 43 | + sys.excepthook = saved_sys |
| 44 | + threading.excepthook = saved_threading |
| 45 | + |
| 46 | + self.addCleanup(_restore) |
| 47 | + |
| 48 | + def assertLoudInsideQuietBlock(self, registry: 'type', call: 'Callable[[], object]', |
| 49 | + message: 'str') -> 'None': |
| 50 | + """Assert ``call`` raises ``message`` and logs it, inside a quiet block.""" |
| 51 | + from pcapkit.utilities.exceptions import EnumValueError, _CaughtByCaller |
| 52 | + |
| 53 | + members = dict(registry.__members__) # type: ignore[attr-defined] |
| 54 | + with capture(logging.getLogger('pcapkit')) as package: |
| 55 | + with _CaughtByCaller(ValueError): |
| 56 | + with self.assertRaises(EnumValueError) as caught: |
| 57 | + call() |
| 58 | + self.assertEqual(str(caught.exception), message) |
| 59 | + self.assertEqual(package.messages, [('CRITICAL', f'EnumValueError: {message}')], |
| 60 | + 'the rejection was quieted by the enclosing block') |
| 61 | + self.assertEqual(dict(registry.__members__), members) # type: ignore[attr-defined] |
| 62 | + |
| 63 | + def test_apptype_register(self) -> 'None': |
| 64 | + from pcapkit.const.reg.apptype import TCP |
| 65 | + |
| 66 | + self.assertLoudInsideQuietBlock(TCP, lambda: TCP.register(70000, 'x'), |
| 67 | + '70000 is not a valid TCP') |
| 68 | + |
| 69 | + def test_option_type_register(self) -> 'None': |
| 70 | + from pcapkit.const.pcapng.option_type import OptionType |
| 71 | + |
| 72 | + self.assertLoudInsideQuietBlock(OptionType, lambda: OptionType.register(-1, 'x'), |
| 73 | + '-1 is not a valid OptionType') |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + unittest.main() |
0 commit comments