From 254fdec04114a087931d4d9d641235678e1ceade Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 31 Oct 2024 07:23:38 -0400 Subject: [PATCH] test: add a test for OSError while reading .py file --- tests/test_parser.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_parser.py b/tests/test_parser.py index d3d4e7570..1b956151f 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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 @@ -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")