summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRonny Pfannschmidt <opensource@ronnypfannschmidt.de>2016-10-24 12:19:23 +0200
committerGitHub <noreply@github.com>2016-10-24 12:19:23 +0200
commit35d154f58058573ccfde431e558f00d2420c2530 (patch)
tree96c3955424ccc3a826e4bcac1503a9c6e4cc8b04
parent4e9c633185e3537f108d3229b39bb92528075a11 (diff)
parent2a2b8cee09a1a6b706423f7655129e17d7e0383d (diff)
downloadpytest-35d154f58058573ccfde431e558f00d2420c2530.tar.gz
Merge pull request #2011 from nicoddemus/false-rewrite-warnings
Fix false-positive warnings from assertion rewrite hook
-rw-r--r--CHANGELOG.rst11
-rw-r--r--_pytest/assertion/rewrite.py7
-rw-r--r--testing/test_assertrewrite.py16
3 files changed, 32 insertions, 2 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 2fb15aa11..3abfc04f6 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -12,6 +12,15 @@
* When loading plugins, import errors which contain non-ascii messages are now properly handled in Python 2 (`#1998`_).
Thanks `@nicoddemus`_ for the PR.
+* Fixed false-positives warnings from assertion rewrite hook for modules that were rewritten but
+ were later marked explicitly by ``pytest.register_assert_rewrite``
+ or implicitly as a plugin (`#2005`_).
+ Thanks `@RonnyPfannschmidt`_ for the report and `@nicoddemus`_ for the PR.
+
+*
+
+*
+
*
@@ -21,7 +30,7 @@
.. _#1976: https://github.com/pytest-dev/pytest/issues/1976
.. _#1998: https://github.com/pytest-dev/pytest/issues/1998
.. _#2004: https://github.com/pytest-dev/pytest/issues/2004
-
+.. _#2005: https://github.com/pytest-dev/pytest/issues/2005
3.0.3
diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py
index 80d6ee3ba..6b4c1f483 100644
--- a/_pytest/assertion/rewrite.py
+++ b/_pytest/assertion/rewrite.py
@@ -51,6 +51,7 @@ class AssertionRewritingHook(object):
self.fnpats = config.getini("python_files")
self.session = None
self.modules = {}
+ self._rewritten_names = set()
self._register_with_pkg_resources()
self._must_rewrite = set()
@@ -92,6 +93,8 @@ class AssertionRewritingHook(object):
if not self._should_rewrite(name, fn_pypath, state):
return None
+ self._rewritten_names.add(name)
+
# The requested module looks like a test file, so rewrite it. This is
# the most magical part of the process: load the source, rewrite the
# asserts, and load the rewritten source. We also cache the rewritten
@@ -178,7 +181,9 @@ class AssertionRewritingHook(object):
"""
already_imported = set(names).intersection(set(sys.modules))
if already_imported:
- self._warn_already_imported(already_imported)
+ for name in names:
+ if name not in self._rewritten_names:
+ self._warn_already_imported(already_imported)
self._must_rewrite.update(names)
def _warn_already_imported(self, names):
diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py
index cedd435f8..e72266a18 100644
--- a/testing/test_assertrewrite.py
+++ b/testing/test_assertrewrite.py
@@ -543,6 +543,22 @@ def test_rewritten():
''')
assert testdir.runpytest_subprocess().ret == 0
+ def test_remember_rewritten_modules(self, pytestconfig, testdir, monkeypatch):
+ """
+ AssertionRewriteHook should remember rewritten modules so it
+ doesn't give false positives (#2005).
+ """
+ monkeypatch.syspath_prepend(testdir.tmpdir)
+ testdir.makepyfile(test_remember_rewritten_modules='')
+ warnings = []
+ hook = AssertionRewritingHook(pytestconfig)
+ monkeypatch.setattr(hook.config, 'warn', lambda code, msg: warnings.append(msg))
+ hook.find_module('test_remember_rewritten_modules')
+ hook.load_module('test_remember_rewritten_modules')
+ hook.mark_rewrite('test_remember_rewritten_modules')
+ hook.mark_rewrite('test_remember_rewritten_modules')
+ assert warnings == []
+
class TestAssertionRewriteHookDetails(object):
def test_loader_is_package_false_for_module(self, testdir):