|
3 | 3 | import pytest |
4 | 4 |
|
5 | 5 | import hyperbrowser.client.file_utils as file_utils |
6 | | -from hyperbrowser.client.file_utils import ensure_existing_file_path |
| 6 | +from hyperbrowser.client.file_utils import ensure_existing_file_path, open_binary_file |
7 | 7 | from hyperbrowser.exceptions import HyperbrowserError |
8 | 8 |
|
9 | 9 |
|
@@ -480,3 +480,62 @@ def __iter__(self): |
480 | 480 | ) |
481 | 481 |
|
482 | 482 | assert exc_info.value.original_error is None |
| 483 | + |
| 484 | + |
| 485 | +def test_open_binary_file_reads_content_and_closes(tmp_path: Path): |
| 486 | + file_path = tmp_path / "binary.bin" |
| 487 | + file_path.write_bytes(b"content") |
| 488 | + |
| 489 | + with open_binary_file( |
| 490 | + str(file_path), |
| 491 | + open_error_message="open failed", |
| 492 | + ) as file_obj: |
| 493 | + assert file_obj.read() == b"content" |
| 494 | + assert file_obj.closed is False |
| 495 | + |
| 496 | + assert file_obj.closed is True |
| 497 | + |
| 498 | + |
| 499 | +def test_open_binary_file_rejects_non_string_error_message(tmp_path: Path): |
| 500 | + file_path = tmp_path / "binary.bin" |
| 501 | + file_path.write_bytes(b"content") |
| 502 | + |
| 503 | + with pytest.raises(HyperbrowserError, match="open_error_message must be a string"): |
| 504 | + with open_binary_file( |
| 505 | + str(file_path), |
| 506 | + open_error_message=123, # type: ignore[arg-type] |
| 507 | + ): |
| 508 | + pass |
| 509 | + |
| 510 | + |
| 511 | +def test_open_binary_file_rejects_invalid_file_path_type(): |
| 512 | + with pytest.raises( |
| 513 | + HyperbrowserError, match="file_path must be a string or os.PathLike object" |
| 514 | + ): |
| 515 | + with open_binary_file( |
| 516 | + 123, # type: ignore[arg-type] |
| 517 | + open_error_message="open failed", |
| 518 | + ): |
| 519 | + pass |
| 520 | + |
| 521 | + |
| 522 | +def test_open_binary_file_rejects_non_string_fspath_results(): |
| 523 | + with pytest.raises(HyperbrowserError, match="file_path must resolve to a string"): |
| 524 | + with open_binary_file( |
| 525 | + b"/tmp/bytes-path", # type: ignore[arg-type] |
| 526 | + open_error_message="open failed", |
| 527 | + ): |
| 528 | + pass |
| 529 | + |
| 530 | + |
| 531 | +def test_open_binary_file_wraps_open_errors(tmp_path: Path): |
| 532 | + missing_path = tmp_path / "missing.bin" |
| 533 | + |
| 534 | + with pytest.raises(HyperbrowserError, match="open failed") as exc_info: |
| 535 | + with open_binary_file( |
| 536 | + str(missing_path), |
| 537 | + open_error_message="open failed", |
| 538 | + ): |
| 539 | + pass |
| 540 | + |
| 541 | + assert isinstance(exc_info.value.original_error, FileNotFoundError) |
0 commit comments