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 @@ -6,6 +6,12 @@ All notable changes to this project are documented here, following

## [Unreleased]

### Fixed

- `sdlc-validate` now says "cannot read the constitution checkout" when the
checkout directory cannot be listed (e.g. an SELinux label), instead of
reporting `validate-constitution.py` as missing.

## [0.6.3] - 2026-11-11

### Changed
Expand Down
91 changes: 91 additions & 0 deletions tests/test_validate_checkout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python3
"""`validate` must tell an unreadable checkout from one missing a file.

An SELinux label or mode that blocks listing the cached constitution checkout
used to surface as "missing validate-constitution.py", sending the reader to
the constitution tag instead of the label. This makes `os.listdir` raise and
asserts the diagnostic names the checkout and the cause, and a plain missing
file still gets the missing-file message.

Run: tests/test_validate_checkout.py (needs nothing but python3)
"""

from __future__ import annotations

import contextlib
import importlib.machinery
import importlib.util
import io
import sys
import tempfile
from pathlib import Path
from unittest import mock

VALIDATE = Path(__file__).resolve().parent.parent / "validate"


def load():
loader = importlib.machinery.SourceFileLoader("sdlc_validate", str(VALIDATE))
spec = importlib.util.spec_from_loader("sdlc_validate", loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
return module


def run(module, cache: Path, manifest: Path, listdir=None) -> tuple[int, str]:
err = io.StringIO()
patches = [
mock.patch.object(module, "CACHE", cache),
mock.patch.object(sys, "argv", ["validate", str(manifest)]),
]
if listdir is not None:
patches.append(mock.patch.object(module.os, "listdir", listdir))
with contextlib.ExitStack() as stack:
for p in patches:
stack.enter_context(p)
stack.enter_context(contextlib.redirect_stderr(err))
try:
module.main()
code = 0
except SystemExit as exc:
code = exc.code
return code, err.getvalue()


def main() -> int:
module = load()
failed = 0
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
manifest = root / "constitution.md"
manifest.write_text("> **Inherits:** constitution v1.2.3\n", encoding="utf-8")
cache = root / "cache"
checkout = cache / "constitution" / "v1.2.3"
(checkout / ".git").mkdir(parents=True)

def denied(_path):
raise PermissionError(13, "Permission denied")

code, err = run(module, cache, manifest, listdir=denied)
checks = [
("unreadable: exits CANNOT_RUN", code == module.CANNOT_RUN),
("unreadable: says cannot read", "cannot read the constitution checkout" in err),
("unreadable: names the path", str(checkout) in err),
("unreadable: carries the error", "Permission denied" in err),
("unreadable: not the missing-file message", "is missing" not in err),
]
code, err = run(module, cache, manifest)
checks += [
("missing file: exits CANNOT_RUN", code == module.CANNOT_RUN),
("missing file: says is missing", "is missing validate-constitution.py" in err),
]
for name, ok in checks:
print(f"{'ok ' if ok else 'FAIL'} {name}")
failed += not ok
print()
print("validate checkout diagnostics " + ("FAILED" if failed else "PASSED"))
return 1 if failed else 0


if __name__ == "__main__":
sys.exit(main())
11 changes: 11 additions & 0 deletions validate
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The clone is cached per tag in the cache volume, never in the worktree.

from __future__ import annotations

import os
import re
import subprocess
import sys
Expand Down Expand Up @@ -80,6 +81,16 @@ def main() -> int:
die(f"cannot clone the constitution at {tag}: {proc.stderr.strip()}")

validator = checkout / "validate-constitution.py"
# A directory that cannot be listed (an SELinux label, a mode) is not a
# directory with a file missing; `exists()` cannot tell them apart.
try:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MEDIUM (Test Coverage): The new unreadable-checkout error path has no corresponding test in the changes shown. This behavior distinguishes a directory that cannot be listed from one where the validator file is missing, so an untested regression could restore the misleading missing-file diagnostic.

Suggestion: Add a test that makes os.listdir(checkout) raise OSError and verifies validation reports that it cannot read the constitution checkout, including the relevant path or error context.

Evidence:

try:
    os.listdir(checkout)
except OSError as exc:
    die(
        f"cannot read the constitution checkout at {checkout} ({tag}): {exc}; "
        f"the checkout is there but not readable -- check its SELinux label "
        f"and mode, not the constitution tag"
    )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in a01fcf0

os.listdir(checkout)
except OSError as exc:
die(
f"cannot read the constitution checkout at {checkout} ({tag}): {exc}; "
f"the checkout is there but not readable -- check its SELinux label "
f"and mode, not the constitution tag"
)
# The two files whose absence makes the validator pass without looking.
for required in (
validator,
Expand Down
Loading