Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | """pytest configuration |
| 2 | |
| 3 | Extends output capture as needed by pybind11: ignore constructors, optional unordered lines. |
| 4 | Adds docstring and exceptions message sanitizers: ignore Python 2 vs 3 differences. |
| 5 | """ |
| 6 | |
| 7 | import pytest |
| 8 | import textwrap |
| 9 | import difflib |
| 10 | import re |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 11 | import sys |
| 12 | import contextlib |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 13 | import platform |
| 14 | import gc |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 15 | |
| 16 | _unicode_marker = re.compile(r'u(\'[^\']*\')') |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 17 | _long_marker = re.compile(r'([0-9])L') |
| 18 | _hexadecimal = re.compile(r'0x[0-9a-fA-F]+') |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 19 | |
| 20 | |
| 21 | def _strip_and_dedent(s): |
| 22 | """For triple-quote strings""" |
| 23 | return textwrap.dedent(s.lstrip('\n').rstrip()) |
| 24 | |
| 25 | |
| 26 | def _split_and_sort(s): |
| 27 | """For output which does not require specific line order""" |
| 28 | return sorted(_strip_and_dedent(s).splitlines()) |
| 29 | |
| 30 | |
| 31 | def _make_explanation(a, b): |
| 32 | """Explanation for a failed assert -- the a and b arguments are List[str]""" |
| 33 | return ["--- actual / +++ expected"] + [line.strip('\n') for line in difflib.ndiff(a, b)] |
| 34 | |
| 35 | |
| 36 | class Output(object): |
| 37 | """Basic output post-processing and comparison""" |
| 38 | def __init__(self, string): |
| 39 | self.string = string |
| 40 | self.explanation = [] |
| 41 | |
| 42 | def __str__(self): |
| 43 | return self.string |
| 44 | |
| 45 | def __eq__(self, other): |
| 46 | # Ignore constructor/destructor output which is prefixed with "###" |
| 47 | a = [line for line in self.string.strip().splitlines() if not line.startswith("###")] |
| 48 | b = _strip_and_dedent(other).splitlines() |
| 49 | if a == b: |
| 50 | return True |
| 51 | else: |
| 52 | self.explanation = _make_explanation(a, b) |
| 53 | return False |
| 54 | |
| 55 | |
| 56 | class Unordered(Output): |
| 57 | """Custom comparison for output without strict line ordering""" |
| 58 | def __eq__(self, other): |
| 59 | a = _split_and_sort(self.string) |
| 60 | b = _split_and_sort(other) |
| 61 | if a == b: |
| 62 | return True |
| 63 | else: |
| 64 | self.explanation = _make_explanation(a, b) |
| 65 | return False |
| 66 | |
| 67 | |
| 68 | class Capture(object): |
| 69 | def __init__(self, capfd): |
| 70 | self.capfd = capfd |
| 71 | self.out = "" |
Dean Moldovan | 67990d9 | 2016-08-29 18:03:34 +0200 | [diff] [blame] | 72 | self.err = "" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 73 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 74 | def __enter__(self): |
Dean Moldovan | 81511be | 2016-09-07 00:50:10 +0200 | [diff] [blame] | 75 | self.capfd.readouterr() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 76 | return self |
| 77 | |
| 78 | def __exit__(self, *_): |
Dean Moldovan | 81511be | 2016-09-07 00:50:10 +0200 | [diff] [blame] | 79 | self.out, self.err = self.capfd.readouterr() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 80 | |
| 81 | def __eq__(self, other): |
| 82 | a = Output(self.out) |
| 83 | b = other |
| 84 | if a == b: |
| 85 | return True |
| 86 | else: |
| 87 | self.explanation = a.explanation |
| 88 | return False |
| 89 | |
| 90 | def __str__(self): |
| 91 | return self.out |
| 92 | |
| 93 | def __contains__(self, item): |
| 94 | return item in self.out |
| 95 | |
| 96 | @property |
| 97 | def unordered(self): |
| 98 | return Unordered(self.out) |
| 99 | |
Dean Moldovan | 67990d9 | 2016-08-29 18:03:34 +0200 | [diff] [blame] | 100 | @property |
| 101 | def stderr(self): |
| 102 | return Output(self.err) |
| 103 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 104 | |
| 105 | @pytest.fixture |
Dean Moldovan | d47febc | 2017-03-10 15:42:42 +0100 | [diff] [blame] | 106 | def capture(capsys): |
| 107 | """Extended `capsys` with context manager and custom equality operators""" |
| 108 | return Capture(capsys) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 109 | |
| 110 | |
| 111 | class SanitizedString(object): |
| 112 | def __init__(self, sanitizer): |
| 113 | self.sanitizer = sanitizer |
| 114 | self.string = "" |
| 115 | self.explanation = [] |
| 116 | |
| 117 | def __call__(self, thing): |
| 118 | self.string = self.sanitizer(thing) |
| 119 | return self |
| 120 | |
| 121 | def __eq__(self, other): |
| 122 | a = self.string |
| 123 | b = _strip_and_dedent(other) |
| 124 | if a == b: |
| 125 | return True |
| 126 | else: |
| 127 | self.explanation = _make_explanation(a.splitlines(), b.splitlines()) |
| 128 | return False |
| 129 | |
| 130 | |
| 131 | def _sanitize_general(s): |
| 132 | s = s.strip() |
| 133 | s = s.replace("pybind11_tests.", "m.") |
| 134 | s = s.replace("unicode", "str") |
| 135 | s = _long_marker.sub(r"\1", s) |
| 136 | s = _unicode_marker.sub(r"\1", s) |
| 137 | return s |
| 138 | |
| 139 | |
| 140 | def _sanitize_docstring(thing): |
| 141 | s = thing.__doc__ |
| 142 | s = _sanitize_general(s) |
| 143 | return s |
| 144 | |
| 145 | |
| 146 | @pytest.fixture |
| 147 | def doc(): |
| 148 | """Sanitize docstrings and add custom failure explanation""" |
| 149 | return SanitizedString(_sanitize_docstring) |
| 150 | |
| 151 | |
| 152 | def _sanitize_message(thing): |
| 153 | s = str(thing) |
| 154 | s = _sanitize_general(s) |
| 155 | s = _hexadecimal.sub("0", s) |
| 156 | return s |
| 157 | |
| 158 | |
| 159 | @pytest.fixture |
| 160 | def msg(): |
| 161 | """Sanitize messages and add custom failure explanation""" |
| 162 | return SanitizedString(_sanitize_message) |
| 163 | |
| 164 | |
| 165 | # noinspection PyUnusedLocal |
| 166 | def pytest_assertrepr_compare(op, left, right): |
| 167 | """Hook to insert custom failure explanation""" |
| 168 | if hasattr(left, 'explanation'): |
| 169 | return left.explanation |
| 170 | |
| 171 | |
| 172 | @contextlib.contextmanager |
| 173 | def suppress(exception): |
| 174 | """Suppress the desired exception""" |
| 175 | try: |
| 176 | yield |
| 177 | except exception: |
| 178 | pass |
| 179 | |
| 180 | |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 181 | def gc_collect(): |
| 182 | ''' Run the garbage collector twice (needed when running |
| 183 | reference counting tests with PyPy) ''' |
| 184 | gc.collect() |
| 185 | gc.collect() |
| 186 | |
| 187 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 188 | def pytest_namespace(): |
| 189 | """Add import suppression and test requirements to `pytest` namespace""" |
| 190 | try: |
| 191 | import numpy as np |
| 192 | except ImportError: |
| 193 | np = None |
| 194 | try: |
| 195 | import scipy |
| 196 | except ImportError: |
| 197 | scipy = None |
| 198 | try: |
| 199 | from pybind11_tests import have_eigen |
| 200 | except ImportError: |
| 201 | have_eigen = False |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 202 | pypy = platform.python_implementation() == "PyPy" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 203 | |
| 204 | skipif = pytest.mark.skipif |
| 205 | return { |
| 206 | 'suppress': suppress, |
| 207 | 'requires_numpy': skipif(not np, reason="numpy is not installed"), |
| 208 | 'requires_scipy': skipif(not np, reason="scipy is not installed"), |
| 209 | 'requires_eigen_and_numpy': skipif(not have_eigen or not np, |
| 210 | reason="eigen and/or numpy are not installed"), |
| 211 | 'requires_eigen_and_scipy': skipif(not have_eigen or not scipy, |
| 212 | reason="eigen and/or scipy are not installed"), |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 213 | 'unsupported_on_pypy': skipif(pypy, reason="unsupported on PyPy"), |
| 214 | 'gc_collect': gc_collect |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 215 | } |
Dean Moldovan | 2391917 | 2016-08-25 17:08:09 +0200 | [diff] [blame] | 216 | |
| 217 | |
| 218 | def _test_import_pybind11(): |
| 219 | """Early diagnostic for test module initialization errors |
| 220 | |
| 221 | When there is an error during initialization, the first import will report the |
| 222 | real error while all subsequent imports will report nonsense. This import test |
| 223 | is done early (in the pytest configuration file, before any tests) in order to |
| 224 | avoid the noise of having all tests fail with identical error messages. |
| 225 | |
| 226 | Any possible exception is caught here and reported manually *without* the stack |
| 227 | trace. This further reduces noise since the trace would only show pytest internals |
| 228 | which are not useful for debugging pybind11 module issues. |
| 229 | """ |
| 230 | # noinspection PyBroadException |
| 231 | try: |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 232 | import pybind11_tests # noqa: F401 imported but unused |
Dean Moldovan | 2391917 | 2016-08-25 17:08:09 +0200 | [diff] [blame] | 233 | except Exception as e: |
| 234 | print("Failed to import pybind11_tests from pytest:") |
| 235 | print(" {}: {}".format(type(e).__name__, e)) |
| 236 | sys.exit(1) |
| 237 | |
| 238 | |
| 239 | _test_import_pybind11() |