summaryrefslogtreecommitdiff
path: root/coverage/sqldata.py
diff options
context:
space:
mode:
authorWingware Development <59840981+wingware-dev@users.noreply.github.com>2023-02-04 18:20:20 -0500
committerGitHub <noreply@github.com>2023-02-04 15:20:20 -0800
commita4cd6a01f766359fbe8ff159e0ed87e03e8023ed (patch)
tree405c55182ecbe4f729c1faf01d9d78018cd33efc /coverage/sqldata.py
parent441823f18f705b8a79829cca36142a7f1511477b (diff)
downloadpython-coveragepy-git-a4cd6a01f766359fbe8ff159e0ed87e03e8023ed.tar.gz
feat: add purge_files method to CoverageData + unit tests for it (#1547)
* Add purge_files method to CoverageData, to allow for selective removal and update of coverage data. * Fix assert syntax so it's not true; this code isn't reached in the test unless it fails and then it would have failed to fail. * Remove trailing whitespace; did not expect this would matter on a blank line. * Add type annotations required by mypy --------- Co-authored-by: Stephan Deibel <sdeibel@wingware.com>
Diffstat (limited to 'coverage/sqldata.py')
-rw-r--r--coverage/sqldata.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/coverage/sqldata.py b/coverage/sqldata.py
index 77577437..12676d0b 100644
--- a/coverage/sqldata.py
+++ b/coverage/sqldata.py
@@ -615,6 +615,42 @@ class CoverageData(AutoReprMixin):
# Set the tracer for this file
self.add_file_tracers({filename: plugin_name})
+ def purge_files(self, filenames: Iterable[str], context: Optional[str] = None) -> None:
+ """Purge any existing coverage data for the given `filenames`.
+
+ If `context` is given, purge only data associated with that measurement context.
+ """
+
+ if self._debug.should("dataop"):
+ self._debug.write(f"Purging {filenames!r} for context {context}")
+ self._start_using()
+ with self._connect() as con:
+
+ if context is not None:
+ context_id = self._context_id(context)
+ if context_id is None:
+ raise DataError("Unknown context {context}")
+ else:
+ context_id = None
+
+ if self._has_lines:
+ table = 'line_bits'
+ elif self._has_arcs:
+ table = 'arcs'
+ else:
+ return
+
+ for filename in filenames:
+ file_id = self._file_id(filename, add=False)
+ if file_id is None:
+ continue
+ self._file_map.pop(filename, None)
+ if context_id is None:
+ q = f'delete from {table} where file_id={file_id}'
+ else:
+ q = f'delete from {table} where file_id={file_id} and context_id={context_id}'
+ con.execute(q)
+
def update(self, other_data: CoverageData, aliases: Optional[PathAliases] = None) -> None:
"""Update this data with data from several other :class:`CoverageData` instances.