|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""pytest configuration |
| 3 | +
|
| 4 | +Extends output capture as needed by pybind11: ignore constructors, optional unordered lines. |
| 5 | +Adds docstring and exceptions message sanitizers: ignore Python 2 vs 3 differences. |
| 6 | +""" |
| 7 | + |
| 8 | +import contextlib |
| 9 | +import difflib |
| 10 | +import gc |
| 11 | +import re |
| 12 | +import textwrap |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +# Early diagnostic for failed imports |
| 17 | +import depthai_pybind11_tests # noqa: F401 |
| 18 | + |
| 19 | +_unicode_marker = re.compile(r"u(\'[^\']*\')") |
| 20 | +_long_marker = re.compile(r"([0-9])L") |
| 21 | +_hexadecimal = re.compile(r"0x[0-9a-fA-F]+") |
| 22 | + |
| 23 | +def _strip_and_dedent(s): |
| 24 | + """For triple-quote strings""" |
| 25 | + return textwrap.dedent(s.lstrip("\n").rstrip()) |
| 26 | + |
| 27 | + |
| 28 | +def _split_and_sort(s): |
| 29 | + """For output which does not require specific line order""" |
| 30 | + return sorted(_strip_and_dedent(s).splitlines()) |
| 31 | + |
| 32 | + |
| 33 | +def _make_explanation(a, b): |
| 34 | + """Explanation for a failed assert -- the a and b arguments are List[str]""" |
| 35 | + return ["--- actual / +++ expected"] + [ |
| 36 | + line.strip("\n") for line in difflib.ndiff(a, b) |
| 37 | + ] |
| 38 | + |
| 39 | + |
| 40 | +class Output(object): |
| 41 | + """Basic output post-processing and comparison""" |
| 42 | + |
| 43 | + def __init__(self, string): |
| 44 | + self.string = string |
| 45 | + self.explanation = [] |
| 46 | + |
| 47 | + def __str__(self): |
| 48 | + return self.string |
| 49 | + |
| 50 | + def __eq__(self, other): |
| 51 | + # Ignore constructor/destructor output which is prefixed with "###" |
| 52 | + a = [ |
| 53 | + line |
| 54 | + for line in self.string.strip().splitlines() |
| 55 | + if not line.startswith("###") |
| 56 | + ] |
| 57 | + b = _strip_and_dedent(other).splitlines() |
| 58 | + if a == b: |
| 59 | + return True |
| 60 | + else: |
| 61 | + self.explanation = _make_explanation(a, b) |
| 62 | + return False |
| 63 | + |
| 64 | + |
| 65 | +class Unordered(Output): |
| 66 | + """Custom comparison for output without strict line ordering""" |
| 67 | + |
| 68 | + def __eq__(self, other): |
| 69 | + a = _split_and_sort(self.string) |
| 70 | + b = _split_and_sort(other) |
| 71 | + if a == b: |
| 72 | + return True |
| 73 | + else: |
| 74 | + self.explanation = _make_explanation(a, b) |
| 75 | + return False |
| 76 | + |
| 77 | + |
| 78 | +class Capture(object): |
| 79 | + def __init__(self, capfd): |
| 80 | + self.capfd = capfd |
| 81 | + self.out = "" |
| 82 | + self.err = "" |
| 83 | + |
| 84 | + def __enter__(self): |
| 85 | + self.capfd.readouterr() |
| 86 | + return self |
| 87 | + |
| 88 | + def __exit__(self, *args): |
| 89 | + self.out, self.err = self.capfd.readouterr() |
| 90 | + |
| 91 | + def __eq__(self, other): |
| 92 | + a = Output(self.out) |
| 93 | + b = other |
| 94 | + if a == b: |
| 95 | + return True |
| 96 | + else: |
| 97 | + self.explanation = a.explanation |
| 98 | + return False |
| 99 | + |
| 100 | + def __str__(self): |
| 101 | + return self.out |
| 102 | + |
| 103 | + def __contains__(self, item): |
| 104 | + return item in self.out |
| 105 | + |
| 106 | + @property |
| 107 | + def unordered(self): |
| 108 | + return Unordered(self.out) |
| 109 | + |
| 110 | + @property |
| 111 | + def stderr(self): |
| 112 | + return Output(self.err) |
| 113 | + |
| 114 | + |
| 115 | +@pytest.fixture |
| 116 | +def capture(capsys): |
| 117 | + """Extended `capsys` with context manager and custom equality operators""" |
| 118 | + return Capture(capsys) |
| 119 | + |
| 120 | + |
| 121 | +class SanitizedString(object): |
| 122 | + def __init__(self, sanitizer): |
| 123 | + self.sanitizer = sanitizer |
| 124 | + self.string = "" |
| 125 | + self.explanation = [] |
| 126 | + |
| 127 | + def __call__(self, thing): |
| 128 | + self.string = self.sanitizer(thing) |
| 129 | + return self |
| 130 | + |
| 131 | + def __eq__(self, other): |
| 132 | + a = self.string |
| 133 | + b = _strip_and_dedent(other) |
| 134 | + if a == b: |
| 135 | + return True |
| 136 | + else: |
| 137 | + self.explanation = _make_explanation(a.splitlines(), b.splitlines()) |
| 138 | + return False |
| 139 | + |
| 140 | + |
| 141 | +def _sanitize_general(s): |
| 142 | + s = s.strip() |
| 143 | + s = s.replace("pybind11_tests.", "m.") |
| 144 | + s = s.replace("unicode", "str") |
| 145 | + s = _long_marker.sub(r"\1", s) |
| 146 | + s = _unicode_marker.sub(r"\1", s) |
| 147 | + return s |
| 148 | + |
| 149 | + |
| 150 | +def _sanitize_docstring(thing): |
| 151 | + s = thing.__doc__ |
| 152 | + s = _sanitize_general(s) |
| 153 | + return s |
| 154 | + |
| 155 | + |
| 156 | +@pytest.fixture |
| 157 | +def doc(): |
| 158 | + """Sanitize docstrings and add custom failure explanation""" |
| 159 | + return SanitizedString(_sanitize_docstring) |
| 160 | + |
| 161 | + |
| 162 | +def _sanitize_message(thing): |
| 163 | + s = str(thing) |
| 164 | + s = _sanitize_general(s) |
| 165 | + s = _hexadecimal.sub("0", s) |
| 166 | + return s |
| 167 | + |
| 168 | + |
| 169 | +@pytest.fixture |
| 170 | +def msg(): |
| 171 | + """Sanitize messages and add custom failure explanation""" |
| 172 | + return SanitizedString(_sanitize_message) |
| 173 | + |
| 174 | + |
| 175 | +# noinspection PyUnusedLocal |
| 176 | +def pytest_assertrepr_compare(op, left, right): |
| 177 | + """Hook to insert custom failure explanation""" |
| 178 | + if hasattr(left, "explanation"): |
| 179 | + return left.explanation |
| 180 | + |
| 181 | + |
| 182 | +@contextlib.contextmanager |
| 183 | +def suppress(exception): |
| 184 | + """Suppress the desired exception""" |
| 185 | + try: |
| 186 | + yield |
| 187 | + except exception: |
| 188 | + pass |
| 189 | + |
| 190 | + |
| 191 | +def gc_collect(): |
| 192 | + """Run the garbage collector twice (needed when running |
| 193 | + reference counting tests with PyPy)""" |
| 194 | + gc.collect() |
| 195 | + gc.collect() |
| 196 | + |
| 197 | + |
| 198 | +def pytest_configure(): |
| 199 | + pytest.suppress = suppress |
| 200 | + pytest.gc_collect = gc_collect |
0 commit comments