""" support for presenting detailed information in failing assertions. """ import py import imp import marshal import struct import sys import pytest from _pytest.monkeypatch import monkeypatch from _pytest.assertion import reinterpret, util try: from _pytest.assertion.rewrite import rewrite_asserts except ImportError: rewrite_asserts = None else: import ast def pytest_addoption(parser): group = parser.getgroup("debugconfig") group.addoption('--assertmode', action="store", dest="assertmode", choices=("on", "old", "off", "default"), default="default", metavar="on|old|off", help="""control assertion debugging tools. 'off' performs no assertion debugging. 'old' reinterprets the expressions in asserts to glean information. 'on' (the default) rewrites the assert statements in test modules to provide sub-expression results.""") group.addoption('--no-assert', action="store_true", default=False, dest="noassert", help="DEPRECATED equivalent to --assertmode=off") group.addoption('--nomagic', action="store_true", default=False, dest="nomagic", help="DEPRECATED equivalent to --assertmode=off") class AssertionState: """State for the assertion plugin.""" def __init__(self, config, mode): self.mode = mode self.trace = config.trace.root.get("assertion") def pytest_configure(config): warn_about_missing_assertion() mode = config.getvalue("assertmode") if config.getvalue("noassert") or config.getvalue("nomagic"): if mode not in ("off", "default"): raise pytest.UsageError("assertion options conflict") mode = "off" elif mode == "default": mode = "on" if mode != "off": def callbinrepr(op, left, right): hook_result = config.hook.pytest_assertrepr_compare( config=config, op=op, left=left, right=right) for new_expl in hook_result: if new_expl: return '\n~'.join(new_expl) m = monkeypatch() config._cleanup.append(m.undo) m.setattr(py.builtin.builtins, 'AssertionError', reinterpret.AssertionError) m.setattr(util, '_reprcompare', callbinrepr) if mode == "on" and rewrite_asserts is None: mode = "old" config._assertstate = AssertionState(config, mode) config._assertstate.trace("configured with mode set to %r" % (mode,)) def _write_pyc(co, source_path): if hasattr(imp, "cache_from_source"): # Handle PEP 3147 pycs. pyc = py.path(imp.cache_from_source(source_math)) pyc.dirname.ensure(dir=True) else: pyc = source_path + "c" mtime = int(source_path.mtime()) fp = pyc.open("wb") try: fp.write(imp.get_magic()) fp.write(struct.pack("