Skip to content

Commit 6cc672f

Browse files
committed
test: cover the last edge cases in sqldata.py
1 parent 982eca8 commit 6cc672f

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

coverage/sqldata.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ def __exit__(self, exc_type, exc_value, traceback):
10681068
except Exception as exc:
10691069
if self.debug:
10701070
self.debug.write(f"EXCEPTION from __exit__: {exc}")
1071-
raise
1071+
raise CoverageException(f"Couldn't end data file {self.filename!r}: {exc}") from exc
10721072

10731073
def execute(self, sql, parameters=()):
10741074
"""Same as :meth:`python:sqlite3.Connection.execute`."""
@@ -1095,7 +1095,7 @@ def execute(self, sql, parameters=()):
10951095
"Looks like a coverage 4.x data file. " +
10961096
"Are you mixing versions of coverage?"
10971097
)
1098-
except Exception:
1098+
except Exception: # pragma: cant happen
10991099
pass
11001100
if self.debug:
11011101
self.debug.write(f"EXCEPTION from execute: {msg}")
@@ -1116,7 +1116,7 @@ def execute_one(self, sql, parameters=()):
11161116
elif len(rows) == 1:
11171117
return rows[0]
11181118
else:
1119-
raise CoverageException(f"Sql {sql!r} shouldn't return {len(rows)} rows")
1119+
raise AssertionError(f"SQL {sql!r} shouldn't return {len(rows)} rows")
11201120

11211121
def executemany(self, sql, data):
11221122
"""Same as :meth:`python:sqlite3.Connection.executemany`."""
@@ -1125,7 +1125,7 @@ def executemany(self, sql, data):
11251125
self.debug.write(f"Executing many {sql!r} with {len(data)} rows")
11261126
try:
11271127
return self.con.executemany(sql, data)
1128-
except Exception:
1128+
except Exception: # pragma: cant happen
11291129
# In some cases, an error might happen that isn't really an
11301130
# error. Try again immediately.
11311131
# https://github.com/nedbat/coveragepy/issues/1010

tests/test_data.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,21 @@ def test_read_errors(self):
601601
def test_hard_read_error(self):
602602
self.make_file("noperms.dat", "go away")
603603
os.chmod("noperms.dat", 0)
604-
msg = r"Couldn't .* '.*[/\\]{}': \S+"
605-
with pytest.raises(CoverageException, match=msg.format("noperms.dat")):
604+
with pytest.raises(CoverageException, match=r"Couldn't .* '.*[/\\]noperms.dat': "):
606605
covdata = DebugCoverageData("noperms.dat")
607606
covdata.read()
608607

608+
@pytest.mark.parametrize("klass", [CoverageData, DebugCoverageData])
609+
def test_error_when_closing(self, klass):
610+
msg = r"Couldn't .* '.*[/\\]flaked.dat': \S+"
611+
with pytest.raises(CoverageException, match=msg):
612+
covdata = klass("flaked.dat")
613+
covdata.add_lines(LINES_1)
614+
# I don't know how to make a real error, so let's fake one.
615+
sqldb = list(covdata._dbs.values())[0]
616+
sqldb.close = lambda: 1/0
617+
covdata.add_lines(LINES_1)
618+
609619
def test_read_sql_errors(self):
610620
with sqlite3.connect("wrong_schema.db") as con:
611621
con.execute("create table coverage_schema (version integer)")

0 commit comments

Comments
 (0)