10000 Add method purge_contexts to CoverageData · nedbat/coveragepy@81321e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81321e8

Browse files
committed
Add method purge_contexts to CoverageData
1 parent 499f4f5 commit 81321e8

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

coverage/sqldata.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,31 @@ def purge_files(self, filenames: Collection[str]) -> None:
641641
continue
642642
con.execute_void(sql, (file_id,))
643643

644+
def purge_contexts(self, contexts):
645+
"""Purge any existing coverage data for the given `contexts`.
646+
647+
This removes all coverage data for the contexts, but does not remove
648+
them from the list returned by measured_contexts(), so that context_ids
649+
for the contexts remain constant over time."""
650+
651+
if self._debug.should("dataop"):
652+
self._debug.write("Purging contexts {contexts}")
653+
self._start_using()
654+
with self._connect() as con:
655+
656+
if self._has_lines:
657+
sql = "delete from line_bits where context_id=?"
658+
elif self._has_arcs:
659+
sql = "delete from arc where context_id=?"
660+
else:
661+
raise DataError("Can't purge contexts in an empty CoverageData")
662+
663+
for context in contexts:
664+
context_id = self._context_id(context)
665+
if context_id is None:
666+
continue
667+
con.execute_void(sql, (context_id,))
668+
644669
def update(self, other_data: CoverageData, aliases: Optional[PathAliases] = None) -> None:
645670
"""Update this data with data from several other :class:`CoverageData` instances.
646671

tests/test_data.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,38 @@ def test_cant_purge_in_empty_data(self) -> None:
617617
with pytest.raises(DataError, match=msg):
618618
covdata.purge_files(["abc.py"])
619619

620+
def test_purge_contexts_lines(self) -> None:
621+
covdata = DebugCoverageData()
622+
covdata.set_context('context1')
623+
covdata.add_lines(LINES_1)
624+
covdata.set_context('context2')
625+
covdata.add_lines(LINES_2)
626+
assert_line_counts(covdata, SUMMARY_1_2)
627+
covdata.purge_contexts(["context2"])
628+
assert_line_counts(covdata, {'a.py': 2, 'b.py': 1, 'c.py': 0})
629+
covdata.purge_contexts(["context1"])
630+
assert_line_counts(covdata, {"a.py": 0, "b.py": 0, "c.py": 0})
631+
# It's OK to "purge" a context that wasn't measured.
632+
covdata.purge_contexts(["context3"])
633+
assert_line_counts(covdata, {"a.py": 0, "b.py": 0, "c.py": 0})
634+
635+
def test_purge_contexts_arcs(self) -> None:
636+
covdata = CoverageData()
637+
covdata.set_context('context1')
638+
covdata.add_arcs(ARCS_3)
639+
covdata.set_context('context2')
640+
covdata.add_arcs(ARCS_4)
641+
assert_line_counts(covdata, SUMMARY_3_4)
642+
covdata.purge_contexts(["context1"])
643+
assert_line_counts(covdata,{'x.py': 2, 'y.py': 0, 'z.py': 1})
644+
covdata.purge_contexts(["context2"])
645+
assert_line_counts(covdata, {"x.py": 0, "y.py": 0, "z.py": 0})
646+
647+
def test_cant_purge_contexts_in_empty_data(self) -> None:
648+
covdata = DebugCoverageData()
649+
msg = "Can't purge contexts in an empty CoverageData"
650+
with pytest.raises(DataError, match=msg):
651+
covdata.purge_contexts(["context1"])
620652

621653
class CoverageDataInTempDirTest(CoverageTest):
622654
"""Tests of CoverageData that need a temporary directory to make files."""

0 commit comments

Comments
 (0)
0