Skip to content

Commit e6df5b3

Browse files
committed
fix: don't create a data file when just trying to read one. #1328
1 parent d849b25 commit e6df5b3

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ Version 6.4 — 2022-05-22
4141
- A new debug option ``debug=sqldata`` adds more detail to ``debug=sql``,
4242
logging all the data being written to the database.
4343

44+
- Previously, running ``coverage report`` (or any of the reporting commands) in
45+
an empty directory would create a .coverage data file. Now they do not,
46+
fixing `issue 1328`_.
47+
4448
- On Python 3.11, the ``[toml]`` extra no longer installs tomli, instead using
4549
tomllib from the standard library. Thanks `Shantanu <pull 1359_>`_.
4650

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

4953
.. _issue 1310: https://github.com/nedbat/coveragepy/issues/1310
5054
.. _issue 1323: https://github.com/nedbat/coveragepy/issues/1323
55+
.. _issue 1328: https://github.com/nedbat/coveragepy/issues/1328
5156
.. _issue 1351: https://github.com/nedbat/coveragepy/issues/1351
5257
.. _pull 1354: https://github.com/nedbat/coveragepy/pull/1354
5358
.. _pull 1359: https://github.com/nedbat/coveragepy/pull/1359

coverage/sqldata.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,9 @@ def erase(self, parallel=False):
783783

784784
def read(self):
785785
"""Start using an existing data file."""
786-
with self._connect(): # TODO: doesn't look right
787-
self._have_used = True
786+
if os.path.exists(self._filename):
787+
with self._connect():
788+
self._have_used = True
788789

789790
def write(self):
790791
"""Ensure the data is written to the data file."""

tests/test_data.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ class CoverageDataTest(CoverageTest):
118118
def test_empty_data_is_false(self):
119119
covdata = DebugCoverageData()
120120
assert not covdata
121+
self.assert_doesnt_exist(".coverage")
122+
123+
def test_empty_data_is_false_when_read(self):
124+
covdata = DebugCoverageData()
125+
covdata.read()
126+
assert not covdata
127+
self.assert_doesnt_exist(".coverage")
121128

122129
def test_line_data_is_true(self):
123130
covdata = DebugCoverageData()

tests/test_summary.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ def test_report_skip_covered_no_data(self):
450450
cov.load()
451451
with pytest.raises(NoDataError, match="No data to report."):
452452
self.get_report(cov, skip_covered=True)
453+
self.assert_doesnt_exist(".coverage")
453454

454455
def test_report_skip_empty(self):
455456
self.make_file("main.py", """

tests/test_xml.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def test_no_data(self):
139139
with pytest.raises(NoDataError, match="No data to report."):
140140
self.run_xml_report()
141141
self.assert_doesnt_exist("coverage.xml")
142+
self.assert_doesnt_exist(".coverage")
142143

143144
def test_no_source(self):
144145
# Written while investigating a bug, might as well keep it.

0 commit comments

Comments
 (0)