Skip to main content

Throw an explicit IsADirectoryError if you write to a dir

ID
ca37b5e
date
2024-08-24 09:07:28+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
407aff3
message
Throw an explicit IsADirectoryError if you write to a dir
changed files
2 files, 4 additions, 1 deletion

Changed files

src/javascript/__init__.py (4632) → src/javascript/__init__.py (4687)

diff --git a/src/javascript/__init__.py b/src/javascript/__init__.py
index aef8e55..2e64194 100644
--- a/src/javascript/__init__.py
+++ b/src/javascript/__init__.py
@@ -51,6 +51,9 @@ def write_js(p: pathlib.Path | str, *, value: typing.Any, varname: str) -> None:
     """
     p = pathlib.Path(p)
 
+    if p.is_dir():
+        raise IsADirectoryError(p)
+
     json_string = json.dumps(value, indent=2)
     js_string = f"const {varname} = {json_string};\n"
 

tests/test_javascript.py (6520) → tests/test_javascript.py (6522)

diff --git a/tests/test_javascript.py b/tests/test_javascript.py
index 9a53181..482181f 100644
--- a/tests/test_javascript.py
+++ b/tests/test_javascript.py
@@ -62,7 +62,7 @@ class TestWriteJs:
     def test_fails_if_cannot_write_file(self) -> None:
         red_pentagon = {"sides": 5, "colour": "red"}
 
-        with pytest.raises(FileExistsError):
+        with pytest.raises(IsADirectoryError):
             write_js("/", value=red_pentagon, varname="redPentagon")
 
     def test_fails_if_target_is_folder(self, tmp_path: pathlib.Path) -> None: