ploop/scripts/tests/test_make_targets.py
2026-08-31 20:19:05 +08:00

247 lines
9.1 KiB
Python

from __future__ import annotations
import os
import shutil
import subprocess
import sys
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
REPOSITORY_ROOT = Path(__file__).resolve().parents[2]
def run_make(target: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
["make", target],
cwd=REPOSITORY_ROOT,
text=True,
capture_output=True,
check=False,
)
def run_make_with_variables(target: str, **variables: str) -> subprocess.CompletedProcess[str]:
assignments = [f"{key}={value}" for key, value in variables.items()]
return subprocess.run(
["make", *assignments, target],
cwd=REPOSITORY_ROOT,
text=True,
capture_output=True,
check=False,
)
def run_make_at(root: Path, target: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
["make", target],
cwd=root,
text=True,
capture_output=True,
check=False,
)
class MakeTargetTest(unittest.TestCase):
def test_check_backend_target_is_available(self) -> None:
result = run_make("check-backend")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_check_backend_pins_python_312(self) -> None:
result = subprocess.run(
["make", "--dry-run", "check-backend"],
cwd=REPOSITORY_ROOT,
text=True,
capture_output=True,
check=False,
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("--python 3.12", result.stdout)
def test_check_docs_excludes_dependency_and_build_directories(self) -> None:
result = subprocess.run(
["make", "--dry-run", "check-docs"],
cwd=REPOSITORY_ROOT,
text=True,
capture_output=True,
check=False,
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("--exclude frontend/node_modules", result.stdout)
self.assertIn("--exclude frontend/dist", result.stdout)
self.assertIn("--exclude backend/.venv", result.stdout)
def test_check_docs_excludes_nested_worktrees(self) -> None:
with TemporaryDirectory() as temporary_directory:
root = Path(temporary_directory)
(root / "scripts").mkdir()
shutil.copy2(REPOSITORY_ROOT / "Makefile", root / "Makefile")
shutil.copy2(
REPOSITORY_ROOT / "scripts" / "check_docs.py",
root / "scripts" / "check_docs.py",
)
(root / "README.md").write_text("# Root\n", encoding="utf-8")
nested_worktree = root / ".worktrees" / "feature"
nested_worktree.mkdir(parents=True)
(nested_worktree / "invalid.md").write_text("[missing](missing.md)\n", encoding="utf-8")
result = run_make_at(root, "check-docs")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("errors=0", result.stdout)
def test_check_frontend_target_is_available(self) -> None:
result = run_make("check-frontend")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_browser_target_builds_current_frontend_before_starting_playwright(self) -> None:
"""检测单独运行浏览器测试时,预览服务使用过期构建或缺少构建的问题。"""
with TemporaryDirectory() as temporary_directory:
runner = Path(temporary_directory) / "pnpm_boundary.py"
runner.write_text(
"from pathlib import Path\n"
"import sys\n"
"built = Path(__file__).with_suffix('.built')\n"
"arguments = sys.argv[1:]\n"
"if arguments == ['--dir', 'frontend', 'build']:\n"
" built.write_text('current frontend')\n"
"elif arguments == ['--dir', 'frontend', 'exec', 'playwright', 'test', "
"'--config', 'tests/e2e/playwright.config.ts']:\n"
" if not built.exists():\n"
" sys.exit('Playwright started without building current frontend')\n"
" print('browser tested current frontend')\n"
"else:\n"
" sys.exit('unexpected frontend command')\n",
encoding="utf-8",
)
result = run_make_with_variables("test-browser", PNPM=f'"{sys.executable}" "{runner}"')
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("browser tested current frontend", result.stdout)
def test_check_architecture_is_part_of_default_gate(self) -> None:
valid_root = "scripts/tests/fixtures/architecture/valid"
violation_root = "scripts/tests/fixtures/architecture/violations/exec001"
explicit_valid = run_make_with_variables("check-architecture", ARCHITECTURE_ROOT=valid_root)
explicit_violation = run_make_with_variables(
"check-architecture", ARCHITECTURE_ROOT=violation_root
)
default_violation = run_make_with_variables("check", ARCHITECTURE_ROOT=violation_root)
self.assertEqual(
explicit_valid.returncode,
0,
explicit_valid.stdout + explicit_valid.stderr,
)
self.assertNotEqual(explicit_violation.returncode, 0)
self.assertIn("EXEC001", explicit_violation.stdout)
self.assertNotEqual(default_violation.returncode, 0)
self.assertIn("EXEC001", default_violation.stdout)
def test_architecture_syncs_frozen_frontend_dependencies_before_the_parser(self) -> None:
result = subprocess.run(
["make", "--dry-run", "check-architecture"],
cwd=REPOSITORY_ROOT,
text=True,
capture_output=True,
check=False,
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
sync = "pnpm --dir frontend install --frozen-lockfile"
checker = "python3 scripts/check_frontend_architecture.py frontend/src frontend/tests/e2e"
self.assertIn(sync, result.stdout)
self.assertLess(result.stdout.index(sync), result.stdout.index(checker))
def test_default_gate_reuses_the_frontend_dependency_prerequisite(self) -> None:
result = subprocess.run(
["make", "--dry-run", "check"],
cwd=REPOSITORY_ROOT,
text=True,
capture_output=True,
check=False,
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
sync = "pnpm --dir frontend install --frozen-lockfile"
self.assertEqual(result.stdout.count(sync), 1)
self.assertLess(
result.stdout.index(sync), result.stdout.index("scripts/check_frontend_architecture.py")
)
def test_check_dev_services_target_is_explicit_and_off_the_default_gate(self) -> None:
development_services = subprocess.run(
["make", "--dry-run", "check-dev-services"],
cwd=REPOSITORY_ROOT,
text=True,
capture_output=True,
check=False,
)
default_check = subprocess.run(
["make", "--dry-run", "check"],
cwd=REPOSITORY_ROOT,
text=True,
capture_output=True,
check=False,
)
self.assertEqual(
development_services.returncode,
0,
development_services.stdout + development_services.stderr,
)
self.assertIn("scripts/check_dev_services.py --env .env", development_services.stdout)
self.assertNotIn("check_dev_services.py", default_check.stdout)
def test_check_migrations_target_is_explicit_and_off_the_default_gate(self) -> None:
migration_check = subprocess.run(
["make", "--dry-run", "check-migrations"],
cwd=REPOSITORY_ROOT,
text=True,
capture_output=True,
check=False,
)
default_check = subprocess.run(
["make", "--dry-run", "check"],
cwd=REPOSITORY_ROOT,
text=True,
capture_output=True,
check=False,
)
self.assertEqual(
migration_check.returncode,
0,
migration_check.stdout + migration_check.stderr,
)
self.assertIn("check_migrations.py --allow-migration", migration_check.stdout)
self.assertNotIn("check_migrations.py", default_check.stdout)
def test_check_target_runs_all_m0a_gates(self) -> None:
result = run_make("check")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_ci_script_rejects_unknown_tier(self) -> None:
result = subprocess.run(
["sh", "scripts/ci.sh"],
cwd=REPOSITORY_ROOT,
env={**os.environ, "CI_TIER": "unknown"},
text=True,
capture_output=True,
check=False,
)
self.assertNotEqual(result.returncode, 0)
self.assertIn("unsupported CI_TIER", result.stdout + result.stderr)
if __name__ == "__main__":
unittest.main()