Skip to content

Commit 27d8255

Browse files
committed
fix: don't warn that dynamic plugins already imported their source files. #1150
1 parent b1b2b8b commit 27d8255

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

CHANGES.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ want to know what's different in 5.0 since 4.5.x, see :ref:`whatsnew5x`.
2424
Unreleased
2525
----------
2626

27-
- Nothing yet.
27+
- Plugins (like the `Django coverage plugin`_) were generating "Already
28+
imported a file that will be measured" warnings about Django itself. These
29+
have been fixed, closing `issue 1150`_.
30+
31+
.. _Django coverage plugin: https://pypi.org/project/django-coverage-plugin/
32+
.. _issue 1150: https://github.com/nedbat/coveragepy/issues/1150
2833

2934

3035
.. _changes_56b1:

coverage/inorout.py

+5
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ def warn_already_imported_files(self):
481481
continue
482482

483483
disp = self.should_trace(filename)
484+
if disp.has_dynamic_filename:
485+
# A plugin with dynamic filenames: the Python file
486+
# shouldn't cause a warning, since it won't be the subject
487+
# of tracing anyway.
488+
continue
484489
if disp.trace:
485490
msg = "Already imported a file that will be measured: {}".format(filename)
486491
self.warn(msg, slug="already-imported")

tests/plugin2.py

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
import coverage
99

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

1119
class Plugin(coverage.CoveragePlugin):
1220
"""A file tracer plugin for testing."""

tests/test_process.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1673,12 +1673,21 @@ def venv_world_fixture(tmp_path_factory):
16731673
# Create a virtualenv.
16741674
run_command("python -m virtualenv venv")
16751675

1676-
# A third-party package that installs two different packages.
1676+
# A third-party package that installs a few different packages.
16771677
make_file("third_pkg/third/__init__.py", """\
16781678
import fourth
16791679
def third(x):
16801680
return 3 * x
16811681
""")
1682+
# Use plugin2.py as third.plugin
1683+
with open(os.path.join(os.path.dirname(__file__), "plugin2.py")) as f:
1684+
make_file("third_pkg/third/plugin.py", f.read())
1685+
# A render function for plugin2 to use for dynamic file names.
1686+
make_file("third_pkg/third/render.py", """\
1687+
def render(filename, linenum):
1688+
return "HTML: {}@{}".format(filename, linenum)
1689+
""")
1690+
# Another package that third can use.
16821691
make_file("third_pkg/fourth/__init__.py", """\
16831692
def fourth(x):
16841693
return 4 * x
@@ -1805,3 +1814,20 @@ def test_venv_isnt_measured(self, coverage_command):
18051814
assert "third" not in out
18061815
assert "coverage" not in out
18071816
assert "colorsys" not in out
1817+
1818+
@pytest.mark.skipif(not env.C_TRACER, reason="Plugins are only supported with the C tracer.")
1819+
def test_venv_with_dynamic_plugin(self, coverage_command):
1820+
# https://github.com/nedbat/coveragepy/issues/1150
1821+
# Django coverage plugin was incorrectly getting warnings:
1822+
# "Already imported: ... django/template/blah.py"
1823+
# It happened because coverage imported the plugin, which imported
1824+
# Django, and then the Django files were reported as traceable.
1825+
self.make_file(".coveragerc", "[run]\nplugins=third.plugin\n")
1826+
self.make_file("myrender.py", """\
1827+
import third.render
1828+
print(third.render.render("hello.html", 1723))
1829+
""")
1830+
out = run_in_venv(coverage_command + " run --source=. myrender.py")
1831+
# The output should not have this warning:
1832+
# Already imported a file that will be measured: ...third/render.py (already-imported)
1833+
assert out == "HTML: hello.html@1723\n"

0 commit comments

Comments
 (0)