Spaces:
Paused
Paused
File size: 9,051 Bytes
c0e9d84 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | """Cold-import graph test: proves the package inits are ``__getattr__``-free.
Every package that used to resolve names through a PEP 562 hook now binds
them eagerly. This test verifies that without needing Telegram credentials:
1. No ``__getattr__`` / ``_LAZY`` machinery remains (static AST check).
2. Every name the lazy hooks used to resolve is bound by an eager ``from``
import (static AST check).
3. The import graph is acyclic (subprocess cold import; only ``ImportError``
counts as failure — credential/network gates mean imports resolved).
"""
from __future__ import annotations
import ast
import subprocess
import sys
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1]
#: The five packages that carried a module-level ``__getattr__`` hook.
LAZY_PACKAGES = [
"pyUltroid",
"pyUltroid.ultroid",
"pyUltroid._core",
"pyUltroid._misc",
"pyUltroid.fns",
"pyUltroid.webserver",
]
#: package -> its ``__init__.py`` path.
INIT_PATHS = {
"pyUltroid": ROOT / "pyUltroid" / "__init__.py",
"pyUltroid.ultroid": ROOT / "pyUltroid" / "ultroid.py",
"pyUltroid._core": ROOT / "pyUltroid" / "_core" / "__init__.py",
"pyUltroid._misc": ROOT / "pyUltroid" / "_misc" / "__init__.py",
"pyUltroid.fns": ROOT / "pyUltroid" / "fns" / "__init__.py",
"pyUltroid.webserver": ROOT / "pyUltroid" / "webserver" / "__init__.py",
}
#: Names each lazy hook used to resolve, keyed by package.
EXPECTED_EXPORTS = {
"pyUltroid": [
"generate_error_card",
"generate_success_card",
"generate_now_playing",
"generate_queue_card",
"render_welcome",
"render_farewell",
"InlineResultBuilder",
"InlineResultCache",
"inline_cache",
"build_menu_results",
"build_menu_grid",
"InstantInline",
"InstantResult",
"InstantQuery",
"InstantInlineError",
"InlineUnavailable",
"instant_inline",
],
"pyUltroid.ultroid": [
"Ultroid",
"core",
],
"pyUltroid._core": [
"QueueManager",
"QueueTask",
"GID",
"FID",
"list_directory_files",
"InlineResultBuilder",
"InlineResultCache",
"build_menu_grid",
"build_menu_results",
"render_welcome",
"render_farewell",
"WebUploader",
"UploadResult",
"UploadError",
"get_uploader",
"upload_to",
"resolve_service",
"UPLOAD_SERVICES",
"MENU_SERVICES",
"SERVICE_LABELS",
"SERVICE_ALIASES",
"CatboxUploader",
"LitterboxUploader",
"FileIoUploader",
"NullStUploader",
"TransferShUploader",
"UguuUploader",
"FilebinUploader",
"SiaskyUploader",
"toggle",
"action",
"manifest",
"handle_manifest",
"ManifestRegistry",
"scan_module",
"install_missing",
"PLUGIN_MANIFEST_RESULT_BUDGET",
"get_help_chars",
"InstantInline",
"InstantResult",
"InstantQuery",
"InstantInlineError",
"InlineUnavailable",
"instant_inline",
],
"pyUltroid._misc": [
"Module",
"BoundModule",
"ModuleError",
"ModuleExists",
"ModuleMissing",
"ModuleMixin",
"HandlerTag",
"Listener",
"handler",
"is_handler",
"MissingVerb",
"ModuleRegistry",
"register_module",
"Misc",
"misc",
"SecretScrubber",
"DISCARDED",
"KEEP_SAFE",
],
"pyUltroid.fns": [
"AdminKit",
"Terminal",
"GDriveManager",
"Helper",
"InfoKit",
"MiscKit",
"ToolKit",
"Fns",
"fns",
],
"pyUltroid.webserver": [
"EnvFile",
"KeepAliveConfig",
"KeepAlivePinger",
"LogBroker",
"LogStreamHub",
"BrokerHandler",
"RedactionFilter",
"LoginManager",
"LoginOutcome",
"LoginSession",
"LoginSessionUnknown",
"SessionFactory",
"ConfigGate",
"SetupBootstrap",
"SseFrame",
"LoginErrorMapper",
"SetupApplication",
"TokenGateMiddleware",
"ThreadedServer",
"PortProbe",
"SetupState",
"MiniAppApplication",
"MiniAppServer",
"MiniAppStore",
"MiniAppState",
"MemoryStore",
"AUTHORITY",
"REDACTOR",
"TokenAuthority",
"SecretRedactor",
"RateLimiter",
"BOOTSTRAP",
"SUPERVISOR",
"create_app",
"create_miniapp",
"ensure_running",
"ensure_token",
"rotate_token",
"attach_broker",
"generate_setup_token",
"token_matches",
"extract_token",
"bind_available",
"serve_forever",
"make_server",
"ensure_config_or_setup",
"config_complete",
"server_host_port",
"spawn_bot_after_setup",
"start_keepalive_thread",
"keep_alive",
"make_state",
"read_env",
"write_env_atomic",
],
}
def _bound_names(package: str) -> set[str]:
"""Names bound by eager ``from`` imports plus assignments in ``__init__``."""
tree = ast.parse(INIT_PATHS[package].read_text())
bound: set[str] = set()
for node in ast.walk(tree):
if isinstance(node, ast.ImportFrom):
for alias in node.names:
bound.add(alias.asname or alias.name)
elif isinstance(node, ast.Assign):
for target in node.targets:
if isinstance(target, ast.Name):
bound.add(target.id)
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
bound.add(node.name)
if package == "pyUltroid":
# W9: the root delegates its startup to the core union; the alias
# surface is written by the runtime's ``expose()`` (surface dict +
# CREATIVE_EXPORTS). Scan those two sources too.
runtime = (ROOT / "pyUltroid" / "_core" / "runtime.py").read_text()
wiring = (ROOT / "pyUltroid" / "_core" / "_client_wiring.py").read_text()
for text in (runtime, wiring):
for name in EXPECTED_EXPORTS[package]:
if name in text:
bound.add(name)
return bound
@pytest.mark.parametrize("package", LAZY_PACKAGES)
def test_no_module_level_getattr(package: str):
"""The package init must not define ``__getattr__`` or lazy machinery."""
source = INIT_PATHS[package].read_text()
assert "def __getattr__" not in source, (
f"{package}.__init__ still defines __getattr__"
)
assert "_LAZY" not in source, f"{package}.__init__ still has a _LAZY dict"
@pytest.mark.parametrize("package", LAZY_PACKAGES)
def test_lazy_names_still_resolvable(package: str):
"""Every name the hook used to resolve is bound eagerly now."""
bound = _bound_names(package)
missing = [name for name in EXPECTED_EXPORTS[package] if name not in bound]
assert not missing, f"{package} is missing eager exports: {missing}"
def test_cold_import_in_bootstrap_order():
"""Import every leaf package from a cold interpreter, in bootstrap order.
Only ``ImportError`` counts as failure (a cycle). Credential, network,
session-validation and deliberate library-mode gates all mean the imports
themselves resolved, so they print success.
"""
program = "\n".join(
[
"import os, sys, traceback",
"os.environ.setdefault('API_ID', '1')",
"os.environ.setdefault('API_HASH', 'x')",
"os.environ.setdefault('SESSION', 'x')",
"os.environ.setdefault('BOT_TOKEN', 'x')",
"os.environ.setdefault('LOG_CHANNEL', '0')",
"sys.argv = ['pyUltroid']",
"import importlib",
"try:",
" for pkg in [",
" 'pyUltroid._core',",
" 'pyUltroid._misc',",
" 'pyUltroid.fns',",
" 'pyUltroid.webserver',",
" ]:",
" importlib.import_module(pkg)",
" print('cold-import-ok')",
"except ImportError:",
" traceback.print_exc()",
" print('cold-import-IMPORT-FAIL')",
"except (SystemExit, Exception) as e:",
" print(f'cold-import-ok-gated ({type(e).__name__})')",
]
)
result = subprocess.run(
[sys.executable, "-c", program],
capture_output=True,
text=True,
cwd=str(ROOT),
timeout=120,
)
assert "cold-import-IMPORT-FAIL" not in result.stdout, (
f"cold import has a cycle:\n{result.stdout}\n{result.stderr}"
)
assert "cold-import-ok" in result.stdout, (
f"cold import failed:\nstdout:\n{result.stdout}\nstderr:\n{result.stderr}"
)
|