conftest.py revision 12037
1"""pytest configuration
2
3Extends output capture as needed by pybind11: ignore constructors, optional unordered lines.
4Adds docstring and exceptions message sanitizers: ignore Python 2 vs 3 differences.
5"""
6
7import pytest
8import textwrap
9import difflib
10import re
11import sys
12import contextlib
13import platform
14import gc
15
16_unicode_marker = re.compile(r'u(\'[^\']*\')')
17_long_marker = re.compile(r'([0-9])L')
18_hexadecimal = re.compile(r'0x[0-9a-fA-F]+')
19
20
21def _strip_and_dedent(s):
22    """For triple-quote strings"""
23    return textwrap.dedent(s.lstrip('\n').rstrip())
24
25
26def _split_and_sort(s):
27    """For output which does not require specific line order"""
28    return sorted(_strip_and_dedent(s).splitlines())
29
30
31def _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
36class 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
56class 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
68class Capture(object):
69    def __init__(self, capfd):
70        self.capfd = capfd
71        self.out = ""
72        self.err = ""
73
74    def __enter__(self):
75        self.capfd.readouterr()
76        return self
77
78    def __exit__(self, *_):
79        self.out, self.err = self.capfd.readouterr()
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
100    @property
101    def stderr(self):
102        return Output(self.err)
103
104
105@pytest.fixture
106def capture(capsys):
107    """Extended `capsys` with context manager and custom equality operators"""
108    return Capture(capsys)
109
110
111class 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
131def _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
140def _sanitize_docstring(thing):
141    s = thing.__doc__
142    s = _sanitize_general(s)
143    return s
144
145
146@pytest.fixture
147def doc():
148    """Sanitize docstrings and add custom failure explanation"""
149    return SanitizedString(_sanitize_docstring)
150
151
152def _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
160def msg():
161    """Sanitize messages and add custom failure explanation"""
162    return SanitizedString(_sanitize_message)
163
164
165# noinspection PyUnusedLocal
166def 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
173def suppress(exception):
174    """Suppress the desired exception"""
175    try:
176        yield
177    except exception:
178        pass
179
180
181def 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
188def 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
202    pypy = platform.python_implementation() == "PyPy"
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"),
213        'unsupported_on_pypy': skipif(pypy, reason="unsupported on PyPy"),
214        'gc_collect': gc_collect
215    }
216
217
218def _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:
232        import pybind11_tests  # noqa: F401 imported but unused
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()
240