Skip to content

Commit

Permalink
fix: CoverageData(no_disk=True).update() now works. #1323
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 21, 2022
1 parent 907d646 commit 84f70f6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -39,7 +39,10 @@ Unreleased
- On Python 3.11, the ``[toml]`` extra no longer installs tomli, instead using
tomllib from the standard library. Thanks `Shantanu <pull 1359_>`_.

- In-memory CoverageData objects now properly update(), closing `issue 1323`_.

.. _issue 1310: https://github.com/nedbat/coveragepy/issues/1310
.. _issue 1323: https://github.com/nedbat/coveragepy/issues/1323
.. _issue 1351: https://github.com/nedbat/coveragepy/issues/1351
.. _pull 1354: https://github.com/nedbat/coveragepy/pull/1354
.. _pull 1359: https://github.com/nedbat/coveragepy/pull/1359
Expand Down
12 changes: 7 additions & 5 deletions coverage/sqldata.py
Expand Up @@ -253,10 +253,10 @@ def _choose_filename(self):

def _reset(self):
"""Reset our attributes."""
if self._dbs:
if not self._no_disk:
for db in self._dbs.values():
db.close()
self._dbs = {}
self._dbs = {}
self._file_map = {}
self._have_used = False
self._current_context_id = None
Expand Down Expand Up @@ -679,6 +679,7 @@ def update(self, other_data, aliases=None):
path: id
for id, path in conn.execute("select id, path from file")
}
self._file_map.update(file_ids)
conn.executemany(
"insert or ignore into context (context) values (?)",
((context,) for context in contexts)
Expand Down Expand Up @@ -752,9 +753,10 @@ def update(self, other_data, aliases=None):
((file_ids[filename], tracer) for filename, tracer in tracer_map.items())
)

# Update all internal cache data.
self._reset()
self.read()
if not self._no_disk:
# Update all internal cache data.
self._reset()
self.read()

def erase(self, parallel=False):
"""Erase the data in this object.
Expand Down
16 changes: 16 additions & 0 deletions tests/test_data.py
Expand Up @@ -891,3 +891,19 @@ def test_misfed_serialization(self):
)
with pytest.raises(DataError, match=msg):
covdata.loads(bad_data)


class NoDiskTest(CoverageTest):
"""Tests of in-memory CoverageData."""

run_in_temp_dir = False

def test_updating(self):
# https://github.com/nedbat/coveragepy/issues/1323
a = CoverageData(no_disk=True)
a.add_lines({'foo.py': [10, 20, 30]})
assert a.measured_files() == {'foo.py'}

b = CoverageData(no_disk=True)
b.update(a)
assert b.measured_files() == {'foo.py'}

0 comments on commit 84f70f6

Please sign in to comment.