diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 237ea478fac..6163c599614 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,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. + +* + +* + * @@ -16,7 +25,7 @@ .. _#1976: https://github.com/pytest-dev/pytest/issues/1976 .. _#1998: https://github.com/pytest-dev/pytest/issues/1998 - +.. _#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 80d6ee3ba73..6b4c1f48357 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -51,6 +51,7 @@ def __init__(self, config): 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 @@ def find_module(self, name, path=None): 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 @@ def mark_rewrite(self, *names): """ 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 cedd435f881..eb5070862d1 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_rewrite_module_warning_once='') + warnings = [] + hook = AssertionRewritingHook(pytestconfig) + monkeypatch.setattr(hook.config, 'warn', lambda code, msg: warnings.append(msg)) + hook.find_module('test_rewrite_module_warning_once') + hook.load_module('test_rewrite_module_warning_once') + hook.mark_rewrite('test_rewrite_module_warning_once') + hook.mark_rewrite('test_rewrite_module_warning_once') + assert warnings == [] + class TestAssertionRewriteHookDetails(object): def test_loader_is_package_false_for_module(self, testdir):