|
| 1 | +import subprocess |
| 2 | +import sys |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from concore_cli.commands.validate import validate_workflow |
| 6 | + |
| 7 | +MKCONCORE = Path(__file__).resolve().parent.parent / "mkconcore.py" |
| 8 | + |
| 9 | +GRAPH = """<?xml version="1.0"?> |
| 10 | +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" |
| 11 | + xmlns:y="http://www.yworks.com/xml/graphml"> |
| 12 | + <graph id="G" edgedefault="directed"> |
| 13 | + <node id="n0"><data key="d0"><y:NodeLabel>{first}</y:NodeLabel></data></node> |
| 14 | + <node id="n1"><data key="d0"><y:NodeLabel>{second}</y:NodeLabel></data></node> |
| 15 | + <edge source="n0" target="n1"><data key="d1"><y:EdgeLabel>u</y:EdgeLabel></data></edge> |
| 16 | + <edge source="n1" target="n0"><data key="d1"><y:EdgeLabel>y</y:EdgeLabel></data></edge> |
| 17 | + </graph> |
| 18 | +</graphml> |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +class _Console: |
| 23 | + def __init__(self): |
| 24 | + self.lines = [] |
| 25 | + |
| 26 | + def print(self, *args, **kwargs): |
| 27 | + self.lines.append(" ".join(str(a) for a in args)) |
| 28 | + |
| 29 | + |
| 30 | +def _setup(tmp_path, first, second): |
| 31 | + src = tmp_path / "src" |
| 32 | + src.mkdir() |
| 33 | + for name in ("a.py", "b.py"): |
| 34 | + (src / name).write_text("print('x')\n") |
| 35 | + wf = tmp_path / "w.graphml" |
| 36 | + wf.write_text(GRAPH.format(first=first, second=second)) |
| 37 | + return wf, src |
| 38 | + |
| 39 | + |
| 40 | +def _run_mkconcore(tmp_path, wf, src): |
| 41 | + return subprocess.run( |
| 42 | + [ |
| 43 | + sys.executable, |
| 44 | + str(MKCONCORE), |
| 45 | + str(wf), |
| 46 | + str(src), |
| 47 | + str(tmp_path / "out"), |
| 48 | + "posix", |
| 49 | + ], |
| 50 | + cwd=tmp_path, |
| 51 | + capture_output=True, |
| 52 | + text=True, |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +def test_mkconcore_rejects_two_nodes_with_same_container_name(tmp_path): |
| 57 | + wf, src = _setup(tmp_path, "ctrl:a.py", "ctrl:b.py") |
| 58 | + result = _run_mkconcore(tmp_path, wf, src) |
| 59 | + assert result.returncode != 0 |
| 60 | + assert "Container name 'ctrl' is used by more than one node" in ( |
| 61 | + result.stdout + result.stderr |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +def test_mkconcore_accepts_distinct_container_names(tmp_path): |
| 66 | + wf, src = _setup(tmp_path, "ctrl:a.py", "plant:b.py") |
| 67 | + assert _run_mkconcore(tmp_path, wf, src).returncode == 0 |
| 68 | + |
| 69 | + |
| 70 | +def test_validate_reports_duplicate_container_name(tmp_path): |
| 71 | + wf, src = _setup(tmp_path, "ctrl:a.py", "ctrl:b.py") |
| 72 | + console = _Console() |
| 73 | + assert validate_workflow(str(wf), str(src), console) is False |
| 74 | + assert any( |
| 75 | + "Duplicate node container name: 'ctrl'" in line for line in console.lines |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +def test_validate_accepts_distinct_container_names(tmp_path): |
| 80 | + wf, src = _setup(tmp_path, "ctrl:a.py", "plant:b.py") |
| 81 | + assert validate_workflow(str(wf), str(src), _Console()) is True |
0 commit comments