Skip to content

Commit 0bf2c53

Browse files
authored
docs(api): document context, IPX._next_exthdr_depth and the pcapng schema helpers (#2234) (#2251)
Problem: ProtocolBase.context, the IPX._next_exthdr_depth override of a documented hook, and held_body, FlagsField and PACKET_FLAGS in the pcapng schema module had docstrings but no API entry. Fix: list context on the Protocol autoclass and in the ProtocolBase property group, add _next_exthdr_depth to the IPX autoclass, and add the three pcapng entries beside their siblings. _held_octets and _secrets_cut stay undocumented: no rendered text names them, so they are recipe. Test: tests/project/test_docs_missing_directives_2234_unit.py pins each entry and the object it names; it fails on origin/main.
1 parent 21b0b57 commit 0bf2c53

4 files changed

Lines changed: 134 additions & 1 deletion

File tree

‎docs/source/pcapkit/protocols/internet/ipx.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Octets Bits Name Description
3434
.. automethod:: make
3535

3636
.. automethod:: _make_data
37+
.. automethod:: _next_exthdr_depth
3738

3839
.. automethod:: __index__
3940

‎docs/source/pcapkit/protocols/misc/pcapng.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ Header Schemas
260260

261261
.. autodata:: pcapkit.protocols.schema.misc.pcapng.DEFAULT_RESOLUTION
262262

263+
.. autodata:: pcapkit.protocols.schema.misc.pcapng.PACKET_FLAGS
264+
:no-value:
265+
263266
.. autoclass:: pcapkit.protocols.schema.misc.pcapng.PCAPNG
264267
:members:
265268
:show-inheritance:
@@ -537,6 +540,10 @@ Header Schemas
537540
:members:
538541
:show-inheritance:
539542

543+
.. autoclass:: pcapkit.protocols.schema.misc.pcapng.FlagsField
544+
:members:
545+
:show-inheritance:
546+
540547
.. autoclass:: pcapkit.protocols.schema.misc.pcapng.OptionEnumField
541548
:members:
542549
:show-inheritance:
@@ -577,6 +584,7 @@ shared with the PCAP frame header and documented in
577584
.. autofunction:: pcapkit.protocols.schema.misc.pcapng.dsb_secrets_selector
578585

579586
.. autofunction:: pcapkit.protocols.schema.misc.pcapng.nonnegative
587+
.. autofunction:: pcapkit.protocols.schema.misc.pcapng.held_body
580588
.. autofunction:: pcapkit.protocols.schema.misc.pcapng.bounded_option
581589
.. autofunction:: pcapkit.protocols.schema.misc.pcapng.bounded_area
582590
.. autofunction:: pcapkit.protocols.schema.misc.pcapng.captured_area

‎docs/source/pcapkit/protocols/protocol.rst‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Internal Definitions below.
4141
.. autoproperty:: protochain
4242
.. autoproperty:: packet
4343
.. autoproperty:: schema
44+
.. autoproperty:: context
4445

4546
.. automethod:: id
4647
.. automethod:: register
@@ -166,6 +167,7 @@ Internal Definitions
166167
protochain
167168
packet
168169
schema
170+
context
169171
:no-index-entry:
170172
:no-contents-entry:
171173

@@ -174,7 +176,7 @@ Internal Definitions
174176
:attr:`~Protocol.data`, :attr:`~Protocol.length`,
175177
:attr:`~Protocol.payload`, :attr:`~Protocol.protocol`,
176178
:attr:`~Protocol.protochain`, :attr:`~Protocol.packet`,
177-
:attr:`~Protocol.schema`
179+
:attr:`~Protocol.schema`, :attr:`~Protocol.context`
178180

179181
.. py:method:: id
180182
register
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# -*- coding: utf-8 -*-
2+
"""Pins the API entries #2234 adds for members that had a docstring but no directive.
3+
4+
* ``Protocol.context`` -- a public property of every protocol, listed on the
5+
``Protocol`` autoclass and in the ``ProtocolBase`` property group beside its
6+
siblings.
7+
* ``IPX._next_exthdr_depth`` -- an override of the
8+
``Internet._next_exthdr_depth`` hook, which ``internet.rst`` documents, so the
9+
override is contract too.
10+
* ``held_body``, ``FlagsField`` and ``PACKET_FLAGS`` in
11+
:mod:`pcapkit.protocols.schema.misc.pcapng` -- listed beside the length
12+
callbacks, the option fields and the ``autodata`` constants respectively.
13+
14+
Each entry is checked as declared on its page and as naming a real object, the
15+
textual precondition a plain (non-nitpicky) build would not notice losing.
16+
17+
"""
18+
19+
from __future__ import annotations
20+
21+
import importlib
22+
import pathlib
23+
import re
24+
import unittest
25+
26+
ROOT = pathlib.Path(__file__).resolve().parents[2]
27+
API = ROOT / 'docs' / 'source' / 'pcapkit' / 'protocols'
28+
29+
#: The pcapng schema module, whose helpers ``pcapng.rst`` documents.
30+
SCHEMA = 'pcapkit.protocols.schema.misc.pcapng'
31+
32+
33+
def _block(text: 'str', directive: 'str') -> 'str':
34+
"""The body of the top-level ``directive`` in ``text``, up to the next unindented line."""
35+
lines = text.splitlines()
36+
start = lines.index(directive)
37+
body = []
38+
for line in lines[start + 1:]:
39+
if line and not line[0].isspace():
40+
break
41+
body.append(line)
42+
return '\n'.join(body)
43+
44+
45+
def _group(block: 'str', directive: 'str') -> 'list[str]':
46+
"""The names of a grouped ``directive`` (e.g. ``py:property``) inside ``block``, options left out."""
47+
match = re.search(rf'^( *)\.\. {re.escape(directive)}:: (\S+)\n((?:\1 +[^:\s]\S*\n)*)', block + '\n', re.M)
48+
if match is None:
49+
return []
50+
return [match.group(2)] + match.group(3).split()
51+
52+
53+
class ProtocolContextTests(unittest.TestCase):
54+
"""``context`` is documented on ``Protocol`` and linked from ``ProtocolBase``."""
55+
56+
text = (API / 'protocol.rst').read_text(encoding='utf-8')
57+
58+
def test_protocol_autoclass_lists_context(self) -> 'None':
59+
"""Listed after ``schema``, the property it follows in the class."""
60+
block = _block(self.text, '.. autoclass:: pcapkit.protocols.protocol.Protocol')
61+
self.assertIn(' .. autoproperty:: schema\n .. autoproperty:: context\n', block)
62+
63+
def test_protocolbase_group_lists_and_links_context(self) -> 'None':
64+
"""In the property group, and linked to its ``Protocol`` entry."""
65+
block = _block(self.text, '.. autoclass:: pcapkit.protocols.protocol.ProtocolBase')
66+
self.assertIn('context', _group(block, 'py:property'))
67+
self.assertIn(':attr:`~Protocol.context`', block)
68+
69+
def test_context_is_a_property_of_protocolbase(self) -> 'None':
70+
"""The entry names a real property, defined where the group says."""
71+
from pcapkit.protocols.protocol import ProtocolBase # pylint: disable=import-outside-toplevel
72+
self.assertIsInstance(ProtocolBase.__dict__.get('context'), property)
73+
74+
75+
class IPXNextExthdrDepthTests(unittest.TestCase):
76+
"""The ``_next_exthdr_depth`` override on ``IPX`` has an entry."""
77+
78+
def test_ipx_autoclass_lists_the_override(self) -> 'None':
79+
"""Listed on the ``IPX`` autoclass beside ``_make_data``."""
80+
text = (API / 'internet' / 'ipx.rst').read_text(encoding='utf-8')
81+
block = _block(text, '.. autoclass:: pcapkit.protocols.internet.ipx.IPX')
82+
self.assertIn(' .. automethod:: _next_exthdr_depth\n', block)
83+
84+
def test_the_hook_it_overrides_is_documented(self) -> 'None':
85+
"""The base hook has its own entry, which makes the override contract."""
86+
text = (API / 'internet' / 'internet.rst').read_text(encoding='utf-8')
87+
self.assertIn(' .. automethod:: _next_exthdr_depth\n', text)
88+
89+
def test_ipx_defines_its_own_override(self) -> 'None':
90+
"""The entry documents the method ``IPX`` defines, not an inherited one."""
91+
from pcapkit.protocols.internet.ipx import IPX # pylint: disable=import-outside-toplevel
92+
self.assertIn('_next_exthdr_depth', IPX.__dict__)
93+
94+
95+
class PCAPNGSchemaHelperTests(unittest.TestCase):
96+
"""``held_body``, ``FlagsField`` and ``PACKET_FLAGS`` have entries on ``pcapng.rst``."""
97+
98+
text = (API / 'misc' / 'pcapng.rst').read_text(encoding='utf-8')
99+
100+
#: Each name, and the directive that documents it.
101+
ENTRIES = {
102+
'held_body': f'.. autofunction:: {SCHEMA}.held_body\n',
103+
'FlagsField': f'.. autoclass:: {SCHEMA}.FlagsField\n :members:\n :show-inheritance:\n',
104+
'PACKET_FLAGS': f'.. autodata:: {SCHEMA}.PACKET_FLAGS\n :no-value:\n',
105+
}
106+
107+
def test_each_entry_is_declared(self) -> 'None':
108+
"""Each directive appears once, with the options its siblings carry."""
109+
for name, directive in self.ENTRIES.items():
110+
with self.subTest(name=name):
111+
self.assertEqual(self.text.count(directive), 1)
112+
113+
def test_each_entry_names_a_real_object(self) -> 'None':
114+
"""Each documented name exists in the schema module."""
115+
module = importlib.import_module(SCHEMA)
116+
for name in self.ENTRIES:
117+
with self.subTest(name=name):
118+
self.assertTrue(hasattr(module, name))
119+
120+
121+
if __name__ == '__main__':
122+
unittest.main()

0 commit comments

Comments
 (0)