Skip to content

Commit 254fdec

Browse files
committed
test: add a test for OSError while reading .py file
1 parent 989b385 commit 254fdec

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

tests/test_parser.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
from __future__ import annotations
77

8+
import re
89
import textwrap
10+
from unittest import mock
911

1012
import pytest
1113

1214
from coverage import env
13-
from coverage.exceptions import NotPython
15+
from coverage.exceptions import NoSource, NotPython
1416
from coverage.parser import PythonParser
1517

1618
from tests.coveragetest import CoverageTest
@@ -1147,3 +1149,10 @@ def test_missing_line_ending(self) -> None:
11471149

11481150
parser = self.parse_file("abrupt.py")
11491151
assert parser.statements == {1}
1152+
1153+
def test_os_error(self) -> None:
1154+
self.make_file("cant-read.py", "BOOM!")
1155+
msg = "No source for code: 'cant-read.py': Fake!"
1156+
with pytest.raises(NoSource, match=re.escape(msg)):
1157+
with mock.patch("coverage.python.read_python_source", side_effect=OSError("Fake!")):
1158+
PythonParser(filename="cant-read.py")

0 commit comments

Comments
 (0)