CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 4m1s
CI / Check (pull_request) Successful in 16m30s
Prüfwerk reads CODESYS control logic only from the PLCopen XML export (decided: no parsing of the binary .project). Exporting by hand before every upload or commit is easy to forget, so customers get a CODESYS Script Engine script that does it, in the IDE or headless in CI: - docs/public/downloads/codesys-export-plcopen.py, served with the docs: exports every top-level object with its children into one PLCopen XML file (projects.open / projects.primary, get_children, export_xml), creates the target folder, and exits with status 1 when the project is missing or the export holds no POU. IronPython 2.7 and Python 3. - scripts/test_codesys_export.py runs it offline against a stand-in for the Script Engine's projects object; CI's Check job now runs the repository's offline script tests. - guide/plc: "Exporting PLCopen XML automatically", with both ways to run it; guide/scanning points there from the native-project note. Not yet run against a real CODESYS installation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
4.4 KiB
Python
125 lines
4.4 KiB
Python
"""Offline tests of docs/public/downloads/codesys-export-plcopen.py.
|
|
|
|
The script runs inside the CODESYS Script Engine, which provides the global
|
|
`projects`. Here it runs with a stand-in that records what the script asks of
|
|
it and writes a fake export, so the script's own logic is checked: arguments,
|
|
the default export path, opening and closing, and the POU check.
|
|
Run: python3 -I -m unittest discover -s scripts -p "test_*.py"
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
SCRIPT = (Path(__file__).resolve().parent.parent
|
|
/ "docs" / "public" / "downloads" / "codesys-export-plcopen.py")
|
|
|
|
|
|
class FakeProject:
|
|
"""A ScriptProject that writes `body` as its PLCopen XML export."""
|
|
|
|
def __init__(self, path: str, body: bytes) -> None:
|
|
self.path = path
|
|
self.body = body
|
|
self.calls: list[tuple[object, ...]] = []
|
|
|
|
def get_children(self, recursive: bool) -> list[str]:
|
|
self.calls.append(("get_children", recursive))
|
|
return ["Device", "Application"]
|
|
|
|
def export_xml(self, objects: list[str], path: str, recursive: bool) -> None:
|
|
self.calls.append(("export_xml", tuple(objects), path, recursive))
|
|
Path(path).write_bytes(self.body)
|
|
|
|
def close(self) -> None:
|
|
self.calls.append(("close",))
|
|
|
|
|
|
class FakeProjects:
|
|
"""The Script Engine's `projects`: one primary project, or one to open."""
|
|
|
|
def __init__(self, project: FakeProject, primary: bool) -> None:
|
|
self.project = project
|
|
self.primary = project if primary else None
|
|
self.opened: list[str] = []
|
|
|
|
def open(self, path: str) -> FakeProject:
|
|
self.opened.append(path)
|
|
return self.project
|
|
|
|
|
|
def run(argv: list[str], projects: FakeProjects | None) -> int:
|
|
"""Run the script with `argv`; its exit status (0 when it returns)."""
|
|
source = SCRIPT.read_text(encoding="utf-8")
|
|
scope: dict[str, object] = {"__name__": "__main__", "__file__": str(SCRIPT)}
|
|
if projects is not None:
|
|
scope["projects"] = projects
|
|
import sys
|
|
|
|
saved = sys.argv
|
|
sys.argv = argv
|
|
try:
|
|
exec(compile(source, str(SCRIPT), "exec"), scope)
|
|
except SystemExit as stop:
|
|
return int(stop.code or 0)
|
|
finally:
|
|
sys.argv = saved
|
|
return 0
|
|
|
|
|
|
POUS = b'<?xml version="1.0"?><project><pou name="A"/><pou name="B"/></project>'
|
|
|
|
|
|
class ExportTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.dir = tempfile.TemporaryDirectory()
|
|
self.root = Path(self.dir.name)
|
|
self.project_file = self.root / "Line.project"
|
|
self.project_file.write_bytes(b"\x00native")
|
|
|
|
def tearDown(self) -> None:
|
|
self.dir.cleanup()
|
|
|
|
def test_a_named_project_is_opened_exported_and_closed(self) -> None:
|
|
out = self.root / "export" / "Line.xml"
|
|
project = FakeProject(str(self.project_file), POUS)
|
|
engine = FakeProjects(project, primary=False)
|
|
|
|
status = run(["x", str(self.project_file), str(out)], engine)
|
|
|
|
self.assertEqual(status, 0)
|
|
self.assertEqual(engine.opened, [str(self.project_file)])
|
|
self.assertIn(("export_xml", ("Device", "Application"), str(out), True), project.calls)
|
|
self.assertEqual(project.calls[-1], ("close",))
|
|
self.assertTrue(out.is_file(), "the export folder is created")
|
|
|
|
def test_the_open_project_is_exported_next_to_itself_and_left_open(self) -> None:
|
|
project = FakeProject(str(self.project_file), POUS)
|
|
|
|
status = run(["x"], FakeProjects(project, primary=True))
|
|
|
|
self.assertEqual(status, 0)
|
|
self.assertTrue((self.root / "Line.plcopen.xml").is_file())
|
|
self.assertNotIn(("close",), project.calls)
|
|
|
|
def test_an_export_without_a_pou_fails(self) -> None:
|
|
project = FakeProject(str(self.project_file), b"<project/>")
|
|
status = run(["x", str(self.project_file)], FakeProjects(project, primary=False))
|
|
self.assertEqual(status, 1)
|
|
|
|
def test_a_missing_project_or_none_open_fails(self) -> None:
|
|
project = FakeProject(str(self.project_file), POUS)
|
|
missing = os.path.join(self.dir.name, "Missing.project")
|
|
self.assertEqual(run(["x", missing], FakeProjects(project, primary=False)), 1)
|
|
self.assertEqual(run(["x"], FakeProjects(project, primary=False)), 1)
|
|
|
|
def test_outside_codesys_it_says_so(self) -> None:
|
|
self.assertEqual(run(["x"], None), 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|