|
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, open_binary_file |
| 6 | +from hyperbrowser.client.file_utils import ( |
| 7 | + ensure_existing_file_path, |
| 8 | + format_file_path_for_error, |
| 9 | + open_binary_file, |
| 10 | +) |
7 | 11 | from hyperbrowser.exceptions import HyperbrowserError |
8 | 12 |
|
9 | 13 |
|
@@ -266,6 +270,38 @@ def test_ensure_existing_file_path_rejects_control_character_paths(): |
266 | 270 | ) |
267 | 271 |
|
268 | 272 |
|
| 273 | +def test_format_file_path_for_error_sanitizes_control_characters(): |
| 274 | + display_path = format_file_path_for_error("bad\tpath\nvalue") |
| 275 | + |
| 276 | + assert display_path == "bad?path?value" |
| 277 | + |
| 278 | + |
| 279 | +def test_format_file_path_for_error_truncates_long_paths(): |
| 280 | + display_path = format_file_path_for_error("abcdef", max_length=5) |
| 281 | + |
| 282 | + assert display_path == "ab..." |
| 283 | + |
| 284 | + |
| 285 | +def test_format_file_path_for_error_falls_back_for_non_string_values(): |
| 286 | + assert format_file_path_for_error(object()) == "<provided path>" |
| 287 | + |
| 288 | + |
| 289 | +def test_format_file_path_for_error_falls_back_for_fspath_failures(): |
| 290 | + class _BrokenPathLike: |
| 291 | + def __fspath__(self) -> str: |
| 292 | + raise RuntimeError("bad fspath") |
| 293 | + |
| 294 | + assert format_file_path_for_error(_BrokenPathLike()) == "<provided path>" |
| 295 | + |
| 296 | + |
| 297 | +def test_format_file_path_for_error_uses_pathlike_string_values(): |
| 298 | + class _PathLike: |
| 299 | + def __fspath__(self) -> str: |
| 300 | + return "/tmp/path-value" |
| 301 | + |
| 302 | + assert format_file_path_for_error(_PathLike()) == "/tmp/path-value" |
| 303 | + |
| 304 | + |
269 | 305 | def test_ensure_existing_file_path_wraps_invalid_path_os_errors(monkeypatch): |
270 | 306 | def raising_exists(path: str) -> bool: |
271 | 307 | raise OSError("invalid path") |
|
0 commit comments