fix(adopt,init): decide by evidence, refuse instead of defaulting
Running a full assess -> plan -> apply -> verify against Infra/LXD and Infra/Docker in throwaway worktrees, and a bootstrap into a scratch directory, surfaced the same two mistakes repeated across both commands: a shape was read where evidence was available, and a missing answer became a silent default rather than a refusal. adopt: - classify secret-shaped paths by content, not by filename. A live credential blocks; a file tracked against the repository's own ignore rules and a name-only match are warnings; findings name keys, never values. In Infra/Docker this turned 14 false blockers into 2 true ones and surfaced 14 files committed despite `**/.env`. - carry assess findings into the plan and refuse while an error-severity finding is unresolved. An owner may accept one through `acknowledgedFindings` with a reason; the acknowledgement enters the plan and therefore planHash, so approval covers it. - derive metadata.name from the git remote instead of the clone directory, and refuse to plan without a confirmed owner (runbook section 9). - propose content roots from the whole content vocabulary rather than the winning type's markers, and recognise N sibling directories sharing a marker file as one file-set root with a selector, as runbook 7.2 already specified. Authority defaulted rather than derived now asks. init: apply the guards adopt already had. Answers are validated against the vocabularies that already existed in constants (name, type, lifecycle, access) so init can no longer create a repository that fails its own validate; owners are required; a directory that already holds the planned paths is refused at plan time and pointed at adopt; and every path from a plan is confined before the first byte is written -- init wrote outside the repository from an edited plan. validate: mask fenced blocks and inline code spans before scanning for Markdown links. A PowerShell snippet in Infra/Docker's CLAUDE.md parsed as a link and kept validate red for a reason unrelated to the repository. paths: allow "." as a whole content-root path, never as a segment, so the file-set root runbook 7.2 describes can be declared. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
693211cee9
commit
1ab02a257c
@@ -35,7 +35,7 @@ def test_apply_does_not_touch_working_tree_before_approval(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
before = sorted(p.relative_to(repo).as_posix() for p in repo.rglob("*") if p.is_file())
|
||||
assessment = adoption.assess(repo)
|
||||
adoption.build_plan(repo, assessment, confirmed_name="infra-lxd")
|
||||
adoption.build_plan(repo, assessment, confirmed_name="infra-lxd", confirmed_owners=["infrastructure"])
|
||||
after = sorted(p.relative_to(repo).as_posix() for p in repo.rglob("*") if p.is_file())
|
||||
assert before == after
|
||||
|
||||
@@ -43,7 +43,7 @@ def test_apply_does_not_touch_working_tree_before_approval(make_repo):
|
||||
def test_apply_rejects_stale_baseline(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="infra-lxd")
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="infra-lxd", confirmed_owners=["infrastructure"])
|
||||
plan["baselineRevision"] = "0" * 40
|
||||
with pytest.raises(ValueError):
|
||||
adoption.apply_plan(repo, plan, plan["planHash"])
|
||||
@@ -77,7 +77,7 @@ def test_assess_antivirus_legacy_detects_occupied_kdb_path(make_repo):
|
||||
def test_plan_antivirus_legacy_does_not_overwrite_existing_kdb(make_repo):
|
||||
repo = make_repo("adoption/antivirus-legacy")
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="services-antivirus")
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="services-antivirus", confirmed_owners=["security"])
|
||||
changes_by_path = {c["path"]: c for c in plan["changes"]}
|
||||
assert changes_by_path["kdb/kdb.yaml"]["action"] == "preserve"
|
||||
|
||||
@@ -86,6 +86,6 @@ def test_apply_preserves_existing_antivirus_kdb_bytes(make_repo):
|
||||
repo = make_repo("adoption/antivirus-legacy")
|
||||
original = (repo / "kdb" / "kdb.yaml").read_bytes()
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="services-antivirus")
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="services-antivirus", confirmed_owners=["security"])
|
||||
adoption.apply_plan(repo, plan, plan["planHash"])
|
||||
assert (repo / "kdb" / "kdb.yaml").read_bytes() == original
|
||||
|
||||
@@ -0,0 +1,297 @@
|
||||
"""The gates adoption must not let through, and the evidence it must not invent.
|
||||
|
||||
Every case here comes from running `adopt` against two real repositories. Each
|
||||
one passed before the fix and reported success on a repository that had live
|
||||
credentials in git, no confirmed owner, a name taken from the clone directory,
|
||||
and a contract that described almost none of its content.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from stewardship import adoption
|
||||
|
||||
BASE = {"README.md": "# Sample\n"}
|
||||
|
||||
|
||||
def _owners_plan(repo, **kwargs):
|
||||
assessment = adoption.assess(repo)
|
||||
return adoption.build_plan(
|
||||
repo, assessment, confirmed_name="sample", confirmed_owners=["platform"], **kwargs
|
||||
)
|
||||
|
||||
|
||||
def _codes(findings, code):
|
||||
return [f for f in findings if f["code"] == code]
|
||||
|
||||
|
||||
# --- 1. secret classes ------------------------------------------------------
|
||||
|
||||
def test_document_about_secrets_is_a_warning_not_a_blocker(synthetic_repo) -> None:
|
||||
"""A document explaining a placeholder convention carries no credential."""
|
||||
repo = synthetic_repo(
|
||||
{
|
||||
**BASE,
|
||||
"docs/SECRETS.md": (
|
||||
"# Secrets convention\n\n"
|
||||
"Profiles carry `<SECRET:name>` tokens instead of real credentials.\n"
|
||||
"The AD-join password was removed from every profile.\n"
|
||||
),
|
||||
}
|
||||
)
|
||||
findings = adoption.assess(repo)["findings"]
|
||||
assert _codes(findings, "ADOPT_SECRET_NAME_SHAPED")
|
||||
assert not [f for f in findings if f["severity"] == "error"]
|
||||
|
||||
|
||||
def test_live_credential_blocks_and_never_echoes_the_value(synthetic_repo) -> None:
|
||||
repo = synthetic_repo(
|
||||
{**BASE, "stack/.env": "TZ=Europe/Moscow\nPOSTGRES_PASSWORD=hunter2-live-value\n"}
|
||||
)
|
||||
findings = adoption.assess(repo)["findings"]
|
||||
verified = _codes(findings, "ADOPT_SECRET_CONTENT_VERIFIED")
|
||||
assert [f["severity"] for f in verified] == ["error"]
|
||||
assert "POSTGRES_PASSWORD" in verified[0]["message"]
|
||||
# Invariant: a scanner that leaks what it found is worse than none.
|
||||
assert "hunter2-live-value" not in verified[0]["message"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("line", "is_credential"),
|
||||
[
|
||||
("AIRFLOW_FERNET_KEY=Rabc123XYZ", True),
|
||||
("OLLAMA_API_KEY=sk-live-1", True),
|
||||
("DB_PASSPHRASE=s3kr3t", True),
|
||||
# "KEY" as a bare substring cannot tell a credential from a file path.
|
||||
("SSH_KEY_PATH=/home/u/.ssh/id", False),
|
||||
("KEYBOARD_LAYOUT=us", False),
|
||||
("BYPASS_CACHE=true", False),
|
||||
("PASSENGER_ROOT=/x", False),
|
||||
("TZ=Europe/Moscow", False),
|
||||
# Placeholders, in the three shapes real repositories use.
|
||||
("POSTGRES_PASSWORD=changeme", False),
|
||||
("DB_PASSWORD=<SECRET:db>", False),
|
||||
("API_TOKEN=${API_TOKEN}", False),
|
||||
],
|
||||
)
|
||||
def test_key_pattern_precision(tmp_path: Path, line: str, is_credential: bool) -> None:
|
||||
"""Which key names count, and which values read as placeholders.
|
||||
|
||||
Exercised against the content check directly: this is a pure decision about
|
||||
a single line, and building a git repository per case bought only runtime.
|
||||
"""
|
||||
env = tmp_path / ".env"
|
||||
env.write_text(line + "\n", encoding="utf-8")
|
||||
assert bool(adoption._secret_content_evidence(env)) is is_credential
|
||||
|
||||
|
||||
def test_pem_private_key_is_content_evidence(synthetic_repo) -> None:
|
||||
repo = synthetic_repo(
|
||||
{**BASE, "certs/server.key": "-----BEGIN RSA PRIVATE KEY-----\nAAAA\n-----END RSA PRIVATE KEY-----\n"}
|
||||
)
|
||||
findings = adoption.assess(repo)["findings"]
|
||||
assert _codes(findings, "ADOPT_SECRET_CONTENT_VERIFIED")
|
||||
|
||||
|
||||
def test_file_committed_against_its_own_ignore_rule_is_reported(synthetic_repo) -> None:
|
||||
"""The fact worth reporting most, and the one the filename check missed."""
|
||||
repo = synthetic_repo({**BASE, ".gitignore": "**/.env\n", "stack/.env": "TZ=Europe/Moscow\n"}, git=False)
|
||||
subprocess.run(["git", "init", "-q"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "config", "user.email", "t@example.com"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "config", "user.name", "T"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "add", "-A", "-f"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "commit", "-q", "-m", "base"], cwd=repo, check=True)
|
||||
|
||||
findings = adoption.assess(repo)["findings"]
|
||||
contradiction = _codes(findings, "ADOPT_SECRET_IGNORED_BUT_TRACKED")
|
||||
assert [f["path"] for f in contradiction] == ["stack/.env"]
|
||||
assert contradiction[0]["severity"] == "warning"
|
||||
|
||||
|
||||
# --- 2. plan inherits findings and refuses on unresolved blockers -----------
|
||||
|
||||
def test_plan_refuses_while_an_assess_blocker_is_unresolved(synthetic_repo) -> None:
|
||||
repo = synthetic_repo({**BASE, "stack/.env": "POSTGRES_PASSWORD=hunter2-live-value\n"})
|
||||
with pytest.raises(adoption.AdoptionError) as excinfo:
|
||||
_owners_plan(repo)
|
||||
assert excinfo.value.code == "ADOPT_ASSESS_BLOCKERS_UNRESOLVED"
|
||||
|
||||
|
||||
def test_acknowledgement_unblocks_and_is_recorded_in_the_plan(synthetic_repo) -> None:
|
||||
repo = synthetic_repo({**BASE, "stack/.env": "POSTGRES_PASSWORD=hunter2-live-value\n"})
|
||||
plan = _owners_plan(
|
||||
repo,
|
||||
acknowledged=[
|
||||
{
|
||||
"code": "ADOPT_SECRET_CONTENT_VERIFIED",
|
||||
"path": "stack/.env",
|
||||
"reason": "rotation scheduled",
|
||||
}
|
||||
],
|
||||
)
|
||||
assert plan["acknowledgedFindings"][0]["reason"] == "rotation scheduled"
|
||||
assert any(f["code"] == "ADOPT_SECRET_CONTENT_VERIFIED" for f in plan["findings"])
|
||||
|
||||
|
||||
def test_acknowledgement_changes_the_plan_hash(synthetic_repo) -> None:
|
||||
"""Approval must cover the acknowledgement, not just the file changes."""
|
||||
plain = synthetic_repo({**BASE}, name="plain")
|
||||
without = _owners_plan(plain)
|
||||
with_ack = _owners_plan(
|
||||
plain, acknowledged=[{"code": "ADOPT_WORKTREE_DIRTY", "reason": "known"}]
|
||||
)
|
||||
assert without["planHash"] != with_ack["planHash"]
|
||||
|
||||
|
||||
def test_acknowledgement_without_a_reason_is_refused(synthetic_repo) -> None:
|
||||
repo = synthetic_repo({**BASE})
|
||||
with pytest.raises(adoption.AdoptionError) as excinfo:
|
||||
_owners_plan(repo, acknowledged=[{"code": "ADOPT_SECRET_CONTENT_VERIFIED", "reason": " "}])
|
||||
assert excinfo.value.code == "ADOPT_ACKNOWLEDGEMENT_INCOMPLETE"
|
||||
|
||||
|
||||
def test_plan_carries_the_assessment_findings(synthetic_repo) -> None:
|
||||
repo = synthetic_repo({**BASE, "docs/guide.md": "# G\n"})
|
||||
plan = _owners_plan(repo)
|
||||
assert any(f["code"] == "ADOPT_SOURCE_MIRROR_AMBIGUOUS" for f in plan["findings"])
|
||||
# The plan's own answer settles this one, so it must not travel onward.
|
||||
assert not any(f["code"] == "ADOPT_OWNER_UNKNOWN" for f in plan["findings"])
|
||||
|
||||
|
||||
# --- 3. identity and owners -------------------------------------------------
|
||||
|
||||
def test_plan_refuses_without_confirmed_owners(synthetic_repo) -> None:
|
||||
repo = synthetic_repo({**BASE})
|
||||
assessment = adoption.assess(repo)
|
||||
with pytest.raises(adoption.AdoptionError) as excinfo:
|
||||
adoption.build_plan(repo, assessment, confirmed_name="sample", confirmed_owners=[])
|
||||
assert excinfo.value.code == "ADOPT_OWNER_UNKNOWN"
|
||||
|
||||
|
||||
def test_name_comes_from_the_remote_keeping_its_namespace(synthetic_repo) -> None:
|
||||
repo = synthetic_repo({**BASE}, name="whatever-this-clone-is-called")
|
||||
subprocess.run(
|
||||
["git", "remote", "add", "origin", "ssh://git@example.com:9922/corsus-it/OpTools/LXD.git"],
|
||||
cwd=repo,
|
||||
check=True,
|
||||
)
|
||||
identity = adoption.assess(repo)["identity"]
|
||||
assert identity == {"name": "optools-lxd", "source": "git-remote"}
|
||||
|
||||
|
||||
def test_scp_style_remote_is_understood(synthetic_repo) -> None:
|
||||
repo = synthetic_repo({**BASE}, name="clone")
|
||||
subprocess.run(["git", "remote", "add", "origin", "git@example.com:group/Repo.git"], cwd=repo, check=True)
|
||||
assert adoption.assess(repo)["identity"]["name"] == "group-repo"
|
||||
|
||||
|
||||
def test_directory_fallback_says_it_was_inferred(synthetic_repo) -> None:
|
||||
repo = synthetic_repo({**BASE}, name="fallback-repo")
|
||||
assessment = adoption.assess(repo)
|
||||
assert assessment["identity"] == {"name": "fallback-repo", "source": "directory"}
|
||||
assert _codes(assessment["findings"], "ADOPT_NAME_INFERRED")
|
||||
|
||||
|
||||
# --- 4. content roots -------------------------------------------------------
|
||||
|
||||
def test_docs_is_proposed_for_an_infrastructure_repository(synthetic_repo) -> None:
|
||||
"""`docs` is listed as evidence for `project`, which used to hide it here."""
|
||||
repo = synthetic_repo({**BASE, "profiles-source/a.yaml": "{}\n", "docs/guide.md": "# G\n"})
|
||||
assessment = adoption.assess(repo)
|
||||
assert assessment["classification"]["proposedType"] == "infrastructure-config"
|
||||
assert "docs" in {cr["path"] for cr in assessment["proposedContentRoots"]}
|
||||
|
||||
|
||||
def test_sibling_stacks_become_one_file_set_root(synthetic_repo) -> None:
|
||||
files = {**BASE}
|
||||
for stack in ("grafana", "loki", "n8n"):
|
||||
files[f"{stack}/compose.yaml"] = "services: {}\n"
|
||||
repo = synthetic_repo(files)
|
||||
roots = adoption.assess(repo)["proposedContentRoots"]
|
||||
stacks = [cr for cr in roots if cr.get("kind") == "file-set"]
|
||||
assert len(stacks) == 1
|
||||
assert stacks[0]["path"] == "." and stacks[0]["selector"] == "*/compose.y*ml"
|
||||
|
||||
|
||||
def test_two_siblings_are_not_a_repeated_shape(synthetic_repo) -> None:
|
||||
files = {**BASE}
|
||||
for stack in ("grafana", "loki"):
|
||||
files[f"{stack}/compose.yaml"] = "services: {}\n"
|
||||
repo = synthetic_repo(files)
|
||||
assert not [cr for cr in adoption.assess(repo)["proposedContentRoots"] if cr.get("kind") == "file-set"]
|
||||
|
||||
|
||||
def test_file_set_root_does_not_forbid_the_whole_repository(synthetic_repo) -> None:
|
||||
files = {**BASE}
|
||||
for stack in ("grafana", "loki", "n8n"):
|
||||
files[f"{stack}/compose.yaml"] = "services: {}\n"
|
||||
repo = synthetic_repo(files)
|
||||
plan = _owners_plan(repo)
|
||||
assert "./**" not in plan["forbiddenChanges"]
|
||||
assert "*/compose.y*ml" in plan["forbiddenChanges"]
|
||||
# The plan must still be able to write what it authors.
|
||||
written = {c["path"] for c in plan["changes"] if c["action"] == "create"}
|
||||
assert not any(adoption.is_forbidden(path, plan["forbiddenChanges"]) for path in written)
|
||||
|
||||
|
||||
def test_defaulted_authority_asks_instead_of_asserting(synthetic_repo) -> None:
|
||||
repo = synthetic_repo({**BASE, "docs/guide.md": "# G\n"})
|
||||
assessment = adoption.assess(repo)
|
||||
assert _codes(assessment["findings"], "ADOPT_SOURCE_MIRROR_AMBIGUOUS")
|
||||
|
||||
|
||||
def test_evidenced_mirror_is_not_ambiguous(synthetic_repo) -> None:
|
||||
"""`profiles` paired with `profiles-source` is derived, not defaulted."""
|
||||
repo = synthetic_repo({**BASE, "profiles/a.yaml": "{}\n", "profiles-source/a.yaml": "{}\n"})
|
||||
assessment = adoption.assess(repo)
|
||||
mirror = next(cr for cr in assessment["proposedContentRoots"] if cr["path"] == "profiles")
|
||||
assert mirror["authority"] == "mirror"
|
||||
ambiguous = {f["path"] for f in _codes(assessment["findings"], "ADOPT_SOURCE_MIRROR_AMBIGUOUS")}
|
||||
assert "profiles" not in ambiguous
|
||||
|
||||
|
||||
# --- 5. markdown links are not found inside code ----------------------------
|
||||
|
||||
def test_link_shaped_code_is_not_a_link(tmp_path: Path) -> None:
|
||||
from stewardship.markdown import check_markdown_file
|
||||
|
||||
(tmp_path / "CLAUDE.md").write_text(
|
||||
"# Doc\n\n"
|
||||
"Cast explicitly: `[string]([IO.File]::ReadAllText($path))`.\n\n"
|
||||
"```powershell\n"
|
||||
"see [also](nowhere/missing.md)\n"
|
||||
"```\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
assert check_markdown_file(tmp_path, "CLAUDE.md") == []
|
||||
|
||||
|
||||
def test_a_real_broken_link_is_still_reported(tmp_path: Path) -> None:
|
||||
from stewardship.markdown import check_markdown_file
|
||||
|
||||
(tmp_path / "CLAUDE.md").write_text("# Doc\n\nSee [x](docs/missing.md).\n", encoding="utf-8")
|
||||
codes = [f.code for f in check_markdown_file(tmp_path, "CLAUDE.md")]
|
||||
assert codes == ["MARKDOWN_LINK_TARGET_MISSING"]
|
||||
|
||||
|
||||
# --- path confinement: "." is a whole path, never a segment ------------------
|
||||
|
||||
def test_repository_root_is_a_legal_content_root_path() -> None:
|
||||
from stewardship.paths import normalize_resource_path
|
||||
|
||||
assert normalize_resource_path(".", allow_root=True) == "."
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "allow_root"),
|
||||
[(".", False), ("a/./b", True), ("../escape", True), ("a/../..", True)],
|
||||
)
|
||||
def test_dot_segments_stay_rejected(value: str, allow_root: bool) -> None:
|
||||
from stewardship.paths import PathViolation, normalize_resource_path
|
||||
|
||||
with pytest.raises(PathViolation):
|
||||
normalize_resource_path(value, allow_root=allow_root)
|
||||
@@ -31,7 +31,7 @@ BASE = {"README.md": "# Sample\n", "policies/.gitkeep": ""}
|
||||
|
||||
def _plan_for(repo):
|
||||
assessment = adoption.assess(repo)
|
||||
return assessment, adoption.build_plan(repo, assessment, confirmed_name="sample")
|
||||
return assessment, adoption.build_plan(repo, assessment, confirmed_name="sample", confirmed_owners=["platform"])
|
||||
|
||||
|
||||
def _actions(plan):
|
||||
@@ -172,7 +172,7 @@ def test_both_canonical_paths_occupied_refuses_to_plan(synthetic_repo):
|
||||
assessment = adoption.assess(repo)
|
||||
|
||||
with pytest.raises(adoption.AdoptionError) as excinfo:
|
||||
adoption.build_plan(repo, assessment, confirmed_name="sample")
|
||||
adoption.build_plan(repo, assessment, confirmed_name="sample", confirmed_owners=["platform"])
|
||||
assert excinfo.value.code == "ADOPT_KNOWLEDGE_UNRESOLVED"
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
"""What `init` must refuse before it writes a repository.
|
||||
|
||||
`init` is the one path where the tool controls every byte of the result, so a
|
||||
repository it creates must pass `repoctl validate` by construction. Each case
|
||||
here produced a `success` envelope before the fix: an invalid contract, an
|
||||
unconfirmed owner, a plan that could never be applied, or a write outside the
|
||||
repository from a hand-edited plan.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from stewardship import bootstrap, io
|
||||
from stewardship.validate import validate_repository
|
||||
|
||||
VALID = {
|
||||
"name": "services-mail",
|
||||
"title": "Corporate mail service",
|
||||
"owners": ["it-infrastructure"],
|
||||
"repositoryType": "service-config",
|
||||
"lifecycle": "production",
|
||||
}
|
||||
|
||||
|
||||
def _answers(**overrides):
|
||||
data = dict(VALID)
|
||||
data.update(overrides)
|
||||
return data
|
||||
|
||||
|
||||
# --- answers that would produce an invalid repository ------------------------
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("overrides", "code"),
|
||||
[
|
||||
({"name": "Services/Mail"}, "INIT_NAME_INVALID"),
|
||||
({"name": "Services Mail"}, "INIT_NAME_INVALID"),
|
||||
({"name": "MAIL"}, "INIT_NAME_INVALID"),
|
||||
({"name": "-leading-dash"}, "INIT_NAME_INVALID"),
|
||||
({"repositoryType": "mail-server"}, "INIT_TYPE_UNKNOWN"),
|
||||
({"lifecycle": "bogus"}, "INIT_LIFECYCLE_UNKNOWN"),
|
||||
({"access": "semi-public"}, "INIT_ACCESS_UNKNOWN"),
|
||||
({"owners": []}, "INIT_OWNER_UNKNOWN"),
|
||||
({"name": ""}, "INIT_ANSWERS_INCOMPLETE"),
|
||||
],
|
||||
)
|
||||
def test_answers_that_break_the_schema_are_refused(overrides, code) -> None:
|
||||
with pytest.raises(bootstrap.BootstrapError) as excinfo:
|
||||
bootstrap.BootstrapAnswers.from_mapping(_answers(**overrides))
|
||||
assert excinfo.value.code == code
|
||||
|
||||
|
||||
def test_a_rejected_name_comes_with_a_usable_suggestion() -> None:
|
||||
"""An error that only says "invalid" makes the owner guess the rule."""
|
||||
with pytest.raises(bootstrap.BootstrapError) as excinfo:
|
||||
bootstrap.BootstrapAnswers.from_mapping(_answers(name="Services/Mail"))
|
||||
assert "'services-mail'" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_owner_refusal_names_both_ways_to_answer() -> None:
|
||||
"""An error that only says "refused" leaves the reader guessing the flag."""
|
||||
with pytest.raises(bootstrap.BootstrapError) as excinfo:
|
||||
bootstrap.BootstrapAnswers.from_mapping(_answers(owners=[]))
|
||||
message = str(excinfo.value)
|
||||
assert "--owner" in message and "owners:" in message
|
||||
|
||||
|
||||
# --- the created repository must validate ------------------------------------
|
||||
|
||||
def test_a_freshly_created_repository_validates(tmp_path: Path) -> None:
|
||||
repo = tmp_path / "mail"
|
||||
repo.mkdir()
|
||||
answers = bootstrap.BootstrapAnswers.from_mapping(_answers())
|
||||
plan = bootstrap.build_bootstrap_plan(answers, repo)
|
||||
bootstrap.apply_bootstrap_plan(repo, plan, plan["planHash"])
|
||||
(repo / ".git").mkdir()
|
||||
|
||||
report = validate_repository(repo)
|
||||
assert not [f for f in report.findings if f.severity == "error"]
|
||||
# Content-root directories are deliberately not created (bootstrap docstring:
|
||||
# no placeholder directories), so those warnings are expected, not failures.
|
||||
assert {f.code for f in report.findings} <= {"CONTENT_ROOT_PATH_MISSING"}
|
||||
|
||||
|
||||
# --- collisions belong to plan, not to apply ---------------------------------
|
||||
|
||||
def test_plan_refuses_a_directory_that_is_already_a_repository(tmp_path: Path) -> None:
|
||||
repo = tmp_path / "existing"
|
||||
(repo / "ops").mkdir(parents=True)
|
||||
(repo / "ops" / "contract.yaml").write_text("existing: true\n", encoding="utf-8")
|
||||
answers = bootstrap.BootstrapAnswers.from_mapping(_answers())
|
||||
with pytest.raises(bootstrap.BootstrapError) as excinfo:
|
||||
bootstrap.build_bootstrap_plan(answers, repo)
|
||||
assert excinfo.value.code == "INIT_PATH_COLLISION"
|
||||
assert "adopt" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_planning_without_a_target_still_works(tmp_path: Path) -> None:
|
||||
"""`root` is optional; omitting it plans without the collision check."""
|
||||
answers = bootstrap.BootstrapAnswers.from_mapping(_answers())
|
||||
assert bootstrap.build_bootstrap_plan(answers)["planHash"].startswith("sha256:")
|
||||
|
||||
|
||||
# --- a plan is an editable file, so apply confines every path ----------------
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"escape",
|
||||
["../ESCAPED.md", "../../ESCAPED.md", "ops/../../ESCAPED.md", "/etc/passwd", "C:/Windows/x"],
|
||||
)
|
||||
def test_apply_refuses_to_write_outside_the_repository(tmp_path: Path, escape: str) -> None:
|
||||
repo = tmp_path / "inner"
|
||||
repo.mkdir()
|
||||
answers = bootstrap.BootstrapAnswers.from_mapping(_answers())
|
||||
plan = bootstrap.build_bootstrap_plan(answers, repo)
|
||||
plan["files"] = [{"path": escape, "content": "escaped\n"}]
|
||||
plan.pop("planHash")
|
||||
plan["planHash"] = io.sha256(plan)
|
||||
|
||||
with pytest.raises(bootstrap.BootstrapError) as excinfo:
|
||||
bootstrap.apply_bootstrap_plan(repo, plan, plan["planHash"])
|
||||
assert excinfo.value.code == "INIT_PATH_OUTSIDE_REPOSITORY"
|
||||
assert not list(tmp_path.glob("ESCAPED.md"))
|
||||
assert not list(tmp_path.parent.glob("ESCAPED.md"))
|
||||
|
||||
|
||||
def test_a_refused_escape_writes_nothing_at_all(tmp_path: Path) -> None:
|
||||
"""Confinement runs over every entry before the first byte is written."""
|
||||
repo = tmp_path / "inner"
|
||||
repo.mkdir()
|
||||
answers = bootstrap.BootstrapAnswers.from_mapping(_answers())
|
||||
plan = bootstrap.build_bootstrap_plan(answers, repo)
|
||||
plan["files"].append({"path": "../ESCAPED.md", "content": "escaped\n"})
|
||||
plan.pop("planHash")
|
||||
plan["planHash"] = io.sha256(plan)
|
||||
|
||||
with pytest.raises(bootstrap.BootstrapError):
|
||||
bootstrap.apply_bootstrap_plan(repo, plan, plan["planHash"])
|
||||
assert not (repo / "ops" / "contract.yaml").exists()
|
||||
|
||||
|
||||
# --- refusals stay machine-readable ------------------------------------------
|
||||
|
||||
def test_refusals_are_envelopes_not_stderr_lines(tmp_path: Path, capsys) -> None:
|
||||
from stewardship.cli import main
|
||||
|
||||
repo = tmp_path / "mail"
|
||||
repo.mkdir()
|
||||
exit_code = main(
|
||||
["init", "--repo", str(repo), "--name", "Services/Mail",
|
||||
"--type", "service-config", "--owner", "x", "--output", "json"]
|
||||
)
|
||||
envelope = json.loads(capsys.readouterr().out)
|
||||
assert exit_code != 0
|
||||
assert envelope["outcome"] == "failure"
|
||||
assert envelope["findings"][0]["code"] == "INIT_NAME_INVALID"
|
||||
Reference in New Issue
Block a user