"""工程工具拒绝越界输入,并在写产物前完成整批检查。""" import json from pathlib import Path import pytest import 构建资源包 import 生成接口 import 维护索引 @pytest.mark.case_id("NC-O04-index-prunes-before-descent") def test_忽略目录在进入前剪枝(tmp_path, monkeypatch): for name in ["docs", ".venv/深层", "web/node_modules/深层"]: directory = tmp_path / name directory.mkdir(parents=True) (directory / "目录.md").write_text("synthetic") visited = [] original = 维护索引.os.walk def walk(*args, **kwargs): for entry in original(*args, **kwargs): visited.append(Path(entry[0]).relative_to(tmp_path).as_posix()) yield entry monkeypatch.setattr(维护索引.os, "walk", walk) assert list(维护索引.遍历文件(tmp_path, "目录.md")) == [tmp_path / "docs/目录.md"] assert not any(".venv" in p or "node_modules" in p for p in visited) @pytest.mark.case_id("NC-O04-openapi-preflight-before-factory") @pytest.mark.parametrize( "bad", ["../outside.json", "/absolute.json", "web"], ids=["parent", "absolute", "directory"] ) def test_整批输出拒绝发生在导入与写入前(tmp_path, monkeypatch, bad): (tmp_path / "web").mkdir() monkeypatch.setattr(生成接口, "仓库根", tmp_path) calls = [] monkeypatch.setattr(生成接口.importlib, "import_module", lambda name: calls.append(name)) items = [ {"格式": "OpenAPI", "工厂": "synthetic:factory", "输出": "good.json"}, {"格式": "OpenAPI", "工厂": "synthetic:factory", "输出": bad}, ] with pytest.raises(生成接口.生成错误): 生成接口.生成接口({"schema_version": 1, "资源": items}) assert calls == [] assert not (tmp_path / "good.json").exists() assert list((tmp_path / "web").iterdir()) == [] @pytest.mark.case_id("NC-O04-resource-collision-preserves-output") def test_迁移投影与直接资源重名在写前拒绝(tmp_path, monkeypatch): tools = tmp_path / "工具" tools.mkdir() migration = tmp_path / "数据库/迁移" migration.mkdir(parents=True) (migration / "V0001__synthetic.sql").write_text("SELECT 1;") resources = tmp_path / "src/muse/资源" resources.mkdir(parents=True) (resources / "清单.json").write_text("previous-release") entry = tools / "资源登记.json" entry.write_text( json.dumps( { "发布身份": "synthetic", "迁移目录": "数据库/迁移", "资源": [ { "源路径": "数据库/迁移/V0001__synthetic.sql", "打包路径": "迁移/V0001__synthetic.sql", } ], } ) ) monkeypatch.setattr(构建资源包, "仓库根", tmp_path) monkeypatch.setattr(构建资源包, "登记文件", entry) monkeypatch.setattr(构建资源包, "资源目录", resources) with pytest.raises(ValueError, match="重复"): 构建资源包.打包() assert (resources / "清单.json").read_text() == "previous-release" assert list(resources.iterdir()) == [resources / "清单.json"]