Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/cablexsec/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import annotations

import errno
import hashlib
import os
import platform
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
Loading