Skip to content

Removed contexts are from purge_files and don't pop from _file_map #1551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions coverage/sqldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
40 changes: 14 additions & 26 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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')
Expand All @@ -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
Expand Down