diff --git a/CHANGELOG.md b/CHANGELOG.md index c20582a..9701f7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ All notable changes to CableXSec are recorded in this file. The format follows ## [Unreleased] +### Fixed + +- `Project.save` and `Project.load` given a directory raise `IsADirectoryError` naming the path + on every platform; on Windows `save` raised `PermissionError` (WinError 5) from the final + replacement and `load` raised `PermissionError`. + ## [0.2.3] - 2026-12-03 The Python interfaces are versioned but may change before `1.0.0`. The evidence for diff --git a/src/cablexsec/project.py b/src/cablexsec/project.py index b311bcb..791ee8d 100644 --- a/src/cablexsec/project.py +++ b/src/cablexsec/project.py @@ -12,6 +12,7 @@ from __future__ import annotations +import errno import hashlib import os import platform @@ -260,6 +261,17 @@ def _name(value: str) -> None: ) +def _not_a_directory(path: Path, action: str) -> None: + # a directory gives PermissionError on Windows and IsADirectoryError elsewhere; + # name the problem the same way on every platform + if path.is_dir(): + raise IsADirectoryError( + errno.EISDIR, + f"{action} needs a project file path, not a directory", + str(path), + ) + + def _digest(value: str) -> str: return hashlib.sha256(value.encode("utf-8")).hexdigest() @@ -716,6 +728,7 @@ def save(self, path: str | Path) -> Path: } ) target = Path(path) + _not_a_directory(target, "Project.save") temporary: Path | None = None try: with tempfile.NamedTemporaryFile( @@ -739,6 +752,7 @@ def save(self, path: str | Path) -> Path: @classmethod def load(cls, path: str | Path) -> Project: """Load versions 1 and 2; reject unknown versions and inconsistent contents.""" + _not_a_directory(Path(path), "Project.load") try: text = Path(path).read_bytes().decode("utf-8") except UnicodeDecodeError as error: diff --git a/tests/test_project.py b/tests/test_project.py index f0646ec..7755001 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -342,6 +342,21 @@ def test_load_rejects_corruption_and_native_missing_file(tmp_path): cx.Project.load(tmp_path / "missing") +def test_save_and_load_name_a_directory_on_every_platform(tmp_path): + folder = tmp_path / "results" + folder.mkdir() + with pytest.raises( + IsADirectoryError, match="Project.save needs a project file path" + ): + project().save(folder) + with pytest.raises( + IsADirectoryError, match="Project.load needs a project file path" + ): + cx.Project.load(folder) + assert list(folder.iterdir()) == [] + assert not list(tmp_path.glob(".cablexsec-*.tmp")) + + @pytest.mark.parametrize( "mutation", [