|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""GitHub issue #1364: re-registering a stored module descriptor keeps it. |
| 3 | +
|
| 4 | +``ProtocolBase.register`` and the ``Internet``, ``Link``, ``Transport``, |
| 5 | +``Frame`` and ``PCAPNG`` overrides resolved a |
| 6 | +:class:`~pcapkit.corekit.module.ModuleDescriptor` argument to its class before |
| 7 | +comparing it with the stored entry. Handing back the stored descriptor itself |
| 8 | +therefore warned of an overwrite and replaced the deferred entry with the |
| 9 | +class. The registrars now compare first: the stored entry, or a descriptor |
| 10 | +equal to it, is a silent no-op that keeps the stored object, while a different |
| 11 | +entry still warns. ``Extractor.register_dumper`` is pinned alongside; #1376 |
| 12 | +already keeps its shipped descriptors. |
| 13 | +
|
| 14 | +Every registry is process-wide, so each case runs under |
| 15 | +:func:`unittest.mock.patch.dict`. Classes are imported inside each test, after |
| 16 | +:func:`~tests._support.reimport_once_per_class`. |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +import importlib |
| 21 | +import unittest |
| 22 | +import warnings |
| 23 | +from unittest import mock |
| 24 | + |
| 25 | +from tests._support import reimport_once_per_class |
| 26 | + |
| 27 | +#: ``(module, class)`` for each registrar, picked so its ``__proto__`` ships |
| 28 | +#: module descriptors. IPv4 and Ethernet reach the ``Internet`` and ``Link`` |
| 29 | +#: overrides; there is no class whose populated ``__proto__`` reaches |
| 30 | +#: ``ProtocolBase.register`` itself, so that one is exercised on a scratch |
| 31 | +#: subclass in its own test. |
| 32 | +SITES = ( |
| 33 | + ('pcapkit.protocols.internet.ipv4', 'IPv4'), |
| 34 | + ('pcapkit.protocols.link.ethernet', 'Ethernet'), |
| 35 | + ('pcapkit.protocols.transport.tcp', 'TCP'), |
| 36 | + ('pcapkit.protocols.transport.udp', 'UDP'), |
| 37 | + ('pcapkit.protocols.misc.pcap.frame', 'Frame'), |
| 38 | + ('pcapkit.protocols.misc.pcapng', 'PCAPNG'), |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +class TestRegistryStoredDescriptor(unittest.TestCase): |
| 43 | + """Pin when a protocol registrar warns about a stored descriptor.""" |
| 44 | + |
| 45 | + def setUp(self) -> None: |
| 46 | + reimport_once_per_class(self) |
| 47 | + |
| 48 | + def _count(self, call, *args) -> int: # type: ignore[no-untyped-def] |
| 49 | + """Run ``call(*args)`` and return how many registry warnings it raised.""" |
| 50 | + from pcapkit.utilities.warnings import RegistryWarning |
| 51 | + |
| 52 | + with warnings.catch_warnings(record=True) as caught: |
| 53 | + warnings.simplefilter('always') |
| 54 | + call(*args) |
| 55 | + return sum(issubclass(item.category, RegistryWarning) for item in caught) |
| 56 | + |
| 57 | + def _each_site(self, check): # type: ignore[no-untyped-def] |
| 58 | + """Run ``check(register, registry)`` for every site, each in its own ``subTest``.""" |
| 59 | + from pcapkit.corekit.module import ModuleDescriptor |
| 60 | + |
| 61 | + for module, name in SITES: |
| 62 | + cls = getattr(importlib.import_module(module), name) |
| 63 | + registry = cls.__proto__ |
| 64 | + self.assertTrue(any(isinstance(value, ModuleDescriptor) for value in registry.values()), name) |
| 65 | + with self.subTest(site=name), mock.patch.dict(registry): |
| 66 | + check(cls.register, registry) |
| 67 | + |
| 68 | + def test_reregistering_the_stored_descriptor_is_silent(self) -> None: |
| 69 | + from pcapkit.corekit.module import ModuleDescriptor |
| 70 | + |
| 71 | + def check(register, registry): # type: ignore[no-untyped-def] |
| 72 | + for code, value in list(registry.items()): |
| 73 | + if not isinstance(value, ModuleDescriptor): |
| 74 | + continue |
| 75 | + self.assertEqual(self._count(register, code, value), 0, code) |
| 76 | + self.assertIs(registry[code], value) |
| 77 | + # An equal descriptor built afresh is the same entry, and the |
| 78 | + # stored object is kept. |
| 79 | + self.assertEqual(self._count(register, code, ModuleDescriptor(*value)), 0, code) |
| 80 | + self.assertIs(registry[code], value) |
| 81 | + self._each_site(check) |
| 82 | + |
| 83 | + def test_reregistering_the_stored_class_is_silent(self) -> None: |
| 84 | + def check(register, registry): # type: ignore[no-untyped-def] |
| 85 | + code, value = next(iter(registry.items())) |
| 86 | + klass = value.klass |
| 87 | + self.assertEqual(self._count(register, code, klass), 1) |
| 88 | + self.assertIs(registry[code], klass) |
| 89 | + self.assertEqual(self._count(register, code, klass), 0) |
| 90 | + self.assertIs(registry[code], klass) |
| 91 | + self._each_site(check) |
| 92 | + |
| 93 | + def test_a_different_entry_still_warns(self) -> None: |
| 94 | + from pcapkit.corekit.module import ModuleDescriptor |
| 95 | + from pcapkit.protocols.misc.raw import Raw |
| 96 | + |
| 97 | + def check(register, registry): # type: ignore[no-untyped-def] |
| 98 | + code = next(iter(registry)) |
| 99 | + other = ModuleDescriptor('pcapkit.protocols.misc.raw', 'Raw') |
| 100 | + self.assertEqual(self._count(register, code, other), 1) |
| 101 | + self.assertIs(registry[code], Raw) |
| 102 | + self._each_site(check) |
| 103 | + |
| 104 | + def test_an_invalid_entry_equal_to_a_descriptor_still_raises(self) -> None: |
| 105 | + from pcapkit.utilities.exceptions import RegistryError |
| 106 | + |
| 107 | + def check(register, registry): # type: ignore[no-untyped-def] |
| 108 | + code, value = next(iter(registry.items())) |
| 109 | + with self.assertRaisesRegex(RegistryError, r'^protocol must be a class, not '): |
| 110 | + register(code, tuple(value)) |
| 111 | + self.assertIs(registry[code], value) |
| 112 | + self._each_site(check) |
| 113 | + |
| 114 | + def test_protocol_base_register_keeps_the_stored_descriptor(self) -> None: |
| 115 | + from pcapkit.corekit.module import ModuleDescriptor |
| 116 | + from pcapkit.protocols.misc.raw import Raw |
| 117 | + |
| 118 | + descriptor = ModuleDescriptor('pcapkit.protocols.misc.raw', 'Raw') |
| 119 | + scratch = type('W1364Raw', (Raw,), {'__proto__': {1: descriptor}}) |
| 120 | + self.assertEqual(self._count(scratch.register, 1, descriptor), 0) |
| 121 | + self.assertIs(scratch.__proto__[1], descriptor) |
| 122 | + self.assertEqual(self._count(scratch.register, 1, ModuleDescriptor(*descriptor)), 0) |
| 123 | + self.assertIs(scratch.__proto__[1], descriptor) |
| 124 | + other = ModuleDescriptor('pcapkit.protocols.misc.null', 'NoPayload') |
| 125 | + self.assertEqual(self._count(scratch.register, 1, other), 1) |
| 126 | + self.assertIs(scratch.__proto__[1], other.klass) |
| 127 | + |
| 128 | + def test_register_dumper_keeps_the_stored_descriptor(self) -> None: |
| 129 | + from pcapkit.corekit.module import ModuleDescriptor |
| 130 | + from pcapkit.foundation.extraction import Extractor |
| 131 | + |
| 132 | + with mock.patch.dict(Extractor.__output__): |
| 133 | + entry = Extractor.__output__['json'] |
| 134 | + self.assertIsInstance(entry[0], ModuleDescriptor) |
| 135 | + self.assertEqual(self._count(Extractor.register_dumper, 'json', *entry), 0) |
| 136 | + self.assertIs(Extractor.__output__['json'][0], entry[0]) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + unittest.main() |
0 commit comments