Skip to content

Commit 0d9c3e8

Browse files
Validate file utility error message arguments
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 2c2ad99 commit 0d9c3e8

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

hyperbrowser/client/file_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ def ensure_existing_file_path(
1111
missing_file_message: str,
1212
not_file_message: str,
1313
) -> str:
14+
if not isinstance(missing_file_message, str):
15+
raise HyperbrowserError("missing_file_message must be a string")
16+
if not missing_file_message.strip():
17+
raise HyperbrowserError("missing_file_message must not be empty")
18+
if not isinstance(not_file_message, str):
19+
raise HyperbrowserError("not_file_message must be a string")
20+
if not not_file_message.strip():
21+
raise HyperbrowserError("not_file_message must not be empty")
1422
try:
1523
normalized_path = os.fspath(file_path)
1624
except HyperbrowserError:

tests/test_file_utils.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,56 @@ def test_ensure_existing_file_path_accepts_existing_file(tmp_path: Path):
2020
assert normalized_path == str(file_path)
2121

2222

23+
def test_ensure_existing_file_path_rejects_non_string_missing_message(tmp_path: Path):
24+
file_path = tmp_path / "file.txt"
25+
file_path.write_text("content")
26+
27+
with pytest.raises(HyperbrowserError, match="missing_file_message must be a string"):
28+
ensure_existing_file_path(
29+
str(file_path),
30+
missing_file_message=123, # type: ignore[arg-type]
31+
not_file_message="not-file",
32+
)
33+
34+
35+
def test_ensure_existing_file_path_rejects_blank_missing_message(tmp_path: Path):
36+
file_path = tmp_path / "file.txt"
37+
file_path.write_text("content")
38+
39+
with pytest.raises(
40+
HyperbrowserError, match="missing_file_message must not be empty"
41+
):
42+
ensure_existing_file_path(
43+
str(file_path),
44+
missing_file_message=" ",
45+
not_file_message="not-file",
46+
)
47+
48+
49+
def test_ensure_existing_file_path_rejects_non_string_not_file_message(tmp_path: Path):
50+
file_path = tmp_path / "file.txt"
51+
file_path.write_text("content")
52+
53+
with pytest.raises(HyperbrowserError, match="not_file_message must be a string"):
54+
ensure_existing_file_path(
55+
str(file_path),
56+
missing_file_message="missing",
57+
not_file_message=123, # type: ignore[arg-type]
58+
)
59+
60+
61+
def test_ensure_existing_file_path_rejects_blank_not_file_message(tmp_path: Path):
62+
file_path = tmp_path / "file.txt"
63+
file_path.write_text("content")
64+
65+
with pytest.raises(HyperbrowserError, match="not_file_message must not be empty"):
66+
ensure_existing_file_path(
67+
str(file_path),
68+
missing_file_message="missing",
69+
not_file_message=" ",
70+
)
71+
72+
2373
def test_ensure_existing_file_path_accepts_pathlike_inputs(tmp_path: Path):
2474
file_path = tmp_path / "pathlike-file.txt"
2575
file_path.write_text("content")

0 commit comments

Comments
 (0)