@@ -336,3 +336,157 @@ def __fspath__(self) -> str:
336336 )
337337
338338 assert exc_info .value .original_error is None
339+
340+
341+ def test_ensure_existing_file_path_wraps_missing_message_strip_runtime_errors (
342+ tmp_path : Path ,
343+ ):
344+ class _BrokenMissingMessage (str ):
345+ def strip (self , chars = None ): # type: ignore[override]
346+ _ = chars
347+ raise RuntimeError ("missing message strip exploded" )
348+
349+ file_path = tmp_path / "file.txt"
350+ file_path .write_text ("content" )
351+
352+ with pytest .raises (
353+ HyperbrowserError , match = "Failed to normalize missing_file_message"
354+ ) as exc_info :
355+ ensure_existing_file_path (
356+ str (file_path ),
357+ missing_file_message = _BrokenMissingMessage ("missing" ),
358+ not_file_message = "not-file" ,
359+ )
360+
361+ assert isinstance (exc_info .value .original_error , RuntimeError )
362+
363+
364+ def test_ensure_existing_file_path_wraps_missing_message_character_validation_failures (
365+ tmp_path : Path ,
366+ ):
367+ class _BrokenMissingMessage (str ):
368+ def strip (self , chars = None ): # type: ignore[override]
369+ _ = chars
370+ return self
371+
372+ def __iter__ (self ):
373+ raise RuntimeError ("missing message iteration exploded" )
374+
375+ file_path = tmp_path / "file.txt"
376+ file_path .write_text ("content" )
377+
378+ with pytest .raises (
379+ HyperbrowserError , match = "Failed to validate missing_file_message characters"
380+ ) as exc_info :
381+ ensure_existing_file_path (
382+ str (file_path ),
383+ missing_file_message = _BrokenMissingMessage ("missing" ),
384+ not_file_message = "not-file" ,
385+ )
386+
387+ assert isinstance (exc_info .value .original_error , RuntimeError )
388+
389+
390+ def test_ensure_existing_file_path_preserves_hyperbrowser_missing_message_strip_errors (
391+ tmp_path : Path ,
392+ ):
393+ class _BrokenMissingMessage (str ):
394+ def strip (self , chars = None ): # type: ignore[override]
395+ _ = chars
396+ raise HyperbrowserError ("custom missing message strip failure" )
397+
398+ file_path = tmp_path / "file.txt"
399+ file_path .write_text ("content" )
400+
401+ with pytest .raises (
402+ HyperbrowserError , match = "custom missing message strip failure"
403+ ) as exc_info :
404+ ensure_existing_file_path (
405+ str (file_path ),
406+ missing_file_message = _BrokenMissingMessage ("missing" ),
407+ not_file_message = "not-file" ,
408+ )
409+
410+ assert exc_info .value .original_error is None
411+
412+
413+ def test_ensure_existing_file_path_wraps_not_file_message_strip_runtime_errors (
414+ tmp_path : Path ,
415+ ):
416+ class _BrokenNotFileMessage (str ):
417+ def strip (self , chars = None ): # type: ignore[override]
418+ _ = chars
419+ raise RuntimeError ("not-file message strip exploded" )
420+
421+ file_path = tmp_path / "file.txt"
422+ file_path .write_text ("content" )
423+
424+ with pytest .raises (
425+ HyperbrowserError , match = "Failed to normalize not_file_message"
426+ ) as exc_info :
427+ ensure_existing_file_path (
428+ str (file_path ),
429+ missing_file_message = "missing" ,
430+ not_file_message = _BrokenNotFileMessage ("not-file" ),
431+ )
432+
433+ assert isinstance (exc_info .value .original_error , RuntimeError )
434+
435+
436+ def test_ensure_existing_file_path_wraps_file_path_strip_runtime_errors ():
437+ class _BrokenPath (str ):
438+ def strip (self , chars = None ): # type: ignore[override]
439+ _ = chars
440+ raise RuntimeError ("path strip exploded" )
441+
442+ with pytest .raises (HyperbrowserError , match = "file_path is invalid" ) as exc_info :
443+ ensure_existing_file_path (
444+ _BrokenPath ("/tmp/path.txt" ),
445+ missing_file_message = "missing" ,
446+ not_file_message = "not-file" ,
447+ )
448+
449+ assert isinstance (exc_info .value .original_error , RuntimeError )
450+
451+
452+ def test_ensure_existing_file_path_wraps_file_path_contains_runtime_errors ():
453+ class _BrokenPath (str ):
454+ def strip (self , chars = None ): # type: ignore[override]
455+ _ = chars
456+ return self
457+
458+ def __contains__ (self , item ): # type: ignore[override]
459+ _ = item
460+ raise RuntimeError ("path contains exploded" )
461+
462+ with pytest .raises (HyperbrowserError , match = "file_path is invalid" ) as exc_info :
463+ ensure_existing_file_path (
464+ _BrokenPath ("/tmp/path.txt" ),
465+ missing_file_message = "missing" ,
466+ not_file_message = "not-file" ,
467+ )
468+
469+ assert isinstance (exc_info .value .original_error , RuntimeError )
470+
471+
472+ def test_ensure_existing_file_path_wraps_file_path_character_iteration_runtime_errors ():
473+ class _BrokenPath (str ):
474+ def strip (self , chars = None ): # type: ignore[override]
475+ _ = chars
476+ return self
477+
478+ def __contains__ (self , item ): # type: ignore[override]
479+ _ = item
480+ return False
481+
482+ def __iter__ (self ):
483+ raise RuntimeError ("path iteration exploded" )
484+
485+ with pytest .raises (HyperbrowserError , match = "file_path is invalid" ) as exc_info :
486+ ensure_existing_file_path (
487+ _BrokenPath ("/tmp/path.txt" ),
488+ missing_file_message = "missing" ,
489+ not_file_message = "not-file" ,
490+ )
491+
492+ assert isinstance (exc_info .value .original_error , RuntimeError )
0 commit comments