diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2018-08-10 16:15:00 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2018-08-10 16:15:00 -0400 |
commit | 90bb6a77e02cbac6a23723b5907d5f59d1db1b82 (patch) | |
tree | 35fcc2fc1063b0142985ca17149abbe83fdb5d09 | |
parent | 02b2bd8afe3cd171e4bd454ccf244f788ccded3c (diff) | |
download | python-coveragepy-git-90bb6a77e02cbac6a23723b5907d5f59d1db1b82.tar.gz |
Move a common method outside the data classes
-rw-r--r-- | coverage/data.py | 29 | ||||
-rw-r--r-- | coverage/html.py | 3 | ||||
-rw-r--r-- | coverage/sqldata.py | 7 | ||||
-rw-r--r-- | tests/test_data.py | 9 |
4 files changed, 29 insertions, 19 deletions
diff --git a/coverage/data.py b/coverage/data.py index db9cd526..9c82ccef 100644 --- a/coverage/data.py +++ b/coverage/data.py @@ -641,20 +641,6 @@ class CoverageJsonData(object): for key in val: assert isinstance(key, string_class), "Key in _runs shouldn't be %r" % (key,) - def add_to_hash(self, filename, hasher): - """Contribute `filename`'s data to the `hasher`. - - `hasher` is a `coverage.misc.Hasher` instance to be updated with - the file's data. It should only get the results data, not the run - data. - - """ - if self._has_arcs(): - hasher.update(sorted(self.arcs(filename) or [])) - else: - hasher.update(sorted(self.lines(filename) or [])) - hasher.update(self.file_tracer(filename)) - ## ## Internal ## @@ -676,6 +662,21 @@ elif which == "sql": CoverageData = CoverageSqliteData +def add_data_to_hash(data, filename, hasher): + """Contribute `filename`'s data to the `hasher`. + + `hasher` is a `coverage.misc.Hasher` instance to be updated with + the file's data. It should only get the results data, not the run + data. + + """ + if data.has_arcs(): + hasher.update(sorted(data.arcs(filename) or [])) + else: + hasher.update(sorted(data.lines(filename) or [])) + hasher.update(data.file_tracer(filename)) + + def combine_parallel_data(data, aliases=None, data_paths=None, strict=False): """Combine a number of data files together. diff --git a/coverage/html.py b/coverage/html.py index 186e9d22..2acc2656 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -12,6 +12,7 @@ import shutil import coverage from coverage import env from coverage.backward import iitems +from coverage.data import add_data_to_hash from coverage.files import flat_rootname from coverage.misc import CoverageException, file_be_gone, Hasher, isolate_module from coverage.report import Reporter @@ -169,7 +170,7 @@ class HtmlReporter(Reporter): """Compute a hash that changes if the file needs to be re-reported.""" m = Hasher() m.update(source) - self.data.add_to_hash(fr.filename, m) + add_data_to_hash(self.data, fr.filename, m) return m.hexdigest() def html_file(self, fr, analysis): diff --git a/coverage/sqldata.py b/coverage/sqldata.py index 80188fca..25a6d62d 100644 --- a/coverage/sqldata.py +++ b/coverage/sqldata.py @@ -242,6 +242,13 @@ class CoverageSqliteData(SimpleRepr): return "" # TODO def lines(self, filename): + if self.has_arcs(): + arcs = self.arcs(filename) + if arcs is not None: + import itertools + all_lines = itertools.chain.from_iterable(arcs) + return list(set(l for l in all_lines if l > 0)) + with self._connect() as con: file_id = self._file_id(filename) return [lineno for lineno, in con.execute("select lineno from line where file_id = ?", (file_id,))] diff --git a/tests/test_data.py b/tests/test_data.py index 68b2c375..a450f90b 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -12,6 +12,7 @@ import re import mock from coverage.data import CoverageData, debug_main, canonicalize_json_data, combine_parallel_data +from coverage.data import add_data_to_hash from coverage.debug import DebugControlString from coverage.files import PathAliases, canonical_filename from coverage.misc import CoverageException @@ -364,7 +365,7 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): covdata = CoverageData() covdata.add_lines(LINES_1) hasher = mock.Mock() - covdata.add_to_hash("a.py", hasher) + add_data_to_hash(covdata, "a.py", hasher) self.assertEqual(hasher.method_calls, [ mock.call.update([1, 2]), # lines mock.call.update(""), # file_tracer name @@ -375,7 +376,7 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): covdata.add_arcs(ARCS_3) covdata.add_file_tracers({"y.py": "hologram_plugin"}) hasher = mock.Mock() - covdata.add_to_hash("y.py", hasher) + add_data_to_hash(covdata, "y.py", hasher) self.assertEqual(hasher.method_calls, [ mock.call.update([(-1, 17), (17, 23), (23, -1)]), # arcs mock.call.update("hologram_plugin"), # file_tracer name @@ -386,7 +387,7 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): covdata = CoverageData() covdata.add_lines(LINES_1) hasher = mock.Mock() - covdata.add_to_hash("missing.py", hasher) + add_data_to_hash(covdata, "missing.py", hasher) self.assertEqual(hasher.method_calls, [ mock.call.update([]), mock.call.update(None), @@ -398,7 +399,7 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): covdata.add_arcs(ARCS_3) covdata.add_file_tracers({"y.py": "hologram_plugin"}) hasher = mock.Mock() - covdata.add_to_hash("missing.py", hasher) + add_data_to_hash(covdata, "missing.py", hasher) self.assertEqual(hasher.method_calls, [ mock.call.update([]), mock.call.update(None), |