diff --git a/coverage/sqldata.py b/coverage/sqldata.py index 12676d0bb..e2ec0a3b1 100644 --- a/coverage/sqldata.py +++ b/coverage/sqldata.py @@ -615,24 +615,19 @@ def touch_files(self, filenames: Collection[str], plugin_name: Optional[str] = N # 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: + def purge_files(self, filenames: Iterable[str]) -> None: """Purge any existing coverage data for the given `filenames`. - If `context` is given, purge only data associated with that measurement context. + This removes all coverage data for the files, but does not remove + them from the list returned my measured_files(), so the file_ids + for the files will remain constant over time. """ if self._debug.should("dataop"): - self._debug.write(f"Purging {filenames!r} for context {context}") + self._debug.write(f"Purging {filenames!r}") 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: @@ -644,12 +639,8 @@ def purge_files(self, filenames: Iterable[str], context: Optional[str] = None) - 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) + q = f'delete from {table} where file_id={file_id}' + con.execute_void(q) def update(self, other_data: CoverageData, aliases: Optional[PathAliases] = None) -> None: """Update this data with data from several other :class:`CoverageData` instances. diff --git a/tests/test_api.py b/tests/test_api.py index 885831550..d278d2da7 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -777,15 +777,16 @@ def test_purge_filenames(self) -> None: assert [1, 2] == sorted_lines(data, fn1) assert [1,] == sorted_lines(data, fn2) - # Purge one file's data and one should remain + # Purge one file's data and only the other should have data, but + # both are still listed as measured data.purge_files([fn1]) - assert len(data.measured_files()) == 1 + assert len(data.measured_files()) == 2 assert [] == sorted_lines(data, fn1) assert [1,] == sorted_lines(data, fn2) - # Purge second file's data and none should remain + # Purge second file's data data.purge_files([fn2]) - assert len(data.measured_files()) == 0 + assert len(data.measured_files()) == 2 assert [] == sorted_lines(data, fn1) assert [] == sorted_lines(data, fn2) @@ -805,7 +806,7 @@ def test_purge_filenames_context(self) -> None: def dummy_function() -> None: unused = 42 - # Start/stop since otherwise cantext + # Start/stop since otherwise context cannot be set cov = coverage.Coverage() cov.start() cov.switch_context('initialcontext') @@ -823,38 +824,25 @@ def dummy_function() -> None: assert len(sorted_lines(data, __file__)) == 1 assert len(data.measured_contexts()) == 2 - # Remove specifying wrong context should raise exception and not remove anything - try: - data.purge_files([fn1], 'wrongcontext') - except coverage.sqldata.DataError: - pass - else: - assert 0, "exception expected" + # Remove one file; measurements are gone but files and contexts remain registered + data.purge_files([fn1]) assert len(data.measured_files()) == 3 - assert [1, 2] == sorted_lines(data, fn1) - assert [1,] == sorted_lines(data, fn2) - assert len(sorted_lines(data, __file__)) == 1 - assert len(data.measured_contexts()) == 2 - - # Remove one file specifying correct context - data.purge_files([fn1], 'testcontext') - assert len(data.measured_files()) == 2 assert [] == sorted_lines(data, fn1) assert [1,] == sorted_lines(data, fn2) assert len(sorted_lines(data, __file__)) == 1 assert len(data.measured_contexts()) == 2 - # Remove second file with other correct context - data.purge_files([__file__], 'initialcontext') - assert len(data.measured_files()) == 1 + # Remove second file + data.purge_files([__file__]) + assert len(data.measured_files()) == 3 assert [] == sorted_lines(data, fn1) assert [1,] == sorted_lines(data, fn2) assert len(sorted_lines(data, __file__)) == 0 assert len(data.measured_contexts()) == 2 - # Remove last file specifying correct context - data.purge_files([fn2], 'testcontext') - assert len(data.measured_files()) == 0 + # Remove last file + data.purge_files([fn2]) + assert len(data.measured_files()) == 3 assert [] == sorted_lines(data, fn1) assert [] == sorted_lines(data, fn2) assert len(sorted_lines(data, __file__)) == 0