From e6df5b39be55c44205ea67811bb812e085599d5e Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sun, 22 May 2022 18:53:51 -0400 Subject: [PATCH] fix: don't create a data file when just trying to read one. #1328 --- CHANGES.rst | 5 +++++ coverage/sqldata.py | 5 +++-- tests/test_data.py | 7 +++++++ tests/test_summary.py | 1 + tests/test_xml.py | 1 + 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 7a200ee1a..86d5b11eb 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -41,6 +41,10 @@ Version 6.4 — 2022-05-22 - A new debug option ``debug=sqldata`` adds more detail to ``debug=sql``, logging all the data being written to the database. +- Previously, running ``coverage report`` (or any of the reporting commands) in + an empty directory would create a .coverage data file. Now they do not, + fixing `issue 1328`_. + - On Python 3.11, the ``[toml]`` extra no longer installs tomli, instead using tomllib from the standard library. Thanks `Shantanu `_. @@ -48,6 +52,7 @@ Version 6.4 — 2022-05-22 .. _issue 1310: https://github.com/nedbat/coveragepy/issues/1310 .. _issue 1323: https://github.com/nedbat/coveragepy/issues/1323 +.. _issue 1328: https://github.com/nedbat/coveragepy/issues/1328 .. _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 diff --git a/coverage/sqldata.py b/coverage/sqldata.py index 1d6f626ea..c0fa6916a 100644 --- a/coverage/sqldata.py +++ b/coverage/sqldata.py @@ -783,8 +783,9 @@ def erase(self, parallel=False): def read(self): """Start using an existing data file.""" - with self._connect(): # TODO: doesn't look right - self._have_used = True + if os.path.exists(self._filename): + with self._connect(): + self._have_used = True def write(self): """Ensure the data is written to the data file.""" diff --git a/tests/test_data.py b/tests/test_data.py index 9f9a92bd5..3cb519ca4 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -118,6 +118,13 @@ class CoverageDataTest(CoverageTest): def test_empty_data_is_false(self): covdata = DebugCoverageData() assert not covdata + self.assert_doesnt_exist(".coverage") + + def test_empty_data_is_false_when_read(self): + covdata = DebugCoverageData() + covdata.read() + assert not covdata + self.assert_doesnt_exist(".coverage") def test_line_data_is_true(self): covdata = DebugCoverageData() diff --git a/tests/test_summary.py b/tests/test_summary.py index 4bce80f6d..d603062be 100644 --- a/tests/test_summary.py +++ b/tests/test_summary.py @@ -450,6 +450,7 @@ def test_report_skip_covered_no_data(self): cov.load() with pytest.raises(NoDataError, match="No data to report."): self.get_report(cov, skip_covered=True) + self.assert_doesnt_exist(".coverage") def test_report_skip_empty(self): self.make_file("main.py", """ diff --git a/tests/test_xml.py b/tests/test_xml.py index 1e94dfa2e..fda11087b 100644 --- a/tests/test_xml.py +++ b/tests/test_xml.py @@ -139,6 +139,7 @@ def test_no_data(self): with pytest.raises(NoDataError, match="No data to report."): self.run_xml_report() self.assert_doesnt_exist("coverage.xml") + self.assert_doesnt_exist(".coverage") def test_no_source(self): # Written while investigating a bug, might as well keep it.