# SPDX-License-Identifier: MIT """Scheme modules import the connector and nothing else of the board (docs/cartridge.md).""" from __future__ import annotations import ast from pathlib import Path PACKAGE = Path(__file__).resolve().parents[1] / "src" / "a2600_python" ALLOWED = { "connector", "atari", "detect", "dpc", "licensed", "supercharger", } # sibling scheme modules may share helpers def test_schemes_do_not_reach_into_the_board(): for path in (PACKAGE / "cartridge").glob("*.py"): if path.name == "__init__.py": continue tree = ast.parse(path.read_text(encoding="utf-8")) for node in ast.walk(tree): if isinstance(node, ast.ImportFrom): if node.level == 0: assert not (node.module or "").startswith("a2600_python"), path.name else: assert node.level == 1 and (node.module or "") in ALLOWED, ( f"{path.name} imports {'.' * node.level}{node.module}" ) elif isinstance(node, ast.Import): for alias in node.names: assert not alias.name.startswith("a2600_python"), path.name # Schemes allowed to look at ``write`` (connector.py): only where a document # says the cartridge reacts to writes alone, the mechanism tagged HYPOTHESIS. MAY_READ_WRITE = {"Tigervision3F", "ActivisionFE"} def test_only_documented_schemes_look_at_the_write_hint(): """There is no R/W pin: ``write`` is read only by the schemes listed above. ActivisionFE uses it only to take the bus byte of a write cycle as it does a read's (the CPU drives the byte it latches), not to decide whether to act. """ for path in (PACKAGE / "cartridge").glob("*.py"): tree = ast.parse(path.read_text(encoding="utf-8")) for cls in (n for n in ast.walk(tree) if isinstance(n, ast.ClassDef)): for fn in (n for n in cls.body if isinstance(n, ast.FunctionDef)): if fn.name != "access": continue names = [a.arg for a in fn.args.args] assert names == ["self", "address", "value", "t", "write"], (cls.name, names) used = any(isinstance(n, ast.Name) and n.id == "write" for n in ast.walk(fn)) assert not used or cls.name in MAY_READ_WRITE, cls.name