Skip to content

Commit

Permalink
fix: don't warn that dynamic plugins already imported their source fi…
Browse files Browse the repository at this point in the history
…les. #1150
  • Loading branch information
nedbat committed Apr 30, 2021
1 parent 78fa3d9 commit 991d2a3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
7 changes: 6 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ want to know what's different in 5.0 since 4.5.x, see :ref:`whatsnew5x`.
Unreleased
----------

- Nothing yet.
- Plugins (like the `Django coverage plugin`_) were generating "Already
imported a file that will be measured" warnings about Django itself. These
have been fixed, closing `issue 1150`_.

.. _Django coverage plugin: https://pypi.org/project/django-coverage-plugin/
.. _issue 1150: https://github.com/nedbat/coveragepy/issues/1150


.. _changes_56b1:
Expand Down
5 changes: 5 additions & 0 deletions coverage/inorout.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ def warn_already_imported_files(self):
continue

disp = self.should_trace(filename)
if disp.has_dynamic_filename:
# A plugin with dynamic filenames: the Python file
# shouldn't cause a warning, since it won't be the subject
# of tracing anyway.
continue
if disp.trace:
msg = "Already imported a file that will be measured: {}".format(filename)
self.warn(msg, slug="already-imported")
Expand Down
8 changes: 8 additions & 0 deletions tests/plugin2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

import coverage

try:
import third.render # pylint: disable=unused-import
except ImportError:
# This plugin is used in a few tests. One of them has the third.render
# module, but most don't. We need to import it but not use it, so just
# try importing it and it's OK if the module doesn't exist.
pass


class Plugin(coverage.CoveragePlugin):
"""A file tracer plugin for testing."""
Expand Down
28 changes: 27 additions & 1 deletion tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,12 +1673,21 @@ def venv_world_fixture(tmp_path_factory):
# Create a virtualenv.
run_command("python -m virtualenv venv")

# A third-party package that installs two different packages.
# A third-party package that installs a few different packages.
make_file("third_pkg/third/__init__.py", """\
import fourth
def third(x):
return 3 * x
""")
# Use plugin2.py as third.plugin
with open(os.path.join(os.path.dirname(__file__), "plugin2.py")) as f:
make_file("third_pkg/third/plugin.py", f.read())
# A render function for plugin2 to use for dynamic file names.
make_file("third_pkg/third/render.py", """\
def render(filename, linenum):
return "HTML: {}@{}".format(filename, linenum)
""")
# Another package that third can use.
make_file("third_pkg/fourth/__init__.py", """\
def fourth(x):
return 4 * x
Expand Down Expand Up @@ -1805,3 +1814,20 @@ def test_venv_isnt_measured(self, coverage_command):
assert "third" not in out
assert "coverage" not in out
assert "colorsys" not in out

@pytest.mark.skipif(not env.C_TRACER, reason="Plugins are only supported with the C tracer.")
def test_venv_with_dynamic_plugin(self, coverage_command):
# https://github.com/nedbat/coveragepy/issues/1150
# Django coverage plugin was incorrectly getting warnings:
# "Already imported: ... django/template/blah.py"
# It happened because coverage imported the plugin, which imported
# Django, and then the Django files were reported as traceable.
self.make_file(".coveragerc", "[run]\nplugins=third.plugin\n")
self.make_file("myrender.py", """\
import third.render
print(third.render.render("hello.html", 1723))
""")
out = run_in_venv(coverage_command + " run --source=. myrender.py")
# The output should not have this warning:
# Already imported a file that will be measured: ...third/render.py (already-imported)
assert out == "HTML: hello.html@1723\n"

0 comments on commit 991d2a3

Please sign in to comment.