44gracefully, especially for cloud-synced filesystems (iCloud, Dropbox).
55"""
66
7- import signal
87from pathlib import Path
98from unittest .mock import patch
109
@@ -58,8 +57,6 @@ def test_timeout_on_read(self, tmp_path: Path) -> None:
5857 test_file .write_text ("content" , encoding = "utf-8" )
5958
6059 def mock_read_text (* args , ** kwargs ):
61- # Simulate a file that hangs by raising SIGALRM
62- signal .alarm (0 ) # Cancel any existing alarm
6360 raise FileReadTimeoutError ("Simulated timeout" )
6461
6562 with (
@@ -220,18 +217,17 @@ def test_index_file_timeout_on_stat(self, clean_db, tmp_path: Path) -> None:
220217 test_file = tmp_path / "slow.md"
221218 test_file .write_text ("content" , encoding = "utf-8" )
222219
223- # Mock os.stat since it's what pathlib.Path.stat() uses internally
224- import os
220+ # Patch Path.stat directly with new= to work across Python versions.
221+ # Using new= preserves self-binding (functions are descriptors).
222+ original_stat = Path .stat
225223
226- original_os_stat = os .stat
227-
228- def os_stat_side_effect (path , * args , ** kwargs ):
229- if str (path ) == str (test_file ):
224+ def patched_stat (self , * args , ** kwargs ):
225+ if str (self ) == str (test_file ):
230226 raise TimeoutError ("stat timeout" )
231- return original_os_stat ( path , * args , ** kwargs )
227+ return original_stat ( self , * args , ** kwargs )
232228
233229 with (
234- patch ( "os. stat" , side_effect = os_stat_side_effect ),
230+ patch . object ( Path , " stat" , new = patched_stat ),
235231 pytest .raises (FileReadTimeoutError ),
236232 ):
237233 service .index_file (test_file )
@@ -248,17 +244,15 @@ def test_index_file_stat_os_error(self, clean_db, tmp_path: Path) -> None:
248244 test_file = tmp_path / "broken.md"
249245 test_file .write_text ("content" , encoding = "utf-8" )
250246
251- import os
252-
253- original_os_stat = os .stat
247+ original_stat = Path .stat
254248
255- def os_stat_side_effect ( path , * args , ** kwargs ):
256- if str (path ) == str (test_file ):
249+ def patched_stat ( self , * args , ** kwargs ):
250+ if str (self ) == str (test_file ):
257251 raise OSError ("Disk error" )
258- return original_os_stat ( path , * args , ** kwargs )
252+ return original_stat ( self , * args , ** kwargs )
259253
260254 with (
261- patch ( "os. stat" , side_effect = os_stat_side_effect ),
255+ patch . object ( Path , " stat" , new = patched_stat ),
262256 pytest .raises (FileReadError ),
263257 ):
264258 service .index_file (test_file )
0 commit comments