Skip to content

Commit 4543202

Browse files
Use fspath-derived paths in upload normalization errors
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 17301b4 commit 4543202

4 files changed

Lines changed: 25 additions & 50 deletions

File tree

hyperbrowser/client/managers/async_manager/session.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,7 @@ async def get_downloads_url(self, id: str) -> GetSessionDownloadsUrlResponse:
164164
async def upload_file(
165165
self, id: str, file_input: Union[str, PathLike[str], IO]
166166
) -> UploadFileResponse:
167-
file_path, file_obj = normalize_upload_file_input(
168-
file_input,
169-
missing_file_message=f"Upload file not found at path: {file_input}",
170-
not_file_message=f"Upload file path must point to a file: {file_input}",
171-
)
167+
file_path, file_obj = normalize_upload_file_input(file_input)
172168
if file_path is not None:
173169
try:
174170
with open(file_path, "rb") as file_obj:

hyperbrowser/client/managers/session_upload_utils.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
def normalize_upload_file_input(
1212
file_input: Union[str, PathLike[str], IO],
13-
*,
14-
missing_file_message: str,
15-
not_file_message: str,
1613
) -> Tuple[Optional[str], Optional[IO]]:
1714
if is_plain_string(file_input) or isinstance(file_input, PathLike):
1815
try:
@@ -26,8 +23,8 @@ def normalize_upload_file_input(
2623
) from exc
2724
file_path = ensure_existing_file_path(
2825
raw_file_path,
29-
missing_file_message=missing_file_message,
30-
not_file_message=not_file_message,
26+
missing_file_message=f"Upload file not found at path: {raw_file_path}",
27+
not_file_message=f"Upload file path must point to a file: {raw_file_path}",
3128
)
3229
return file_path, None
3330

hyperbrowser/client/managers/sync_manager/session.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,7 @@ def get_downloads_url(self, id: str) -> GetSessionDownloadsUrlResponse:
156156
def upload_file(
157157
self, id: str, file_input: Union[str, PathLike[str], IO]
158158
) -> UploadFileResponse:
159-
file_path, file_obj = normalize_upload_file_input(
160-
file_input,
161-
missing_file_message=f"Upload file not found at path: {file_input}",
162-
not_file_message=f"Upload file path must point to a file: {file_input}",
163-
)
159+
file_path, file_obj = normalize_upload_file_input(file_input)
164160
if file_path is not None:
165161
try:
166162
with open(file_path, "rb") as file_obj:

tests/test_session_upload_utils.py

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ def test_normalize_upload_file_input_returns_path_for_plain_string(tmp_path: Pat
1414
file_path = tmp_path / "file.txt"
1515
file_path.write_text("content")
1616

17-
normalized_path, file_obj = normalize_upload_file_input(
18-
str(file_path),
19-
missing_file_message=f"Upload file not found at path: {file_path}",
20-
not_file_message=f"Upload file path must point to a file: {file_path}",
21-
)
17+
normalized_path, file_obj = normalize_upload_file_input(str(file_path))
2218

2319
assert normalized_path == str(file_path)
2420
assert file_obj is None
@@ -28,11 +24,7 @@ def test_normalize_upload_file_input_returns_path_for_pathlike(tmp_path: Path):
2824
file_path = tmp_path / "file.txt"
2925
file_path.write_text("content")
3026

31-
normalized_path, file_obj = normalize_upload_file_input(
32-
file_path,
33-
missing_file_message=f"Upload file not found at path: {file_path}",
34-
not_file_message=f"Upload file path must point to a file: {file_path}",
35-
)
27+
normalized_path, file_obj = normalize_upload_file_input(file_path)
3628

3729
assert normalized_path == str(file_path)
3830
assert file_obj is None
@@ -45,11 +37,7 @@ class _PathString(str):
4537
with pytest.raises(
4638
HyperbrowserError, match="file_input path must be a plain string path"
4739
) as exc_info:
48-
normalize_upload_file_input(
49-
_PathString("/tmp/file.txt"), # type: ignore[arg-type]
50-
missing_file_message="Upload file not found at path: /tmp/file.txt",
51-
not_file_message="Upload file path must point to a file: /tmp/file.txt",
52-
)
40+
normalize_upload_file_input(_PathString("/tmp/file.txt")) # type: ignore[arg-type]
5341

5442
assert exc_info.value.original_error is None
5543

@@ -62,23 +50,29 @@ def __fspath__(self) -> str:
6250
with pytest.raises(
6351
HyperbrowserError, match="file_input path is invalid"
6452
) as exc_info:
65-
normalize_upload_file_input(
66-
_BrokenPathLike(),
67-
missing_file_message="Upload file not found",
68-
not_file_message="Upload file path must point to a file",
69-
)
53+
normalize_upload_file_input(_BrokenPathLike())
7054

7155
assert isinstance(exc_info.value.original_error, RuntimeError)
7256

7357

58+
def test_normalize_upload_file_input_uses_fspath_path_in_missing_file_errors():
59+
class _StringifyFailingPathLike(PathLike[str]):
60+
def __fspath__(self) -> str:
61+
return "/tmp/nonexistent-path-for-upload-utils-test"
62+
63+
def __str__(self) -> str:
64+
raise RuntimeError("broken stringify")
65+
66+
with pytest.raises(HyperbrowserError, match="Upload file not found at path: /tmp/nonexistent-path-for-upload-utils-test") as exc_info:
67+
normalize_upload_file_input(_StringifyFailingPathLike())
68+
69+
assert exc_info.value.original_error is None
70+
71+
7472
def test_normalize_upload_file_input_returns_open_file_like_object():
7573
file_obj = io.BytesIO(b"content")
7674

77-
normalized_path, normalized_file_obj = normalize_upload_file_input(
78-
file_obj,
79-
missing_file_message="Upload file not found",
80-
not_file_message="Upload file path must point to a file",
81-
)
75+
normalized_path, normalized_file_obj = normalize_upload_file_input(file_obj)
8276

8377
assert normalized_path is None
8478
assert normalized_file_obj is file_obj
@@ -89,19 +83,11 @@ def test_normalize_upload_file_input_rejects_closed_file_like_object():
8983
file_obj.close()
9084

9185
with pytest.raises(HyperbrowserError, match="file-like object must be open"):
92-
normalize_upload_file_input(
93-
file_obj,
94-
missing_file_message="Upload file not found",
95-
not_file_message="Upload file path must point to a file",
96-
)
86+
normalize_upload_file_input(file_obj)
9787

9888

9989
def test_normalize_upload_file_input_rejects_non_callable_read_attribute():
10090
fake_file = type("FakeFile", (), {"read": "not-callable"})()
10191

10292
with pytest.raises(HyperbrowserError, match="file_input must be a file path"):
103-
normalize_upload_file_input(
104-
fake_file, # type: ignore[arg-type]
105-
missing_file_message="Upload file not found",
106-
not_file_message="Upload file path must point to a file",
107-
)
93+
normalize_upload_file_input(fake_file) # type: ignore[arg-type]

0 commit comments

Comments
 (0)