Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent plugin dependencies in "omit" from causing already-imported warnings #1044

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions coverage/inorout.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ def warn_already_imported_files(self):
if filename in warned:
continue

file_path = canonical_filename(filename)
if self.omit_match and self.omit_match.match(file_path):
continue

disp = self.should_trace(filename)
if disp.trace:
msg = "Already imported a file that will be measured: {}".format(filename)
Expand Down
68 changes: 68 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,36 @@ def coverage_init(reg, options):
out = self.run_command("coverage html")
assert out == ""

def test_omitted_traced_plugin_deps_dont_warn(self):
self.make_file("traces_own_deps_plugin.py", """\
from coverage import CoveragePlugin
import local_module
class MyPlugin(CoveragePlugin):
def file_tracer(self, filename):
if 'local_module' in filename:
return self
return None

def coverage_init(reg, options):
reg.add_noop(MyPlugin())
""")
self.make_file("local_module.py", "CONST = 1")
self.make_file(".coveragerc", """\
[run]
plugins = traces_own_deps_plugin
omit=local_module.py,traces_own_deps_plugin.py
source=.
""")
self.make_file("main_file.py", """\
import local_module
print('MAIN')
""")

out = self.run_command("coverage run main_file.py")
self.assertEqual(out, "MAIN\n")
out = self.run_command("coverage html")
self.assertEqual(out, "")


@pytest.mark.skipif(env.C_TRACER, reason="This test is only about PyTracer.")
class PluginWarningOnPyTracer(CoverageTest):
Expand Down Expand Up @@ -577,6 +607,44 @@ def coverage_init(reg, options):
cov.analysis("fictional.py")


def test_omitted_traced_plugin_deps_dont_warn(self):
self.make_file("traces_own_deps_plugin.py", """\
from coverage.plugin import CoveragePlugin, FileTracer
import local_module

class MyPlugin(CoveragePlugin,FileTracer):
def file_tracer(self, filename):
if 'local_module' in filename:
return self
return None

def has_dynamic_source_filename(self):
return True

def dynamic_source_filename(self, filename, frame):
return filename

def coverage_init(reg, options):
reg.add_file_tracer(MyPlugin())
""")
self.make_file("local_module.py", "CONST = 1")
self.make_file(".coveragerc", """\
[run]
plugins = traces_own_deps_plugin
omit=local_module.py,traces_own_deps_plugin.py
source=.
""")
self.make_file("main_file.py", """\
import local_module
print('MAIN')
""")

out = self.run_command("coverage run main_file.py")
self.assertEqual(out, "MAIN\n")
out = self.run_command("coverage html")
self.assertEqual(out, "")


class BadFileTracerTest(FileTracerTest):
"""Test error handling around file tracer plugins."""

Expand Down