@@ -88,28 +88,23 @@ def test_readfromjson_file_not_found(self) -> None:
8888 with pytest .raises (JSONReadError , match = "Invalid JSON File" ):
8989 readfromjson ("non_existent_file.json" )
9090
91- def test_readfromjson_permission_error (self ) -> None :
91+ @patch ('builtins.open' )
92+ def test_readfromjson_permission_error (self , mock_open : Mock ) -> None :
9293 """Test reading a file with permission issues."""
93- # Create a temporary file and then make it unreadable
94- with tempfile .NamedTemporaryFile (mode = 'w' , suffix = '.json' , delete = False ) as f :
95- json .dump ({"test" : "data" }, f )
96- temp_filename = f .name
94+ # Mock open to raise PermissionError
95+ mock_open .side_effect = PermissionError ("Permission denied" )
9796
98- try :
99- import os
100- # Make file unreadable (this might not work on all systems)
101- os .chmod (temp_filename , 0o000 )
97+ with pytest .raises (JSONReadError , match = "Invalid JSON File" ):
98+ readfromjson ("some_file.json" )
10299
103- with pytest .raises (JSONReadError , match = "Invalid JSON File" ):
104- readfromjson (temp_filename )
105- finally :
106- # Restore permissions and cleanup
107- import os
108- try :
109- os .chmod (temp_filename , 0o644 )
110- os .unlink (temp_filename )
111- except (OSError , PermissionError ):
112- pass # Best effort cleanup
100+ @patch ('builtins.open' )
101+ def test_readfromjson_os_error (self , mock_open : Mock ) -> None :
102+ """Test reading a file with OS error."""
103+ # Mock open to raise OSError (covers line 34-35 in utils.py)
104+ mock_open .side_effect = OSError ("Device not ready" )
105+
106+ with pytest .raises (JSONReadError , match = "Invalid JSON File" ):
107+ readfromjson ("some_file.json" )
113108
114109
115110class TestReadFromUrl :
0 commit comments