diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2019-09-03 13:36:10 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2019-09-03 13:36:10 -0400 |
commit | d9bc47d0db97ea47ebbe1b775947049792e8aa00 (patch) | |
tree | fd7dddd7a52a112bbd10ac6c6b605cb8b4c5523c | |
parent | 48d95f9510328769faa174f569548f5c63513d95 (diff) | |
download | python-coveragepy-git-d9bc47d0db97ea47ebbe1b775947049792e8aa00.tar.gz |
Updating with an empty data is ok
With no arc or line data, it used to choose lines arbitrarily, which
would fail if an empty data was updating an arc data.
-rw-r--r-- | coverage/sqldata.py | 40 | ||||
-rw-r--r-- | tests/test_data.py | 16 |
2 files changed, 40 insertions, 16 deletions
diff --git a/coverage/sqldata.py b/coverage/sqldata.py index babfcb8d..06ccff52 100644 --- a/coverage/sqldata.py +++ b/coverage/sqldata.py @@ -411,6 +411,8 @@ class CoverageData(SimpleReprMixin): ) def _choose_lines_or_arcs(self, lines=False, arcs=False): + assert lines or arcs + assert not (lines and arcs) if lines and self._has_arcs: raise CoverageException("Can't add lines to existing arc data") if arcs and self._has_lines: @@ -480,6 +482,8 @@ class CoverageData(SimpleReprMixin): If `aliases` is provided, it's a `PathAliases` object that is used to re-map paths to match the local machine's. """ + if self._debug.should('dataop'): + self._debug.write("Updating with data from %r" % (getattr(other_data, '_filename', '???'),)) if self._has_lines and other_data._has_arcs: raise CoverageException("Can't combine arc data with line data") if self._has_arcs and other_data._has_lines: @@ -608,23 +612,27 @@ class CoverageData(SimpleReprMixin): lines[key] = numbits cur.close() - self._choose_lines_or_arcs(arcs=bool(arcs), lines=bool(lines)) + if arcs: + self._choose_lines_or_arcs(arcs=True) - # Write the combined data. - conn.executemany( - 'insert or ignore into arc ' - '(file_id, context_id, fromno, tono) values (?, ?, ?, ?)', - arc_rows - ) - conn.execute("delete from line_bits") - conn.executemany( - "insert into line_bits " - "(file_id, context_id, numbits) values (?, ?, ?)", - [ - (file_ids[file], context_ids[context], numbits) - for (file, context), numbits in lines.items() - ] - ) + # Write the combined data. + conn.executemany( + 'insert or ignore into arc ' + '(file_id, context_id, fromno, tono) values (?, ?, ?, ?)', + arc_rows + ) + + if lines: + self._choose_lines_or_arcs(lines=True) + conn.execute("delete from line_bits") + conn.executemany( + "insert into line_bits " + "(file_id, context_id, numbits) values (?, ?, ?)", + [ + (file_ids[file], context_ids[context], numbits) + for (file, context), numbits in lines.items() + ] + ) conn.executemany( 'insert or ignore into tracer (file_id, tracer) values (?, ?)', ((file_ids[filename], tracer) for filename, tracer in tracer_map.items()) diff --git a/tests/test_data.py b/tests/test_data.py index e09aaf44..eb389e35 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -428,6 +428,22 @@ class CoverageDataTest(DataTestHelpers, CoverageTest): with self.assertRaisesRegex(CoverageException, msg): covdata2.update(covdata1) + def test_update_lines_empty(self): + covdata1 = CoverageData(suffix='1') + covdata1.add_lines(LINES_1) + + covdata2 = CoverageData(suffix='2') + covdata1.update(covdata2) + self.assert_line_counts(covdata1, SUMMARY_1) + + def test_update_arcs_empty(self): + covdata1 = CoverageData(suffix='1') + covdata1.add_arcs(ARCS_3) + + covdata2 = CoverageData(suffix='2') + covdata1.update(covdata2) + self.assert_line_counts(covdata1, SUMMARY_3) + def test_asking_isnt_measuring(self): # Asking about an unmeasured file shouldn't make it seem measured. covdata = CoverageData() |