55 lines
2.2 KiB
Python
55 lines
2.2 KiB
Python
"""构建与资源验收:通过真实生成器和资源读取接口验证发布内容。"""
|
|
|
|
import json
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from muse import 资源加载
|
|
from muse.共享.错误 import 资源缺失错误
|
|
|
|
仓库根 = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
def test_资源核对遍历实际字节__a02003(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""非空资源包完整可读,篡改任一资源后核对失败。"""
|
|
工具 = tmp_path / "工具"
|
|
工具.mkdir()
|
|
shutil.copyfile(仓库根 / "工具/构建资源包.py", 工具 / "构建资源包.py")
|
|
(tmp_path / "pyproject.toml").write_text('[project]\nversion="0.0.0-test"\n')
|
|
(tmp_path / "正文方法.md").write_text("合成方法资源", encoding="utf-8")
|
|
(工具 / "资源登记.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"发布身份": "test-resource",
|
|
"资源": [{"源路径": "正文方法.md", "打包路径": "方法/正文.md"}],
|
|
},
|
|
ensure_ascii=False,
|
|
)
|
|
)
|
|
subprocess.run([sys.executable, str(工具 / "构建资源包.py")], check=True, capture_output=True)
|
|
资源目录 = tmp_path / "src/muse/资源"
|
|
monkeypatch.setattr(资源加载.resources, "files", lambda _: 资源目录)
|
|
assert 资源加载.核对资源()["发布身份"] == "test-resource"
|
|
with zipfile.ZipFile(资源目录 / "运行资源.zip", "w") as 包:
|
|
包.writestr("方法/正文.md", "被篡改的资源")
|
|
with pytest.raises(资源缺失错误, match="哈希"):
|
|
资源加载.核对资源()
|
|
|
|
|
|
def test_非空生成输入不能伪成功__a02004(tmp_path: Path) -> None:
|
|
"""未接入的生成器接到实际登记输入必须非零退出。"""
|
|
工具 = tmp_path / "工具"
|
|
工具.mkdir()
|
|
shutil.copyfile(仓库根 / "工具/生成接口.py", 工具 / "生成接口.py")
|
|
(工具 / "生成输入.json").write_text(json.dumps({"资源": [{"源路径": "缺失.py"}]}))
|
|
结果 = subprocess.run(
|
|
[sys.executable, str(工具 / "生成接口.py")], capture_output=True, text=True
|
|
)
|
|
assert 结果.returncode != 0
|
|
assert "未生成合同" in 结果.stderr
|