Skip to content

Commit

Permalink
test: add a test for OSError while reading .py file
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Oct 31, 2024
1 parent 989b385 commit 254fdec
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

from __future__ import annotations

import re
import textwrap
from unittest import mock

import pytest

from coverage import env
from coverage.exceptions import NotPython
from coverage.exceptions import NoSource, NotPython
from coverage.parser import PythonParser

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

parser = self.parse_file("abrupt.py")
assert parser.statements == {1}

def test_os_error(self) -> None:
self.make_file("cant-read.py", "BOOM!")
msg = "No source for code: 'cant-read.py': Fake!"
with pytest.raises(NoSource, match=re.escape(msg)):
with mock.patch("coverage.python.read_python_source", side_effect=OSError("Fake!")):
PythonParser(filename="cant-read.py")

0 comments on commit 254fdec

Please sign in to comment.