|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import ast |
| 4 | +from pathlib import Path |
| 5 | +from typing import Set |
| 6 | + |
| 7 | +import pytest |
| 8 | +import yaml |
| 9 | + |
| 10 | +root = Path(__file__).parent.parent.parent |
| 11 | + |
| 12 | +CONTROLLER_DIR = root / "shiny/playwright/controller" |
| 13 | +DOCS_CONFIG = root / "docs/_quartodoc-testing.yml" |
| 14 | +SKIP_PATTERNS = {"Base", "Container", "Label", "StyleM"} |
| 15 | +CONTROLLER_BASE_PATTERNS = { |
| 16 | + "Base", |
| 17 | + "Container", |
| 18 | + "Label", |
| 19 | + "StyleM", |
| 20 | + "WidthLocM", |
| 21 | + "InputActionButton", |
| 22 | + "UiBase", |
| 23 | + "UiWithLabel", |
| 24 | + "UiWithContainer", |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +def _is_valid_controller_class(node: ast.ClassDef) -> bool: |
| 29 | + class_name = node.name |
| 30 | + base_names = {ast.unparse(base) for base in node.bases} |
| 31 | + |
| 32 | + return ( |
| 33 | + not class_name.startswith("_") |
| 34 | + and not any(pattern in class_name for pattern in SKIP_PATTERNS) |
| 35 | + and not any(base.endswith("P") for base in base_names if isinstance(base, str)) |
| 36 | + and any( |
| 37 | + base.startswith("_") or any(p in base for p in CONTROLLER_BASE_PATTERNS) |
| 38 | + for base in base_names |
| 39 | + ) |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def get_controller_classes() -> Set[str]: |
| 44 | + classes: Set[str] = set() |
| 45 | + for py_file in CONTROLLER_DIR.glob("*.py"): |
| 46 | + if py_file.name == "__init__.py": |
| 47 | + continue |
| 48 | + try: |
| 49 | + tree = ast.parse(py_file.read_text(encoding="utf-8")) |
| 50 | + classes.update( |
| 51 | + node.name |
| 52 | + for node in ast.walk(tree) |
| 53 | + if isinstance(node, ast.ClassDef) and _is_valid_controller_class(node) |
| 54 | + ) |
| 55 | + except Exception as e: |
| 56 | + pytest.fail(f"Failed to parse {py_file}: {e}") |
| 57 | + return classes |
| 58 | + |
| 59 | + |
| 60 | +def get_documented_controllers() -> Set[str]: |
| 61 | + try: |
| 62 | + config = yaml.safe_load(DOCS_CONFIG.read_text(encoding="utf-8")) |
| 63 | + except Exception as e: |
| 64 | + pytest.fail(f"Failed to load or parse {DOCS_CONFIG}: {e}") |
| 65 | + |
| 66 | + return { |
| 67 | + content.split(".")[-1] |
| 68 | + for section in config.get("quartodoc", {}).get("sections", []) |
| 69 | + for content in section.get("contents", []) |
| 70 | + if isinstance(content, str) and content.startswith("playwright.controller.") |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +def test_all_controllers_are_documented(): |
| 75 | + controller_classes = get_controller_classes() |
| 76 | + documented_controllers = get_documented_controllers() |
| 77 | + |
| 78 | + missing_from_docs = controller_classes - documented_controllers |
| 79 | + extra_in_docs = documented_controllers - controller_classes |
| 80 | + |
| 81 | + error_messages: list[str] = [] |
| 82 | + if missing_from_docs: |
| 83 | + missing_list = "\n".join( |
| 84 | + sorted(f" - playwright.controller.{c}" for c in missing_from_docs) |
| 85 | + ) |
| 86 | + error_messages.append( |
| 87 | + f"Controllers missing from {DOCS_CONFIG}:\n{missing_list}" |
| 88 | + ) |
| 89 | + |
| 90 | + if extra_in_docs: |
| 91 | + extra_list = "\n".join( |
| 92 | + sorted(f" - playwright.controller.{c}" for c in extra_in_docs) |
| 93 | + ) |
| 94 | + error_messages.append(f"Extraneous classes in {DOCS_CONFIG}:\n{extra_list}") |
| 95 | + |
| 96 | + if error_messages: |
| 97 | + pytest.fail("\n\n".join(error_messages), pytrace=False) |
| 98 | + |
| 99 | + assert controller_classes, "No controller classes were found." |
| 100 | + assert documented_controllers, "No documented controllers were found." |
0 commit comments