summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-07-20 08:40:23 -0400
committerNed Batchelder <ned@nedbatchelder.com>2015-07-20 08:40:23 -0400
commitc283ccda4cdd2eea3a22a06619d0460cbc8440cb (patch)
treef19d4e1c62df04808620667f0fa28be7a8397876
parentb53778a41bb8f799008adc3284c8f1da86fe860c (diff)
downloadpython-coveragepy-git-c283ccda4cdd2eea3a22a06619d0460cbc8440cb.tar.gz
Be more strict when recording plugin names
-rw-r--r--coverage/data.py15
-rw-r--r--tests/test_data.py22
2 files changed, 36 insertions, 1 deletions
diff --git a/coverage/data.py b/coverage/data.py
index e5c37cb7..b1fcd212 100644
--- a/coverage/data.py
+++ b/coverage/data.py
@@ -216,7 +216,20 @@ class CoverageData(object):
`plugin_data` is { filename: plugin_name, ... }
"""
- self._plugins.update(plugin_data)
+ existing_files = self._arcs or self._lines
+ for filename, plugin_name in iitems(plugin_data):
+ if filename not in existing_files:
+ raise CoverageException(
+ "Can't add plugin data for unmeasured file '%s'" % (filename,)
+ )
+ existing_plugin = self._plugins.get(filename)
+ if existing_plugin is not None and plugin_name != existing_plugin:
+ raise CoverageException(
+ "Conflicting plugin name for '%s': %r vs %r" % (
+ filename, existing_plugin, plugin_name,
+ )
+ )
+ self._plugins[filename] = plugin_name
def update(self, other_data, aliases=None):
"""Update this data with data from another `CoverageData`.
diff --git a/tests/test_data.py b/tests/test_data.py
index 7083e69a..da16bd76 100644
--- a/tests/test_data.py
+++ b/tests/test_data.py
@@ -165,6 +165,25 @@ class CoverageDataTest(DataTestHelpers, CoverageTest):
self.assertEqual(covdata.plugin_name("main.py"), "")
self.assertIsNone(covdata.plugin_name("p3.not_here"))
+ def test_cant_plugin_unmeasured_files(self):
+ covdata = CoverageData()
+ msg = "Can't add plugin data for unmeasured file 'p1.foo'"
+ with self.assertRaisesRegex(CoverageException, msg):
+ covdata.add_plugins({"p1.foo": "p1.plugin"})
+
+ covdata.add_lines({"p2.html": dict.fromkeys([10, 11, 12])})
+ with self.assertRaisesRegex(CoverageException, msg):
+ covdata.add_plugins({"p1.foo": "p1.plugin"})
+
+ def test_cant_change_plugin_name(self):
+ covdata = CoverageData()
+ covdata.add_lines({"p1.foo": dict.fromkeys([1, 2, 3])})
+ covdata.add_plugins({"p1.foo": "p1.plugin"})
+
+ msg = "Conflicting plugin name for 'p1.foo': 'p1.plugin' vs 'p1.plugin.foo'"
+ with self.assertRaisesRegex(CoverageException, msg):
+ covdata.add_plugins({"p1.foo": "p1.plugin.foo"})
+
def test_update_lines(self):
covdata1 = CoverageData()
covdata1.add_lines(LINES_1)
@@ -251,6 +270,9 @@ class CoverageDataTest(DataTestHelpers, CoverageTest):
with self.assertRaises(CoverageException):
covdata1.update(covdata2)
+ with self.assertRaises(CoverageException):
+ covdata2.update(covdata1)
+
def test_update_plugin_vs_no_plugin(self):
covdata1 = CoverageData()
covdata1.add_lines({"p1.html": dict.fromkeys([1, 2, 3])})