199 lines
9.0 KiB
Python
199 lines
9.0 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
|
|
MODULE_PATH = Path(__file__).with_name("docs-check.py")
|
|
SPEC = importlib.util.spec_from_file_location("docs_check", MODULE_PATH)
|
|
assert SPEC is not None and SPEC.loader is not None
|
|
DOCS_CHECK = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(DOCS_CHECK)
|
|
|
|
|
|
class RepositoryTestCase(unittest.TestCase):
|
|
"""Create a minimal valid repository layout in a temporary directory."""
|
|
|
|
def setUp(self) -> None:
|
|
self.temporary = tempfile.TemporaryDirectory()
|
|
self.root = Path(self.temporary.name)
|
|
self.write("README.md", "# Root\n\n[Guide](docs/en/DEVELOPMENT.md)\n")
|
|
self.write("README.en.md", "# Root\n")
|
|
self.write("AGENTS.md", "Project rules.\n")
|
|
self.write("CLAUDE.md", "@AGENTS.md\n")
|
|
self.write(
|
|
"AGENTS_ROUTING.md",
|
|
"Read `.agents/workflows/development.md` and `.agents/workflows/validation.md`.\n",
|
|
)
|
|
self.write(".agents/workflows/development.md", "Development.\n")
|
|
self.write(".agents/workflows/validation.md", "Validation.\n")
|
|
for language in ("ru", "en"):
|
|
for name in ("ARCHITECTURE", "DEPLOYMENT", "DEVELOPMENT", "SECURITY", "TESTING"):
|
|
self.write(f"docs/{language}/{name}.md", f"# {name}\n")
|
|
|
|
def tearDown(self) -> None:
|
|
self.temporary.cleanup()
|
|
|
|
def write(self, relative: str, content: str) -> Path:
|
|
path = self.root / relative
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(content, encoding="utf-8")
|
|
return path
|
|
|
|
|
|
class DocsCheckTests(RepositoryTestCase):
|
|
def test_accepts_angle_bracket_path_with_spaces(self) -> None:
|
|
self.write("README.md", "# Root\n\n[Guide](<guides/guide with spaces.md>)\n")
|
|
self.write("guides/guide with spaces.md", "# Guide\n")
|
|
self.assertEqual([], DOCS_CHECK.validate_repository(self.root))
|
|
|
|
def test_accepts_additional_paired_translation_files(self) -> None:
|
|
self.write("docs/ru/FAQ.md", "# Вопросы\n")
|
|
self.write("docs/en/FAQ.md", "# FAQ\n")
|
|
self.assertEqual([], DOCS_CHECK.validate_repository(self.root))
|
|
|
|
def test_requires_deployment_and_security_guides(self) -> None:
|
|
for name in ("DEPLOYMENT", "SECURITY"):
|
|
with self.subTest(name=name):
|
|
path = self.root / "docs/ru" / f"{name}.md"
|
|
path.unlink()
|
|
self.write("docs/ru/DEPLOY.md", "# Legacy deployment guide\n")
|
|
errors = DOCS_CHECK.validate_repository(self.root)
|
|
self.assertIn(f"docs/ru is missing required files: {name}.md", errors)
|
|
self.write(f"docs/ru/{name}.md", f"# {name}\n")
|
|
|
|
def test_accepts_top_level_readme_pairs(self) -> None:
|
|
for directory in ("backend", ".github", ".agents", "docs"):
|
|
self.write(f"{directory}/README.md", "[English](README.en.md)\n")
|
|
self.write(f"{directory}/README.en.md", "[Русский](README.md)\n")
|
|
self.assertEqual([], DOCS_CHECK.validate_repository(self.root))
|
|
|
|
def test_requires_both_readme_languages(self) -> None:
|
|
for missing in ("README.md", "README.en.md"):
|
|
with self.subTest(missing=missing):
|
|
self.write("backend/README.md", "# Backend\n")
|
|
self.write("backend/README.en.md", "# Backend\n")
|
|
(self.root / "backend" / missing).unlink()
|
|
errors = DOCS_CHECK.validate_repository(self.root)
|
|
self.assertIn(f"missing README translation: backend/{missing}", errors)
|
|
|
|
def test_accepts_github_without_readmes(self) -> None:
|
|
self.write(".github/workflows/ci.yml", "name: CI\n")
|
|
self.assertEqual([], DOCS_CHECK.validate_repository(self.root))
|
|
|
|
def test_does_not_require_github_readme_translation(self) -> None:
|
|
for name in ("README.md", "README.en.md"):
|
|
with self.subTest(name=name):
|
|
path = self.write(f".github/{name}", "# GitHub Actions\n")
|
|
errors = DOCS_CHECK.validate_repository(self.root)
|
|
path.unlink()
|
|
self.assertEqual([], errors)
|
|
|
|
def test_still_checks_links_in_github(self) -> None:
|
|
self.write(".github/CONTRIBUTING.md", "[Missing](missing.md)\n")
|
|
errors = DOCS_CHECK.validate_repository(self.root)
|
|
self.assertTrue(any("broken local link" in error for error in errors), errors)
|
|
|
|
def test_requires_root_readme_translation(self) -> None:
|
|
(self.root / "README.en.md").unlink()
|
|
errors = DOCS_CHECK.validate_repository(self.root)
|
|
self.assertIn("missing README translation: README.en.md", errors)
|
|
|
|
def test_rejects_deeply_nested_readmes(self) -> None:
|
|
self.write("backend/internal/README.md", "# Internal\n")
|
|
self.write("backend/internal/README.en.md", "# Internal\n")
|
|
errors = DOCS_CHECK.validate_repository(self.root)
|
|
self.assertTrue(any("README is below a top-level directory" in error for error in errors), errors)
|
|
|
|
def test_ignores_scanner_dependency_documentation(self) -> None:
|
|
for directory in (".cache", ".bin", ".tools"):
|
|
self.write(f"security/{directory}/dependency/README.md", "[Upstream](missing.md)\n")
|
|
self.assertEqual([], DOCS_CHECK.validate_repository(self.root))
|
|
|
|
def test_ignores_link_syntax_inside_inline_code(self) -> None:
|
|
self.write("README.md", "Use `[missing](not-a-real-link.md)` as an example.\n")
|
|
self.assertEqual([], DOCS_CHECK.validate_repository(self.root))
|
|
|
|
def test_reports_broken_local_markdown_link(self) -> None:
|
|
self.write("README.md", "[Missing](docs/en/missing.md)\n")
|
|
errors = DOCS_CHECK.validate_repository(self.root)
|
|
self.assertTrue(any("broken local link" in error for error in errors), errors)
|
|
|
|
def test_reports_typo_in_agent_route_target(self) -> None:
|
|
self.write("AGENTS_ROUTING.md", "Read `.agents/workflows/developmnt.md`.\n")
|
|
errors = DOCS_CHECK.validate_repository(self.root)
|
|
self.assertTrue(any("missing route target" in error for error in errors), errors)
|
|
|
|
def test_reports_instruction_import_cycle(self) -> None:
|
|
self.write("AGENTS.md", "@CLAUDE.md\n")
|
|
self.write("CLAUDE.md", "@AGENTS.md\n")
|
|
errors = DOCS_CHECK.validate_repository(self.root)
|
|
self.assertTrue(any("instruction import cycle" in error for error in errors), errors)
|
|
|
|
|
|
@unittest.skipUnless(shutil.which("git"), "git is required for ignore-rule tests")
|
|
class DocsCheckGitIgnoreTests(RepositoryTestCase):
|
|
LOCAL_README = "docs/other/notes/README.md"
|
|
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
# Isolate git from user and system configuration and from any parent repository.
|
|
environment = mock.patch.dict(
|
|
os.environ,
|
|
{
|
|
"GIT_CONFIG_GLOBAL": os.devnull,
|
|
"GIT_CONFIG_NOSYSTEM": "1",
|
|
"GIT_CEILING_DIRECTORIES": str(self.root.resolve().parent),
|
|
},
|
|
)
|
|
environment.start()
|
|
self.addCleanup(environment.stop)
|
|
self.write(self.LOCAL_README, "[Missing](missing.md)\n")
|
|
|
|
def git(self, *arguments: str) -> None:
|
|
subprocess.run(["git", *arguments], cwd=self.root, check=True, capture_output=True)
|
|
|
|
def local_errors(self) -> list[str]:
|
|
errors = DOCS_CHECK.validate_repository(self.root)
|
|
return [error for error in errors if self.LOCAL_README in error]
|
|
|
|
def test_skips_markdown_excluded_through_info_exclude(self) -> None:
|
|
self.git("init", "--quiet")
|
|
with (self.root / ".git" / "info" / "exclude").open("a", encoding="utf-8") as exclude:
|
|
exclude.write("/docs/other/\n")
|
|
self.assertEqual([], DOCS_CHECK.validate_repository(self.root))
|
|
|
|
def test_skips_markdown_ignored_through_gitignore(self) -> None:
|
|
self.git("init", "--quiet")
|
|
self.write(".gitignore", "/docs/other/\n")
|
|
self.assertEqual([], DOCS_CHECK.validate_repository(self.root))
|
|
|
|
def test_reports_untracked_markdown_that_is_not_ignored(self) -> None:
|
|
self.git("init", "--quiet")
|
|
errors = self.local_errors()
|
|
self.assertTrue(any("broken local link" in error for error in errors), errors)
|
|
self.assertTrue(any("README is below a top-level directory" in error for error in errors), errors)
|
|
|
|
def test_reports_tracked_markdown_that_matches_ignore_rules(self) -> None:
|
|
self.git("init", "--quiet")
|
|
self.write(".gitignore", "/docs/other/\n")
|
|
self.git("add", "--force", self.LOCAL_README)
|
|
errors = self.local_errors()
|
|
self.assertTrue(any("broken local link" in error for error in errors), errors)
|
|
|
|
def test_reports_markdown_outside_git_work_tree(self) -> None:
|
|
self.write(".gitignore", "/docs/other/\n")
|
|
errors = self.local_errors()
|
|
self.assertTrue(any("broken local link" in error for error in errors), errors)
|
|
self.assertTrue(any("README is below a top-level directory" in error for error in errors), errors)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|