|
| 1 | +import io |
| 2 | +from os import PathLike |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from hyperbrowser.client.managers.session_upload_utils import ( |
| 8 | + normalize_upload_file_input, |
| 9 | +) |
| 10 | +from hyperbrowser.exceptions import HyperbrowserError |
| 11 | + |
| 12 | + |
| 13 | +def test_normalize_upload_file_input_returns_path_for_plain_string(tmp_path: Path): |
| 14 | + file_path = tmp_path / "file.txt" |
| 15 | + file_path.write_text("content") |
| 16 | + |
| 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 | + ) |
| 22 | + |
| 23 | + assert normalized_path == str(file_path) |
| 24 | + assert file_obj is None |
| 25 | + |
| 26 | + |
| 27 | +def test_normalize_upload_file_input_returns_path_for_pathlike(tmp_path: Path): |
| 28 | + file_path = tmp_path / "file.txt" |
| 29 | + file_path.write_text("content") |
| 30 | + |
| 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 | + ) |
| 36 | + |
| 37 | + assert normalized_path == str(file_path) |
| 38 | + assert file_obj is None |
| 39 | + |
| 40 | + |
| 41 | +def test_normalize_upload_file_input_rejects_string_subclass(): |
| 42 | + class _PathString(str): |
| 43 | + pass |
| 44 | + |
| 45 | + with pytest.raises( |
| 46 | + HyperbrowserError, match="file_input path must be a plain string path" |
| 47 | + ) 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 | + ) |
| 53 | + |
| 54 | + assert exc_info.value.original_error is None |
| 55 | + |
| 56 | + |
| 57 | +def test_normalize_upload_file_input_wraps_invalid_pathlike_state_errors(): |
| 58 | + class _BrokenPathLike(PathLike[str]): |
| 59 | + def __fspath__(self) -> str: |
| 60 | + raise RuntimeError("broken fspath") |
| 61 | + |
| 62 | + with pytest.raises( |
| 63 | + HyperbrowserError, match="file_input path is invalid" |
| 64 | + ) 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 | + ) |
| 70 | + |
| 71 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 72 | + |
| 73 | + |
| 74 | +def test_normalize_upload_file_input_returns_open_file_like_object(): |
| 75 | + file_obj = io.BytesIO(b"content") |
| 76 | + |
| 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 | + ) |
| 82 | + |
| 83 | + assert normalized_path is None |
| 84 | + assert normalized_file_obj is file_obj |
| 85 | + |
| 86 | + |
| 87 | +def test_normalize_upload_file_input_rejects_closed_file_like_object(): |
| 88 | + file_obj = io.BytesIO(b"content") |
| 89 | + file_obj.close() |
| 90 | + |
| 91 | + 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 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def test_normalize_upload_file_input_rejects_non_callable_read_attribute(): |
| 100 | + fake_file = type("FakeFile", (), {"read": "not-callable"})() |
| 101 | + |
| 102 | + 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 | + ) |
0 commit comments