plcsharp/Tests/mcp_device_batch_smoke.py

49 lines
1.9 KiB
Python

"""Verify three hardware renames and reverse-order undo in one live PLCSharp editor."""
import json
import shutil
import sys
from pathlib import Path
from mcp_smoke import Client
DEVICES = (("Cylinders", "夹具", "定位夹具"), ("Robots", "R1", "搬运机器人"),
("Connects", "PLC", "主站连接"))
def prepare(sample, root):
shutil.copytree(sample, root)
caller, = (p for p in root.rglob("*.cs") if "Controller.DI.FirstOrDefault" in p.read_text(encoding="utf-8"))
source = caller.read_text(encoding="utf-8")
lines = [
'model.CylindersModel.Cylinders.FirstOrDefault(c => c.Name == "夹具")',
'model.CylindersModel.Cylinders.FirstOrDefault(c => c.Name == flow.Step.ToString())',
'model.GetRobot("R1")', 'model.GetRobot(flow.Step.ToString())',
'model.GetConnect("PLC")', 'model.GetConnect(flow.Step.ToString())',
]
pos = source.rindex(" }")
caller.write_bytes((source[:pos] + "".join(f" _ = {line};\n" for line in lines) + source[pos:]).encode())
def verify(exe, root):
def snapshot():
return {p.relative_to(root): p.read_bytes() for p in root.rglob("*") if p.is_file()}
original = snapshot()
hw = Path("configuration/hardware.json")
client = Client(exe, root, "--allow-write")
try:
assert client.call("get_project_summary")["source"] == "editor"
for kind, old, new in DEVICES:
item, = (r for r in client.call("list_objects", kind=kind)["items"] if r["name"] == old)
result = client.request("tools/call", {"name": "preview_object_rename", "arguments":
{"kind": kind, "id": item["id"], "newName": new}})
assert result["isError"] and snapshot() == original
print("PASS: device names are protected.")
finally:
client.close()
if __name__ == "__main__":
{"verify": verify, "prepare": prepare}[sys.argv[1]](*map(Path, sys.argv[2:]))