"""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'' 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"") 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()