# -*- coding: utf-8 -*- """run_tests.py — 批量生成 tests/.html + tests/index.html 与 run-tests.ps1 互为镜像;Python 版无 PS5.1 BOM/编码问题。 用法:python run_tests.py 依赖:无(标准库)""" import os import re import json import sys import html ROOT = os.getcwd() LIB = os.path.join(ROOT, ".design_library/aurora-admin") TESTS_DIR = os.path.join(ROOT, "tests") TEMPLATE = os.path.join(TESTS_DIR, "_template.html") with open(os.path.join(LIB, "components/index.json"), encoding="utf-8") as f: idx = json.load(f) CAT_MAP = { "general": ["button","buttongroup","tag","card"], "navigation": ["tabs","steps","breadcrumb","sidemenu","topmenu","mixednavigation","quicknav","anchornav","enhancedtabnav","dropdown"], "input": ["input","select","cascader","colorpicker","rangequickpicker","numberrangeinput","switchgroup","dragupload","signaturepad","codeinput","passwordinput","phoneinput","bankcardinput","idcardinput","plateinput","autocomplete","mention","transfer","rate","slider","listpicker","radiocard","checkboxcard","segmented"], "display": ["table","treetable","expandabletable","mergedcelltable","summaryrowtable","fixedcolumntable","cardlist","timelinelist","steplist","chartpanel","dashboardcard","calendar","carousel","imagepreview","qrcode","countdown","tree","collapse","watermark","employeecard"], "feedback": ["modal","confirmmodal","alertmodal","formmodal","fullscreenmodal","messagepro","notificationpro","progressvariants","skeletonpro","emptypro","loadingoverlay","resultvariants","exceptionvariants","popconfirm","bottomsheet"], "system": ["themeswitcher","layoutswitcher","densityswitcher","shortcutpanel","backtotop","pagetransition"] } SLUG_CAT = {s: k for k, vs in CAT_MAP.items() for s in vs} CAT_ZH = {"general": "通用", "navigation": "导航", "input": "数据录入", "display": "数据展示", "feedback": "反馈", "system": "工具与系统"} CAT_ORDER = ["general", "navigation", "input", "display", "feedback", "system"] def get_snapshot(prefix): fw = os.path.join(ROOT, "frameworks", prefix + ".html") if not os.path.isfile(fw): return '

\uff08\u65e0 H5 \u6f14\u793a\u9875\uff09

' with open(fw, encoding="utf-8") as f: raw = f.read() m = re.search(r"]*>(.*?)", raw, flags=re.S) if not m: return '

\uff08\u65e0 body \u5185\u5bb9\uff09

' body = m.group(1) body = re.sub(r"", "", body, flags=re.I) body = re.sub(r"", "", body, flags=re.I) body = re.sub(r'\son[a-z]+="[^"]*"', "", body, flags=re.I) if len(body) > 1200: body = body[:1200] + "..." return body def split_name(name): parts = name.split(" ") return parts[0], parts[-1] def main(): os.makedirs(TESTS_DIR, exist_ok=True) with open(TEMPLATE, encoding="utf-8") as f: template = f.read() count = 0 for c in idx["components"]: slug = c["slug"] name = c["name"] zh, en = split_name(name) cat = SLUG_CAT.get(slug, "general") batch = int(c["specBatch"]) prefix = c["frameworksPrefix"] demo = get_snapshot(prefix) html_doc = template html_doc = html_doc.replace("SLUG_PLACEHOLDER", slug) html_doc = html_doc.replace("SLUG_ZH_PLACEHOLDER", zh) placeholder_demo = '

\uff08\u751f\u6210\u5668\u5c06\u586b\u5145\uff09

' if placeholder_demo in html_doc: html_doc = html_doc.replace(placeholder_demo, demo) placeholder_info = 'slug\uff1aSLUG_PLACEHOLDER \u00b7 \u7c7b\u76ee\uff1ageneral \u00b7 \u89c4\u683c\u6279\u6b21\uff1a\u7b2c 1 \u6279' if placeholder_info in html_doc: html_doc = html_doc.replace(placeholder_info, f"slug\uff1a{slug} \u00b7 \u7c7b\u76ee\uff1a{cat} \u00b7 \u89c4\u683c\u6279\u6b21\uff1a\u7b2c {batch} \u6279") # \u79fb\u9664\u6a21\u677f\u5185\u7f6e\u7684\u811a\u672c\u5757\uff0c\u6539\u4e3a\u5916\u90e8 _runtime.js # \u6a21\u677f\u4e2d\u7684\u811a\u672c\u5757\u4ece `/* \u6d4b\u8bd5\u9875\u901a\u7528 runtime */` \u5f00\u59cb\uff0c\u5230 `` \u4e3a\u6b62 script_marker_start = '', script_idx) if end_idx >= 0: # \u7528 __SLUG__ \u521d\u59cb\u5316 + \u5916\u90e8 _runtime.js \u811a\u672c\u4ee3\u66ff replacement = ( '\n' '' ) html_doc = html_doc[:script_idx] + replacement + html_doc[end_idx + len(''):] target = os.path.join(TESTS_DIR, slug + ".html") with open(target, "w", encoding="utf-8") as f: f.write(html_doc) count += 1 by_cat = {k: [] for k in CAT_ORDER} for c in idx["components"]: cat = SLUG_CAT.get(c["slug"], "general") by_cat.setdefault(cat, []).append(c) body = "" for cat in CAT_ORDER: items = by_cat.get(cat, []) if not items: continue zh_cat = CAT_ZH[cat] body += f'
\n

{zh_cat} {cat}

\n
\n' for c in items: slug = c["slug"] name = c["name"] zh2, en2 = split_name(name) body += f'
{html.escape(zh2)}
{html.escape(en2)}
\n' body += "
\n
\n" overview = ( '\n\n\n\n' '\n' '\u6d4b\u8bd5\u603b\u89c8 \u2014 Aurora Admin \u7ec4\u4ef6\u5e93\n' '\n' '\n' '\n\n\n
\n' '

\u6d4b\u8bd5\u603b\u89c8 \u2190 \u6587\u6863\u7ad9

\n' '

\u4e3a\u6bcf\u4e2a\u7ec4\u4ef6\u751f\u6210\u72ec\u7acb\u6d4b\u8bd5\u9875\uff08\u65ad\u8a00\u534f\u8bae + \u901a\u7528\u6d4b\u8bd5\u77e9\u9635\uff09\u3002\u70b9\u51fb\u5361\u7247\u8fdb\u5165\u6d4b\u8bd5\u9875\uff1b\u901a\u8fc7\u7387\u4f1a\u5199 localStorage[aa-test-log]\u3002

\n' '
\n' f' {count} \u4e2a\u7ec4\u4ef6\u6d4b\u8bd5\n' ' \u901a\u8fc7\uff1a\u2014\n' ' \u5931\u8d25\uff1a\u2014\n' '
\n' + body + '
\n\n\n\n\n' ) with open(os.path.join(TESTS_DIR, "index.html"), "w", encoding="utf-8") as f: f.write(overview) print("generated %d test pages" % count) print("generated tests/index.html") print("done.") if __name__ == "__main__": main()