Style fixes for pep8

Includes a quotation change for consistent style.
This commit is contained in:
Floris Bruynooghe 2014-04-02 17:16:37 +01:00
parent 36288c5134
commit 844c141d10
1 changed files with 37 additions and 17 deletions

View File

@ -6,20 +6,32 @@ import sys
from _pytest.monkeypatch import monkeypatch from _pytest.monkeypatch import monkeypatch
from _pytest.assertion import util from _pytest.assertion import util
def pytest_addoption(parser): def pytest_addoption(parser):
group = parser.getgroup("debugconfig") group = parser.getgroup("debugconfig")
group.addoption('--assert', action="store", dest="assertmode", group.addoption('--assert',
action="store",
dest="assertmode",
choices=("rewrite", "reinterp", "plain",), choices=("rewrite", "reinterp", "plain",),
default="rewrite", metavar="MODE", default="rewrite",
help="""control assertion debugging tools. metavar="MODE",
'plain' performs no assertion debugging. help="""control assertion debugging tools. 'plain'
'reinterp' reinterprets assert statements after they failed to provide assertion expression information. performs no assertion debugging. 'reinterp'
'rewrite' (the default) rewrites assert statements in test modules on import reinterprets assert statements after they failed
to provide assert expression information. """) to provide assertion expression information.
group.addoption('--no-assert', action="store_true", default=False, 'rewrite' (the default) rewrites assert
dest="noassert", help="DEPRECATED equivalent to --assert=plain") statements in test modules on import to
group.addoption('--nomagic', '--no-magic', action="store_true", provide assert expression information. """)
default=False, help="DEPRECATED equivalent to --assert=plain") group.addoption('--no-assert',
action="store_true",
default=False,
dest="noassert",
help="DEPRECATED equivalent to --assert=plain")
group.addoption('--nomagic', '--no-magic',
action="store_true",
default=False,
help="DEPRECATED equivalent to --assert=plain")
class AssertionState: class AssertionState:
"""State for the assertion plugin.""" """State for the assertion plugin."""
@ -28,6 +40,7 @@ class AssertionState:
self.mode = mode self.mode = mode
self.trace = config.trace.root.get("assertion") self.trace = config.trace.root.get("assertion")
def pytest_configure(config): def pytest_configure(config):
mode = config.getvalue("assertmode") mode = config.getvalue("assertmode")
if config.getvalue("noassert") or config.getvalue("nomagic"): if config.getvalue("noassert") or config.getvalue("nomagic"):
@ -41,7 +54,7 @@ def pytest_configure(config):
# Both Jython and CPython 2.6.0 have AST bugs that make the # Both Jython and CPython 2.6.0 have AST bugs that make the
# assertion rewriting hook malfunction. # assertion rewriting hook malfunction.
if (sys.platform.startswith('java') or if (sys.platform.startswith('java') or
sys.version_info[:3] == (2, 6, 0)): sys.version_info[:3] == (2, 6, 0)):
mode = "reinterp" mode = "reinterp"
if mode != "plain": if mode != "plain":
_load_modules(mode) _load_modules(mode)
@ -58,11 +71,13 @@ def pytest_configure(config):
config._assertstate.hook = hook config._assertstate.hook = hook
config._assertstate.trace("configured with mode set to %r" % (mode,)) config._assertstate.trace("configured with mode set to %r" % (mode,))
def pytest_unconfigure(config): def pytest_unconfigure(config):
hook = config._assertstate.hook hook = config._assertstate.hook
if hook is not None and hook in sys.meta_path: if hook is not None and hook in sys.meta_path:
sys.meta_path.remove(hook) sys.meta_path.remove(hook)
def pytest_collection(session): def pytest_collection(session):
# this hook is only called when test modules are collected # this hook is only called when test modules are collected
# so for example not in the master process of pytest-xdist # so for example not in the master process of pytest-xdist
@ -71,11 +86,11 @@ def pytest_collection(session):
if hook is not None: if hook is not None:
hook.set_session(session) hook.set_session(session)
def pytest_runtest_setup(item): def pytest_runtest_setup(item):
def callbinrepr(op, left, right): def callbinrepr(op, left, right):
hook_result = item.ihook.pytest_assertrepr_compare( hook_result = item.ihook.pytest_assertrepr_compare(
config=item.config, op=op, left=left, right=right) config=item.config, op=op, left=left, right=right)
for new_expl in hook_result: for new_expl in hook_result:
if new_expl: if new_expl:
# Don't include pageloads of data unless we are very # Don't include pageloads of data unless we are very
@ -84,7 +99,7 @@ def pytest_runtest_setup(item):
and item.config.option.verbose < 2): and item.config.option.verbose < 2):
new_expl[1:] = [py.builtin._totext( new_expl[1:] = [py.builtin._totext(
'Detailed information truncated, use "-vv" to show')] 'Detailed information truncated, use "-vv" to show')]
res = py.builtin._totext('\n~').join(new_expl) res = py.builtin._totext("\n~").join(new_expl)
if item.config.getvalue("assertmode") == "rewrite": if item.config.getvalue("assertmode") == "rewrite":
# The result will be fed back a python % formatting # The result will be fed back a python % formatting
# operation, which will fail if there are extraneous # operation, which will fail if there are extraneous
@ -93,14 +108,17 @@ def pytest_runtest_setup(item):
return res return res
util._reprcompare = callbinrepr util._reprcompare = callbinrepr
def pytest_runtest_teardown(item): def pytest_runtest_teardown(item):
util._reprcompare = None util._reprcompare = None
def pytest_sessionfinish(session): def pytest_sessionfinish(session):
hook = session.config._assertstate.hook hook = session.config._assertstate.hook
if hook is not None: if hook is not None:
hook.session = None hook.session = None
def _load_modules(mode): def _load_modules(mode):
"""Lazily import assertion related code.""" """Lazily import assertion related code."""
global rewrite, reinterpret global rewrite, reinterpret
@ -108,6 +126,7 @@ def _load_modules(mode):
if mode == "rewrite": if mode == "rewrite":
from _pytest.assertion import rewrite # noqa from _pytest.assertion import rewrite # noqa
def warn_about_missing_assertion(mode): def warn_about_missing_assertion(mode):
try: try:
assert False assert False
@ -121,8 +140,9 @@ def warn_about_missing_assertion(mode):
specifically = "failing tests may report as passing" specifically = "failing tests may report as passing"
sys.stderr.write("WARNING: " + specifically + sys.stderr.write("WARNING: " + specifically +
" because assert statements are not executed " " because assert statements are not executed "
"by the underlying Python interpreter " "by the underlying Python interpreter "
"(are you using python -O?)\n") "(are you using python -O?)\n")
pytest_assertrepr_compare = util.assertrepr_compare pytest_assertrepr_compare = util.assertrepr_compare