Files
ss-tools/backend/tests/scripts/test_check_specs_file_policy.py

158 lines
7.1 KiB
Python

# #region Test.Tooling.SpecsFilePolicy [C:3] [TYPE Module] [SEMANTICS specs,policy,git,negative]
# @TEST_INVARIANT Exact committed changes, including copied/renamed destinations, cannot introduce raw spec evidence.
import importlib.util
import json
from pathlib import Path
import subprocess
import pytest
ROOT = Path(__file__).resolve().parents[3]
spec = importlib.util.spec_from_file_location("specs_file_policy", ROOT / "scripts/check_specs_file_policy.py")
policy = importlib.util.module_from_spec(spec)
spec.loader.exec_module(policy)
SCHEMA = "specs/037-superset-baseline-engine/contracts/baseline-pin.schema.json"
DOCUMENT = {"$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "urn:synthetic:policy", "properties": {}}
# #region Test.Tooling.SpecsFilePolicy.Git [C:1] [TYPE Function]
def git(repo, *args):
return subprocess.check_output(["git", "-C", str(repo), *args], stderr=subprocess.PIPE, text=True).strip()
# #endregion Test.Tooling.SpecsFilePolicy.Git
# #region Test.Tooling.SpecsFilePolicy.Commit [C:1] [TYPE Function]
def commit(repo):
git(repo, "add", "-A")
git(repo, "commit", "-qm", "synthetic policy fixture")
return git(repo, "rev-parse", "HEAD")
# #endregion Test.Tooling.SpecsFilePolicy.Commit
# #region Test.Tooling.SpecsFilePolicy.Repository [C:2] [TYPE Function]
@pytest.fixture
def repository(tmp_path, monkeypatch):
git(tmp_path, "init", "-q")
git(tmp_path, "config", "user.name", "Synthetic policy test")
git(tmp_path, "config", "user.email", "synthetic@example.invalid")
for name, content in (("specs/prototype/old.py", "# historical\n"), (SCHEMA, json.dumps(DOCUMENT))):
path = tmp_path / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)
base = commit(tmp_path)
monkeypatch.setattr(policy, "POLICY_BASE", base)
return tmp_path, base
# #endregion Test.Tooling.SpecsFilePolicy.Repository
# #region Test.Tooling.SpecsFilePolicy.Forbidden [C:2] [TYPE Function]
@pytest.mark.parametrize("suffix", [".png", ".log", ".json", ".txt", ".py", ".PNG", ".JsOn"])
def test_added_raw_spec_path_refuses(repository, suffix):
repo, base = repository
(repo / f"specs/new{suffix}").write_text("synthetic")
candidate = commit(repo)
with pytest.raises(policy.PolicyRefusal) as refusal:
policy.check(repo, base, candidate)
assert refusal.value.code == "SPEC_POLICY_PATH_FORBIDDEN"
assert refusal.value.paths == (f"specs/new{suffix}",)
# #endregion Test.Tooling.SpecsFilePolicy.Forbidden
# #region Test.Tooling.SpecsFilePolicy.Historical [C:2] [TYPE Function]
def test_unchanged_history_and_deletion_allowed_but_modified_prototype_refuses(repository):
repo, base = repository
(repo / "specs/new.md").write_text("current decision")
candidate = commit(repo)
assert policy.check(repo, base, candidate)["changed_spec_paths"] == 1
(repo / "specs/prototype/old.py").write_text("# changed\n")
with pytest.raises(policy.PolicyRefusal, match="SPEC_POLICY_PATH_FORBIDDEN"):
policy.check(repo, base, commit(repo))
(repo / "specs/prototype/old.py").unlink()
assert policy.check(repo, base, commit(repo))["status"] == "PASS"
# #endregion Test.Tooling.SpecsFilePolicy.Historical
# #region Test.Tooling.SpecsFilePolicy.Destination [C:2] [TYPE Function]
@pytest.mark.parametrize("operation", ["rename", "copy"])
def test_copy_and_rename_check_destination(repository, operation):
repo, base = repository
source, destination = repo / SCHEMA, repo / "specs/fixture.json"
if operation == "rename":
source.rename(destination)
else:
destination.write_bytes(source.read_bytes())
candidate = commit(repo)
status = git(repo, "diff", "--name-status", "--find-copies-harder", base, candidate)
assert status.startswith("R" if operation == "rename" else "C")
with pytest.raises(policy.PolicyRefusal) as refusal:
policy.check(repo, base, candidate)
assert refusal.value.paths == ("specs/fixture.json",)
# #endregion Test.Tooling.SpecsFilePolicy.Destination
# #region Test.Tooling.SpecsFilePolicy.TypeChange [C:2] [TYPE Function]
@pytest.mark.parametrize("path,code", [("specs/prototype/old.py", "SPEC_POLICY_PATH_FORBIDDEN"),
(SCHEMA, "SPEC_POLICY_SCHEMA_INVALID")])
def test_regular_to_symlink_is_changed_not_grandfathered(repository, path, code):
repo, base = repository
source = repo / path
source.unlink()
source.symlink_to(repo / "target.json")
(repo / "target.json").write_text(json.dumps(DOCUMENT))
candidate = commit(repo)
assert git(repo, "diff", "--name-status", base, candidate).startswith("T")
with pytest.raises(policy.PolicyRefusal, match=code):
policy.check(repo, base, candidate)
# #endregion Test.Tooling.SpecsFilePolicy.TypeChange
# #region Test.Tooling.SpecsFilePolicy.Schema [C:2] [TYPE Function]
@pytest.mark.parametrize("value", [None, [], {"fixture": "not a schema"}, {"$schema": "x", "$id": "y"},
{"$schema": "", "$id": "y", "properties": {}}])
def test_allowlisted_path_does_not_admit_fixture_payload(repository, value):
repo, base = repository
(repo / SCHEMA).write_text(json.dumps(value))
with pytest.raises(policy.PolicyRefusal, match="SPEC_POLICY_SCHEMA_INVALID"):
policy.check(repo, base, commit(repo))
# #endregion Test.Tooling.SpecsFilePolicy.Schema
# #region Test.Tooling.SpecsFilePolicy.Objects [C:2] [TYPE Function]
def test_candidate_objects_win_over_uncommitted_worktree(repository):
repo, base = repository
(repo / SCHEMA).write_text(json.dumps({**DOCUMENT, "properties": {"current": {"type": "string"}}}))
candidate = commit(repo)
(repo / SCHEMA).write_text("not JSON")
assert policy.check(repo, base, candidate)["status"] == "PASS"
(repo / SCHEMA).write_text("not JSON")
invalid = commit(repo)
(repo / SCHEMA).write_text(json.dumps(DOCUMENT))
with pytest.raises(policy.PolicyRefusal, match="SPEC_POLICY_SCHEMA_INVALID"):
policy.check(repo, base, invalid)
# #endregion Test.Tooling.SpecsFilePolicy.Objects
# #region Test.Tooling.SpecsFilePolicy.Identity [C:2] [TYPE Function]
def test_wrong_base_short_alias_and_nonancestor_refuse(repository):
repo, base = repository
with pytest.raises(policy.PolicyRefusal, match="SPEC_POLICY_BASE_INVALID"):
policy.check(repo, "1" * 40, base)
with pytest.raises(policy.PolicyRefusal, match="SPEC_POLICY_COMMIT_INVALID"):
policy.check(repo, base, "HEAD")
git(repo, "checkout", "--orphan", "foreign")
git(repo, "rm", "-rf", ".")
(repo / "foreign.md").write_text("independent tree")
with pytest.raises(policy.PolicyRefusal, match="SPEC_POLICY_BASE_NOT_ANCESTOR"):
policy.check(repo, base, commit(repo))
# #endregion Test.Tooling.SpecsFilePolicy.Identity
# #region Test.Tooling.SpecsFilePolicy.Cli [C:2] [TYPE Function]
@pytest.mark.parametrize("argv", [[], ["--base", policy.POLICY_BASE], ["--commit", "1" * 40]])
def test_cli_never_infers_commit_or_base(argv):
with pytest.raises(SystemExit) as exit_code:
policy.main(argv)
assert exit_code.value.code == 2
# #endregion Test.Tooling.SpecsFilePolicy.Cli
# #endregion Test.Tooling.SpecsFilePolicy