|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Shared known-failure tables for the corekit round-trip modules. C.f. #1202. |
| 3 | +
|
| 4 | +The owner's rule is that every round trip the library offers reproduces its |
| 5 | +input exactly. The ``test_*_roundtrip_unit`` modules beside this one enumerate |
| 6 | +the corekit round trips -- a field's ``pack``/``unpack``, a schema's |
| 7 | +``pack``/``unpack`` and ``from_dict``/``to_dict``, an :class:`~pcapkit.corekit. |
| 8 | +infoclass.Info`'s ``from_dict``/``to_dict`` -- and report one :class:`Outcome` |
| 9 | +per case. A case that does not close is listed in its module's |
| 10 | +``KNOWN_FAILURES``, one :class:`Gap` per root cause, in the style of |
| 11 | +:mod:`tests.protocols._edge_roundtrip`. |
| 12 | +
|
| 13 | +The table is asserted in both directions: a case that fails and is not listed |
| 14 | +turns its module red, and so does a listed case that passes, so a fix has to |
| 15 | +delete its entry in the same change. A pattern that matches no case is stale and |
| 16 | +fails too. |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import fnmatch |
| 23 | +import importlib.util |
| 24 | +import unittest |
| 25 | +from typing import TYPE_CHECKING, NamedTuple |
| 26 | + |
| 27 | +from tests._support import time_limit |
| 28 | + |
| 29 | +if TYPE_CHECKING: |
| 30 | + from typing import Any, Callable, Iterable, Optional |
| 31 | + |
| 32 | +RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper') |
| 33 | +HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS) |
| 34 | + |
| 35 | +#: Whole seconds one case may take; a working case takes milliseconds. |
| 36 | +CASE_TIMEOUT = 30 |
| 37 | + |
| 38 | + |
| 39 | +class Outcome(NamedTuple): |
| 40 | + """What one check found.""" |
| 41 | + |
| 42 | + #: ``'OK'`` or a module-specific failure status. |
| 43 | + status: 'str' |
| 44 | + #: Failure detail; empty when the case closed. |
| 45 | + detail: 'str' = '' |
| 46 | + |
| 47 | + |
| 48 | +#: The outcome of a case that closed. |
| 49 | +OK = Outcome('OK') |
| 50 | + |
| 51 | + |
| 52 | +class Gap(NamedTuple): |
| 53 | + """One root cause, and every case it stops.""" |
| 54 | + |
| 55 | + #: GitHub issue tracking the defect. |
| 56 | + issue: 'Optional[int]' |
| 57 | + #: The defect, with the ``file:line`` that causes it. |
| 58 | + defect: 'str' |
| 59 | + #: Status every case below reports, or a tuple of which each reports one. |
| 60 | + status: 'str | tuple[str, ...]' |
| 61 | + #: Substring every failure detail must contain, or a tuple of which each |
| 62 | + #: detail must contain one. |
| 63 | + fragment: 'str | tuple[str, ...]' |
| 64 | + #: Labels of the cases this defect stops, as :mod:`fnmatch` patterns. Each |
| 65 | + #: pattern must match at least one case, and every case it matches must fail |
| 66 | + #: as recorded, so a pattern is only as wide as the defect. |
| 67 | + cases: 'tuple[str, ...]' |
| 68 | + |
| 69 | + |
| 70 | +def describe(exc: 'BaseException') -> 'str': |
| 71 | + """``Type: message``, as a failure detail.""" |
| 72 | + return f'{type(exc).__name__}: {exc}' |
| 73 | + |
| 74 | + |
| 75 | +def diff(got: 'bytes', want: 'bytes') -> 'str': |
| 76 | + """Two octet strings as a failure detail.""" |
| 77 | + return f'{len(got)} octets {got.hex()} != {len(want)} octets {want.hex()}' |
| 78 | + |
| 79 | + |
| 80 | +def run(func: 'Callable[..., Outcome]', *args: 'Any') -> 'Outcome': |
| 81 | + """Run ``func(*args)``, one check, under :data:`CASE_TIMEOUT`.""" |
| 82 | + try: |
| 83 | + with time_limit(CASE_TIMEOUT): |
| 84 | + return func(*args) |
| 85 | + except TimeoutError as exc: |
| 86 | + return Outcome('TIMEOUT', str(exc)) |
| 87 | + |
| 88 | + |
| 89 | +def _as_tuple(value: 'str | tuple[str, ...]') -> 'tuple[str, ...]': |
| 90 | + return (value,) if isinstance(value, str) else value |
| 91 | + |
| 92 | + |
| 93 | +class KnownFailureTable: |
| 94 | + """The table assertions, mixed into a :class:`unittest.TestCase`.""" |
| 95 | + |
| 96 | + #: Known failures, one entry per root cause. |
| 97 | + KNOWN_FAILURES = () # type: tuple[Gap, ...] |
| 98 | + #: Statuses a case in this module may report. |
| 99 | + STATUSES = ('OK',) # type: tuple[str, ...] |
| 100 | + |
| 101 | + def gap_table(self: 'Any', labels: 'Iterable[str]') -> 'dict[str, Gap]': |
| 102 | + """Every label a ``KNOWN_FAILURES`` pattern matches, to its entry.""" |
| 103 | + labels = sorted(labels) |
| 104 | + table = {} # type: dict[str, Gap] |
| 105 | + for gap in self.KNOWN_FAILURES: |
| 106 | + for pattern in gap.cases: |
| 107 | + for label in fnmatch.filter(labels, pattern): |
| 108 | + self.assertIs(table.setdefault(label, gap), gap, |
| 109 | + f'{label} is listed under two root causes') |
| 110 | + return table |
| 111 | + |
| 112 | + def check_table(self: 'Any', labels: 'Iterable[str]') -> 'None': |
| 113 | + """Every pattern names a case, and every entry is well formed.""" |
| 114 | + labels = sorted(labels) |
| 115 | + self.assertEqual(len(labels), len(set(labels)), 'duplicate case labels') |
| 116 | + stale = [pattern for gap in self.KNOWN_FAILURES for pattern in gap.cases |
| 117 | + if not fnmatch.filter(labels, pattern)] |
| 118 | + self.assertEqual(stale, [], 'KNOWN_FAILURES patterns that match no case') |
| 119 | + for gap in self.KNOWN_FAILURES: |
| 120 | + self.assertTrue(gap.cases, f'empty KNOWN_FAILURES entry: {gap.defect}') |
| 121 | + for status in _as_tuple(gap.status): |
| 122 | + self.assertIn(status, self.STATUSES) |
| 123 | + self.assertNotEqual(status, 'OK', gap.defect) |
| 124 | + |
| 125 | + def check_outcome(self: 'Any', label: 'str', outcome: 'Outcome', |
| 126 | + gaps: 'dict[str, Gap]') -> 'None': |
| 127 | + """Assert ``outcome`` is ``OK``, or fails as its entry records.""" |
| 128 | + gap = gaps.get(label) |
| 129 | + if gap is None: |
| 130 | + self.assertEqual( |
| 131 | + outcome.status, 'OK', |
| 132 | + f'{label} does not round-trip: {outcome.status}: {outcome.detail}. ' |
| 133 | + f'If this is a new defect, add it to KNOWN_FAILURES under its root cause.') |
| 134 | + return |
| 135 | + self.assertIn( |
| 136 | + outcome.status, _as_tuple(gap.status), |
| 137 | + f'{label} was recorded as failing with {gap.status} (#{gap.issue}: ' |
| 138 | + f'{gap.defect}) but came back {outcome.status}: {outcome.detail}. ' |
| 139 | + f'If the defect is fixed, delete the label from its entry.') |
| 140 | + fragments = [fragment for fragment in _as_tuple(gap.fragment) if fragment] |
| 141 | + if fragments: |
| 142 | + self.assertTrue( |
| 143 | + any(fragment in outcome.detail for fragment in fragments), |
| 144 | + f'{label} fails, but not in the recorded way ({gap.defect}); ' |
| 145 | + f'detail was {outcome.detail!r}') |
| 146 | + |
| 147 | + |
| 148 | +def skip_without_runtime(case: 'unittest.TestCase') -> 'None': |
| 149 | + """Skip ``case`` when the runtime dependencies are not installed.""" |
| 150 | + if not HAS_RUNTIME: |
| 151 | + case.skipTest('runtime dependencies not installed') |
0 commit comments