Skip to content

Commit 12c9ff8

Browse files
Require concrete fspath string outputs in file utils
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 6253181 commit 12c9ff8

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

hyperbrowser/client/file_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def ensure_existing_file_path(
6363
) from exc
6464
except Exception as exc:
6565
raise HyperbrowserError("file_path is invalid", original_error=exc) from exc
66-
if not isinstance(normalized_path, str):
66+
if type(normalized_path) is not str:
6767
raise HyperbrowserError("file_path must resolve to a string path")
6868
try:
6969
stripped_normalized_path = normalized_path.strip()

tests/test_file_utils.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,22 @@ def test_ensure_existing_file_path_rejects_non_string_fspath_results():
159159
)
160160

161161

162+
def test_ensure_existing_file_path_rejects_string_subclass_fspath_results():
163+
class _PathLike:
164+
class _PathString(str):
165+
pass
166+
167+
def __fspath__(self):
168+
return self._PathString("/tmp/subclass-path")
169+
170+
with pytest.raises(HyperbrowserError, match="file_path must resolve to a string"):
171+
ensure_existing_file_path(
172+
_PathLike(), # type: ignore[arg-type]
173+
missing_file_message="missing",
174+
not_file_message="not-file",
175+
)
176+
177+
162178
def test_ensure_existing_file_path_rejects_empty_string_paths():
163179
with pytest.raises(HyperbrowserError, match="file_path must not be empty"):
164180
ensure_existing_file_path(
@@ -485,23 +501,25 @@ def strip(self, chars=None): # type: ignore[override]
485501
assert isinstance(exc_info.value.original_error, TypeError)
486502

487503

488-
def test_ensure_existing_file_path_wraps_file_path_strip_runtime_errors():
504+
def test_ensure_existing_file_path_rejects_string_subclass_path_inputs_before_strip():
489505
class _BrokenPath(str):
490506
def strip(self, chars=None): # type: ignore[override]
491507
_ = chars
492508
raise RuntimeError("path strip exploded")
493509

494-
with pytest.raises(HyperbrowserError, match="file_path is invalid") as exc_info:
510+
with pytest.raises(
511+
HyperbrowserError, match="file_path must resolve to a string path"
512+
) as exc_info:
495513
ensure_existing_file_path(
496514
_BrokenPath("/tmp/path.txt"),
497515
missing_file_message="missing",
498516
not_file_message="not-file",
499517
)
500518

501-
assert isinstance(exc_info.value.original_error, RuntimeError)
519+
assert exc_info.value.original_error is None
502520

503521

504-
def test_ensure_existing_file_path_wraps_file_path_string_subclass_strip_results():
522+
def test_ensure_existing_file_path_rejects_string_subclass_path_strip_result_inputs():
505523
class _BrokenPath(str):
506524
class _NormalizedPath(str):
507525
pass
@@ -510,17 +528,19 @@ def strip(self, chars=None): # type: ignore[override]
510528
_ = chars
511529
return self._NormalizedPath("/tmp/path.txt")
512530

513-
with pytest.raises(HyperbrowserError, match="file_path is invalid") as exc_info:
531+
with pytest.raises(
532+
HyperbrowserError, match="file_path must resolve to a string path"
533+
) as exc_info:
514534
ensure_existing_file_path(
515535
_BrokenPath("/tmp/path.txt"),
516536
missing_file_message="missing",
517537
not_file_message="not-file",
518538
)
519539

520-
assert isinstance(exc_info.value.original_error, TypeError)
540+
assert exc_info.value.original_error is None
521541

522542

523-
def test_ensure_existing_file_path_wraps_file_path_contains_runtime_errors():
543+
def test_ensure_existing_file_path_rejects_string_subclass_path_inputs_before_contains():
524544
class _BrokenPath(str):
525545
def strip(self, chars=None): # type: ignore[override]
526546
_ = chars
@@ -530,17 +550,19 @@ def __contains__(self, item): # type: ignore[override]
530550
_ = item
531551
raise RuntimeError("path contains exploded")
532552

533-
with pytest.raises(HyperbrowserError, match="file_path is invalid") as exc_info:
553+
with pytest.raises(
554+
HyperbrowserError, match="file_path must resolve to a string path"
555+
) as exc_info:
534556
ensure_existing_file_path(
535557
_BrokenPath("/tmp/path.txt"),
536558
missing_file_message="missing",
537559
not_file_message="not-file",
538560
)
539561

540-
assert isinstance(exc_info.value.original_error, TypeError)
562+
assert exc_info.value.original_error is None
541563

542564

543-
def test_ensure_existing_file_path_wraps_file_path_character_iteration_runtime_errors():
565+
def test_ensure_existing_file_path_rejects_string_subclass_path_inputs_before_character_iteration():
544566
class _BrokenPath(str):
545567
def strip(self, chars=None): # type: ignore[override]
546568
_ = chars
@@ -553,11 +575,13 @@ def __contains__(self, item): # type: ignore[override]
553575
def __iter__(self):
554576
raise RuntimeError("path iteration exploded")
555577

556-
with pytest.raises(HyperbrowserError, match="file_path is invalid") as exc_info:
578+
with pytest.raises(
579+
HyperbrowserError, match="file_path must resolve to a string path"
580+
) as exc_info:
557581
ensure_existing_file_path(
558582
_BrokenPath("/tmp/path.txt"),
559583
missing_file_message="missing",
560584
not_file_message="not-file",
561585
)
562586

563-
assert isinstance(exc_info.value.original_error, TypeError)
587+
assert exc_info.value.original_error is None

0 commit comments

Comments
 (0)