2010-11-06 18:38:53 +08:00
|
|
|
"""
|
|
|
|
support for presented detailed information in failing assertions.
|
|
|
|
"""
|
2009-08-27 23:26:02 +08:00
|
|
|
import py
|
2011-05-20 10:52:10 +08:00
|
|
|
import imp
|
|
|
|
import marshal
|
|
|
|
import struct
|
2009-08-29 01:16:15 +08:00
|
|
|
import sys
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.monkeypatch import monkeypatch
|
2011-05-27 01:01:34 +08:00
|
|
|
from _pytest.assertion import reinterpret, util
|
2009-08-27 23:26:02 +08:00
|
|
|
|
2011-05-20 10:52:10 +08:00
|
|
|
try:
|
2011-05-26 05:18:45 +08:00
|
|
|
from _pytest.assertion.rewrite import rewrite_asserts
|
2011-05-20 10:52:10 +08:00
|
|
|
except ImportError:
|
|
|
|
rewrite_asserts = None
|
|
|
|
else:
|
|
|
|
import ast
|
|
|
|
|
2009-08-27 23:26:02 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("debugconfig")
|
2010-07-27 03:15:15 +08:00
|
|
|
group._addoption('--no-assert', action="store_true", default=False,
|
|
|
|
dest="noassert",
|
2009-08-27 23:26:02 +08:00
|
|
|
help="disable python assert expression reinterpretation."),
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
2011-05-20 10:52:10 +08:00
|
|
|
global rewrite_asserts
|
2011-02-16 07:24:18 +08:00
|
|
|
# The _reprcompare attribute on the py.code module is used by
|
2010-09-22 07:26:12 +08:00
|
|
|
# py._code._assertionnew to detect this plugin was loaded and in
|
|
|
|
# turn call the hooks defined here as part of the
|
|
|
|
# DebugInterpreter.
|
2011-04-17 18:20:13 +08:00
|
|
|
m = monkeypatch()
|
|
|
|
config._cleanup.append(m.undo)
|
2010-12-09 18:00:31 +08:00
|
|
|
warn_about_missing_assertion()
|
2009-09-01 02:06:55 +08:00
|
|
|
if not config.getvalue("noassert") and not config.getvalue("nomagic"):
|
2010-10-03 00:47:39 +08:00
|
|
|
def callbinrepr(op, left, right):
|
2010-10-03 01:00:47 +08:00
|
|
|
hook_result = config.hook.pytest_assertrepr_compare(
|
2010-10-03 00:47:39 +08:00
|
|
|
config=config, op=op, left=left, right=right)
|
|
|
|
for new_expl in hook_result:
|
|
|
|
if new_expl:
|
|
|
|
return '\n~'.join(new_expl)
|
2011-05-26 06:54:02 +08:00
|
|
|
m.setattr(py.builtin.builtins, 'AssertionError',
|
|
|
|
reinterpret.AssertionError)
|
2011-05-27 01:01:34 +08:00
|
|
|
m.setattr(util, '_reprcompare', callbinrepr)
|
2011-05-20 10:52:10 +08:00
|
|
|
else:
|
|
|
|
rewrite_asserts = None
|
|
|
|
|
2011-05-25 06:48:56 +08:00
|
|
|
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("<l", mtime))
|
|
|
|
marshal.dump(co, fp)
|
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
return pyc
|
|
|
|
|
2011-05-20 10:52:10 +08:00
|
|
|
def pytest_pycollect_before_module_import(mod):
|
|
|
|
if rewrite_asserts is None:
|
|
|
|
return
|
|
|
|
# Some deep magic: load the source, rewrite the asserts, and write a
|
2011-05-27 02:15:03 +08:00
|
|
|
# fake pyc, so that it'll be loaded when the module is imported.
|
2011-05-20 10:52:10 +08:00
|
|
|
source = mod.fspath.read()
|
|
|
|
try:
|
|
|
|
tree = ast.parse(source)
|
|
|
|
except SyntaxError:
|
|
|
|
# Let this pop up again in the real import.
|
|
|
|
return
|
|
|
|
rewrite_asserts(tree)
|
|
|
|
try:
|
|
|
|
co = compile(tree, str(mod.fspath), "exec")
|
|
|
|
except SyntaxError:
|
|
|
|
# It's possible that this error is from some bug in the assertion
|
|
|
|
# rewriting, but I don't know of a fast way to tell.
|
|
|
|
return
|
2011-05-25 06:48:56 +08:00
|
|
|
mod._pyc = _write_pyc(co, mod.fspath)
|
2011-05-20 10:52:10 +08:00
|
|
|
|
|
|
|
def pytest_pycollect_after_module_import(mod):
|
|
|
|
if rewrite_asserts is None or not hasattr(mod, "_pyc"):
|
|
|
|
return
|
|
|
|
# Remove our tweaked pyc to avoid subtle bugs.
|
|
|
|
try:
|
|
|
|
mod._pyc.remove()
|
|
|
|
except py.error.ENOENT:
|
|
|
|
pass
|
2009-08-27 23:26:02 +08:00
|
|
|
|
|
|
|
def warn_about_missing_assertion():
|
|
|
|
try:
|
|
|
|
assert False
|
|
|
|
except AssertionError:
|
|
|
|
pass
|
|
|
|
else:
|
2010-12-09 18:00:31 +08:00
|
|
|
sys.stderr.write("WARNING: failing tests may report as passing because "
|
|
|
|
"assertions are turned off! (are you using python -O?)\n")
|
2010-09-07 02:35:17 +08:00
|
|
|
|
2011-05-27 01:01:34 +08:00
|
|
|
pytest_assertrepr_compare = util.assertrepr_compare
|