Respect PYTEST_DONT_REWRITE for plugins too.

This commit is contained in:
Antony Lee 2017-12-12 17:39:10 -08:00
parent 5c6d7739bc
commit c8e7d1ae34
4 changed files with 21 additions and 7 deletions

View File

@ -179,10 +179,11 @@ class AssertionRewritingHook(object):
The named module or package as well as any nested modules will The named module or package as well as any nested modules will
be rewritten on import. be rewritten on import.
""" """
already_imported = set(names).intersection(set(sys.modules)) already_imported = (
if already_imported: (set(names) & set(sys.modules)) - set(self._rewritten_names))
for name in already_imported: for name in already_imported:
if name not in self._rewritten_names: if not AssertionRewriter.is_rewrite_disabled(
sys.modules[name].__doc__ or ""):
self._warn_already_imported(name) self._warn_already_imported(name)
self._must_rewrite.update(names) self._must_rewrite.update(names)
@ -635,7 +636,8 @@ class AssertionRewriter(ast.NodeVisitor):
not isinstance(field, ast.expr)): not isinstance(field, ast.expr)):
nodes.append(field) nodes.append(field)
def is_rewrite_disabled(self, docstring): @staticmethod
def is_rewrite_disabled(docstring):
return "PYTEST_DONT_REWRITE" in docstring return "PYTEST_DONT_REWRITE" in docstring
def variable(self): def variable(self):

View File

@ -347,7 +347,7 @@ class RunResult:
:stdout: :py:class:`LineMatcher` of stdout, use ``stdout.str()`` to :stdout: :py:class:`LineMatcher` of stdout, use ``stdout.str()`` to
reconstruct stdout or the commonly used ``stdout.fnmatch_lines()`` reconstruct stdout or the commonly used ``stdout.fnmatch_lines()``
method method
:stderrr: :py:class:`LineMatcher` of stderr :stderr: :py:class:`LineMatcher` of stderr
:duration: duration in seconds :duration: duration in seconds
""" """

2
changelog/2995.bugfix Normal file
View File

@ -0,0 +1,2 @@
PYTEST_DONT_REWRITE is now checked for plugins too (rather than only for
test modules).

View File

@ -128,6 +128,16 @@ class TestAssertionRewrite(object):
assert len(m.body) == 1 assert len(m.body) == 1
assert m.body[0].msg is None assert m.body[0].msg is None
def test_dont_rewrite_plugin(self, testdir):
contents = {
"conftest.py": "pytest_plugins = 'plugin'; import plugin",
"plugin.py": "'PYTEST_DONT_REWRITE'",
"test_foo.py": "def test_foo(): pass",
}
testdir.makepyfile(**contents)
result = testdir.runpytest_subprocess()
assert "warnings" not in "".join(result.outlines)
def test_name(self): def test_name(self):
def f(): def f():
assert False assert False