Skip to content

Commit

Permalink
Merge pull request #2011 from nicoddemus/false-rewrite-warnings
Browse files Browse the repository at this point in the history
Fix false-positive warnings from assertion rewrite hook
  • Loading branch information
RonnyPfannschmidt authored Oct 24, 2016
2 parents 4e9c633 + 2a2b8ce commit 35d154f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

*

*

*


Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion _pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 35d154f

Please sign in to comment.