plcsharp/Tests/mcp_io_batch_smoke.py

50 lines
2.0 KiB
Python

"""Prepare or verify both IO renames in one portable-project editor session."""
import json
import shutil
import sys
from pathlib import Path
from mcp_smoke import Client
def prepare(sample, root):
shutil.copytree(sample, root)
hardware = root / "configuration/hardware.json"
config = json.loads(hardware.read_bytes())
for kind in ("DI", "DQ"):
config[kind][0]["Name"] = "夹具信号"
config["Cylinders"][0].update(InA="夹具信号", OutB="夹具信号")
hardware.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8")
caller, = (p for p in root.rglob("*.cs") if "ControllersModel.Axes" in p.read_text(encoding="utf-8"))
source = caller.read_text(encoding="utf-8")
lookups = "\n".join(
f' _ = model.ControllersModel.Controller.{kind}.FirstOrDefault(p => p.Name == {name});'
for kind in ("DI", "DQ") for name in ('"夹具信号"', 'flow.Step.ToString()'))
caller.write_bytes(source[:source.index(" _ =")].encode() + (lookups + "\n }\n}\n").encode())
def verify(exe, root, expected_source="editor"):
def snapshot():
return {p.relative_to(root): p.read_bytes() for p in root.rglob("*") if p.is_file()}
original = snapshot()
client = Client(exe, root, "--allow-write")
try:
assert client.call("get_project_summary")["source"] == expected_source
for kind in ("DI", "DQ"):
channel, = client.call("list_objects", kind=kind)["items"]
result = client.request("tools/call", {"name": "preview_object_rename", "arguments":
{"kind": kind, "id": channel["id"], "newName": "禁止改名"}})
assert result["isError"] and snapshot() == original
print("PASS: input and output names are protected.")
finally:
client.close()
if __name__ == "__main__":
mode, first, second = sys.argv[1:]
if mode == "prepare":
prepare(Path(first), Path(second))
else:
verify(Path(first), Path(second), {"verify": "editor", "verify-directory": "directory"}[mode])