modify:进程插件开发环境目录结构变更

This commit is contained in:
2026-08-25 17:03:35 +08:00
parent d8cb69bb6b
commit 741d7cda09
5 changed files with 188 additions and 104 deletions
+61 -19
View File
@@ -2,9 +2,10 @@
build_plugin 打包命令冒烟测试
验证进程插件打包产物的完整性与错误路径:
1. demo 插件打包成功,zip 内含代码/配置/最小根配置/依赖清单/可执行 start.sh
2. 插件代码目录缺失时报 FileNotFoundError
3. 插件 config.toml 缺失时报 FileNotFoundError
1. demo 插件打包成功,zip = 项目根直接压缩(代码/配置/最小根配置/依赖清单/可执行 start.sh)
2. 打包排除 __pycache__ / .venv / *.pyc 等非产物内容
3. 插件项目目录缺失时报 FileNotFoundError
4. 插件项目内配置缺失时报 FileNotFoundError
运行: uv run python test_build_plugin.py
"""
@@ -16,9 +17,10 @@ import zipfile
from pathlib import Path
SRC = Path(__file__).parent
sys.path.insert(0, str(SRC / "utils" / "cli" / "src"))
sys.path.insert(0, str(SRC.parent / "utils" / "cli" / "src"))
from chongming_cli import build_plugin as bp # noqa: E402
from chongming_cli import gen_plugin as gp # noqa: E402
failures = []
@@ -47,9 +49,9 @@ with zipfile.ZipFile(zip_path) as z:
check("包含 start.sh", "start.sh" in names)
check("不包含 __pycache__", not any("__pycache__" in n for n in names))
# 最小根配置:仅 [zenoh] 节,不含网关侧敏感配置
# 最小根配置:worker 启动需 [zenoh] 节,且不含网关侧敏感配置
root_conf = tomllib.loads(z.read("config.toml").decode())
check("根配置仅含 [zenoh] 节", set(root_conf.keys()) == {"zenoh"}, str(sorted(root_conf)))
check("根配置含 [zenoh] 节", "zenoh" in root_conf, str(sorted(root_conf)))
check(
"根配置不泄露网关 [security] 密钥",
"secret_key" not in z.read("config.toml").decode(),
@@ -66,7 +68,7 @@ with zipfile.ZipFile(zip_path) as z:
)
check(
"pyproject 声明 chongming-worker git 源",
pyproject["tool"]["uv"]["sources"]["chongming-worker"]["git"] == bp.CHONGMING_WORKER_GIT,
pyproject["tool"]["uv"]["sources"]["chongming-worker"]["git"] == gp.CHONGMING_WORKER_GIT,
)
# start.sh:可执行 + 指向正确的插件 type
@@ -76,31 +78,71 @@ with zipfile.ZipFile(zip_path) as z:
check("start.sh 可执行(0o755)", perms == 0o755, oct(perms))
# ========== 2. 错误路径:插件代码目录缺失 ==========
# ========== 2. 打包排除非产物内容(临时最小项目) ==========
def make_mini_project(proj: Path) -> None:
"""构造一个结构合法的迷你插件项目(含需要排除的垃圾文件)。"""
(proj / "workers" / "mini" / "router").mkdir(parents=True)
(proj / "workers" / "mini" / "__init__.py").write_text("", encoding="utf-8")
(proj / "workers" / "mini" / "router" / "__init__.py").write_text("", encoding="utf-8")
conf = proj / "config" / "workers" / "mini"
conf.mkdir(parents=True)
(conf / "config.toml").write_text(
'[workers]\ntype = "mini"\nmodule = "workers.mini"\nload_mode = "external"\n',
encoding="utf-8",
)
(proj / "config" / "zenoh_config.json5").write_text("{}", encoding="utf-8")
(proj / "config.toml").write_text('[zenoh]\njson5_path = "config/zenoh_config.json5"\n', encoding="utf-8")
(proj / "pyproject.toml").write_text("[project]\nname = \"mini-worker\"\n", encoding="utf-8")
(proj / "start.sh").write_text("#!/usr/bin/env bash\necho mini\n", encoding="utf-8")
(proj / "start.sh").chmod(0o755)
# 垃圾内容:缓存目录、字节码、虚拟环境
(proj / "workers" / "mini" / "__pycache__" / "x.pyc").parent.mkdir(parents=True)
(proj / "workers" / "mini" / "__pycache__" / "x.pyc").write_bytes(b"x")
(proj / "workers" / "mini" / "y.pyc").write_bytes(b"y")
(proj / ".venv" / "bin" / "pip").parent.mkdir(parents=True)
(proj / ".venv" / "bin" / "pip").write_text("", encoding="utf-8")
orig_workers = bp.WORKERS_DIR
bp.WORKERS_DIR = tmp / "no_workers"
mini_proj = tmp / "workers" / "mini"
make_mini_project(mini_proj)
bp.WORKERS_DIR = tmp / "workers"
try:
bp.build_plugin_package("ghost", output_dir=tmp / "dist")
check("缺插件代码目录应抛 FileNotFoundError", False)
except FileNotFoundError:
check("缺插件代码目录应抛 FileNotFoundError", True)
zip_path = bp.build_plugin_package("mini", output_dir=tmp / "dist")
with zipfile.ZipFile(zip_path) as z:
names = set(z.namelist())
check("排除 __pycache__", not any("__pycache__" in n for n in names), str(sorted(names)))
check("排除 *.pyc", not any(n.endswith(".pyc") for n in names))
check("排除 .venv", not any(n.startswith(".venv") for n in names))
check("保留代码与配置", {"workers/mini/__init__.py", "config/workers/mini/config.toml"} <= names)
finally:
bp.WORKERS_DIR = orig_workers
# ========== 3. 错误路径:插件配置缺失 ==========
orig_conf_dir = bp.PLUGINS_CONFIG_DIR
bp.PLUGINS_CONFIG_DIR = tmp / "no_config"
# ========== 3. 错误路径:插件项目目录缺失 ==========
bp.WORKERS_DIR = tmp / "no_workers"
try:
bp.build_plugin_package("demo", output_dir=tmp / "dist")
bp.build_plugin_package("ghost", output_dir=tmp / "dist")
check("缺插件项目目录应抛 FileNotFoundError", False)
except FileNotFoundError:
check("缺插件项目目录应抛 FileNotFoundError", True)
finally:
bp.WORKERS_DIR = orig_workers
# ========== 4. 错误路径:项目内插件配置缺失 ==========
# 只建代码目录、不建 config/workers/<name>/config.toml
(orig_workers / "ghost").mkdir(parents=True)
try:
bp.build_plugin_package("ghost", output_dir=tmp / "dist")
check("缺插件配置应抛 FileNotFoundError", False)
except FileNotFoundError:
check("缺插件配置应抛 FileNotFoundError", True)
finally:
bp.PLUGINS_CONFIG_DIR = orig_conf_dir
shutil.rmtree(orig_workers / "ghost", ignore_errors=True)
# ========== 4. 错误路径:插件名不合法 ==========
# ========== 5. 错误路径:插件名不合法 ==========
try:
bp.build_plugin_package("Bad-Name", output_dir=tmp / "dist")
check("非法插件名应抛 ValueError", False)