Files
crank/tests/unit/test_check_community_scope.py
bsodfather 9a7d60593a
CI / Rust Checks (pull_request) Successful in 8m33s
CI / UI Checks (pull_request) Successful in 5s
CI / Frontend E2E (pull_request) Successful in 6m51s
CI / Community Image Smoke (pull_request) Failing after 9m10s
CI / Deploy (pull_request) Has been skipped
исправить: закрыть ревью сквозной корреляции
2026-07-31 03:01:51 +03:00

106 lines
3.7 KiB
Python

import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
CHECKER = ROOT / "scripts" / "check-community-scope.py"
class CommunityScopeCheckTests(unittest.TestCase):
def run_checker(self, root: Path, files: list[str]) -> subprocess.CompletedProcess[str]:
return subprocess.run(
[sys.executable, str(CHECKER), "--root", str(root), "--files", *files],
check=False,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
def test_passes_for_clean_community_content(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "README.md").write_text(
"Crank publishes REST API endpoints as MCP tools.\n",
encoding="utf-8",
)
result = self.run_checker(root, ["README.md"])
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn("Community scope check passed", result.stdout)
def test_fails_for_forbidden_product_marker(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "apps").mkdir()
(root / "apps" / "ui.md").write_text(
"Hidden GraphQL protocol card must not be here.\n",
encoding="utf-8",
)
result = self.run_checker(root, ["apps/ui.md"])
self.assertNotEqual(result.returncode, 0)
self.assertIn("apps/ui.md:1", result.stderr)
self.assertIn("graphql", result.stderr.lower())
def test_inline_directive_allows_only_declared_marker(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "protocol.rs").write_text(
'assert_rejected_protocol("grpc"); '
"// community-scope: allow=grpc\n"
'assert_rejected_protocol("grpc");\n'
'assert_rejected_protocol("graphql"); '
"// community-scope: allow=grpc\n",
encoding="utf-8",
)
result = self.run_checker(root, ["protocol.rs"])
self.assertNotEqual(result.returncode, 0)
self.assertIn("protocol.rs:2: forbidden marker `grpc`", result.stderr)
self.assertIn("protocol.rs:3: forbidden marker `graphql`", result.stderr)
self.assertNotIn("protocol.rs:1:", result.stderr)
def test_rejects_marker_without_inline_directive(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "README.md").write_text(
"Enable grpc transport.\n",
encoding="utf-8",
)
result = self.run_checker(root, ["README.md"])
self.assertNotEqual(result.returncode, 0)
self.assertIn("forbidden marker `grpc`", result.stderr)
def test_ignores_default_excluded_paths(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "LICENSE").write_text(
"The license text may mention commercial distribution terms.\n",
encoding="utf-8",
)
result = self.run_checker(root, ["LICENSE"])
self.assertEqual(result.returncode, 0, result.stderr)
def test_skips_binary_files(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "logo.png").write_bytes(b"\x89PNG\x00enterprise\x00")
result = self.run_checker(root, ["logo.png"])
self.assertEqual(result.returncode, 0, result.stderr)
if __name__ == "__main__":
unittest.main()