Каждый markdown-рендерер процентно кодирует ссылку на файл с пробелами или не-ASCII в имени. Проверка сравнивала закодированную форму с файловой системой и объявляла отсутствующим файл, который лежит на месте: MARKDOWN_LINK_TARGET_MISSING: GitLab%2017.10.0%20%E2%86%92%20Docker%20Migration.md Ложное срабатывание блокировало `validate` на репозитории Infra/Docker. Ссылка декодируется перед резолвом; тест закрепляет и пробел, и то, что настоящая битая ссылка по-прежнему находится. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
345 lines
15 KiB
Python
345 lines
15 KiB
Python
"""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_repeated_shape_is_found_below_the_root(synthetic_repo) -> None:
|
|
"""Stacks grouped by deployment model sit at `swarm/<stack>/`, not at the root."""
|
|
files = {**BASE}
|
|
for stack in ("grafana", "loki", "n8n"):
|
|
files[f"swarm/{stack}/docker-compose.yaml"] = "services: {}\n"
|
|
repo = synthetic_repo(files)
|
|
roots = [cr for cr in adoption.assess(repo)["proposedContentRoots"] if cr.get("kind") == "file-set"]
|
|
assert len(roots) == 1
|
|
assert roots[0]["path"] == "swarm"
|
|
assert roots[0]["selector"] == "*/docker-compose.y*ml"
|
|
|
|
|
|
def test_nested_file_set_forbids_the_depth_its_files_are_at(synthetic_repo) -> None:
|
|
"""The selector is relative to `path`, so the forbidden glob must carry `path`."""
|
|
files = {**BASE}
|
|
for stack in ("grafana", "loki", "n8n"):
|
|
files[f"swarm/{stack}/docker-compose.yaml"] = "services: {}\n"
|
|
repo = synthetic_repo(files)
|
|
plan = _owners_plan(repo)
|
|
assert "swarm/*/docker-compose.y*ml" in plan["forbiddenChanges"]
|
|
assert "*/docker-compose.y*ml" not in plan["forbiddenChanges"]
|
|
assert adoption.is_forbidden("swarm/grafana/docker-compose.yaml", plan["forbiddenChanges"])
|
|
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_two_parents_both_repeat_the_shape(synthetic_repo) -> None:
|
|
"""`swarm/` and `compose/` are two content roots, not a contest between them."""
|
|
files = {**BASE}
|
|
for stack in ("grafana", "loki", "n8n"):
|
|
files[f"swarm/{stack}/docker-compose.yaml"] = "services: {}\n"
|
|
for stack in ("portainer", "proxy", "agent"):
|
|
files[f"compose/{stack}/docker-compose.yaml"] = "services: {}\n"
|
|
repo = synthetic_repo(files)
|
|
roots = [cr for cr in adoption.assess(repo)["proposedContentRoots"] if cr.get("kind") == "file-set"]
|
|
assert {cr["path"] for cr in roots} == {"swarm", "compose"}
|
|
assert len({cr["id"] for cr in roots}) == 2
|
|
|
|
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"]
|
|
|
|
|
|
def test_percent_encoded_link_resolves_to_the_real_file(tmp_path) -> None:
|
|
"""Renderers percent-encode spaces and non-ASCII; the file is still there."""
|
|
from stewardship.markdown import check_markdown_file
|
|
|
|
(tmp_path / "GitLab 17.10.0 Migration.md").write_text("# M" + chr(10), encoding="utf-8")
|
|
link = "See [m](GitLab%2017.10.0%20Migration.md)."
|
|
(tmp_path / "CLAUDE.md").write_text("# Doc" + chr(10) * 2 + link + chr(10), encoding="utf-8")
|
|
assert check_markdown_file(tmp_path, "CLAUDE.md") == []
|
|
|
|
# --- 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)
|