diff --git a/mkconcore.py b/mkconcore.py index a1f44ef..fb0ed37 100644 --- a/mkconcore.py +++ b/mkconcore.py @@ -339,7 +339,8 @@ def cleanup_script_files(): for node in nodes_text: try: - data = node.find('data', recursive=False) + # yEd can put the label in any child, not only the first one + data = next((d for d in node.find_all('data', recursive=False) if d.find('y:NodeLabel')), None) if data: node_label_tag = data.find('y:NodeLabel') if node_label_tag: @@ -359,8 +360,12 @@ def cleanup_script_files(): nodes_dict[node['id']] = node_label node_id_to_label_map[node['id']] = node_label.split(':')[0] + else: + logging.warning(f"Node '{node.get('id')}' has no label and was ignored") + else: + logging.warning(f"Node '{node.get('id')}' has no label and was ignored") except (IndexError, AttributeError): - logging.debug('A node with no valid properties encountered and ignored') + logging.warning(f"Node '{node.get('id')}' has no valid properties and was ignored") label_values = list(nodes_dict.values()) duplicates = {label for label in label_values if label_values.count(label) > 1} @@ -370,7 +375,7 @@ def cleanup_script_files(): for edge in edges_text: try: - data = edge.find('data', recursive=False) + data = next((d for d in edge.find_all('data', recursive=False) if d.find('y:EdgeLabel')), None) if data: edge_label_tag = data.find('y:EdgeLabel') if edge_label_tag: @@ -394,8 +399,12 @@ def cleanup_script_files(): ) sys.exit(1) edges_dict[edge_label][1].append(nodes_dict[edge['target']]) - except (IndexError, AttributeError, KeyError): - logging.debug('An edge with no valid properties or missing node encountered and ignored') + else: + logging.warning(f"Edge '{edge.get('source')}' -> '{edge.get('target')}' has no label and was ignored") + else: + logging.warning(f"Edge '{edge.get('source')}' -> '{edge.get('target')}' has no label and was ignored") + except (IndexError, AttributeError, KeyError) as e: + logging.warning(f"Edge '{edge.get('source')}' -> '{edge.get('target')}' refers to a missing node or has no valid properties ({e!r}) and was ignored") ############## Mark's Docker diff --git a/tests/test_mkconcore_skipped_elements.py b/tests/test_mkconcore_skipped_elements.py new file mode 100644 index 0000000..a0046fd --- /dev/null +++ b/tests/test_mkconcore_skipped_elements.py @@ -0,0 +1,84 @@ +import subprocess +import sys +from pathlib import Path + +MKCONCORE = Path(__file__).resolve().parent.parent / "mkconcore.py" + +NODE_A = 'a:a.py' +NODE_B = 'b:b.py' + + +def _build(tmp_path, body): + src = tmp_path / "src" + src.mkdir() + (src / "a.py").write_text("print('a')\n") + (src / "b.py").write_text("print('b')\n") + wf = tmp_path / "w.graphml" + wf.write_text( + '\n' + '' + f'{body}\n' + ) + result = subprocess.run( + [ + sys.executable, + str(MKCONCORE), + str(wf), + str(src), + str(tmp_path / "out"), + "posix", + ], + cwd=tmp_path, + capture_output=True, + text=True, + ) + return result, result.stdout + result.stderr + + +def test_edge_without_label_is_reported(tmp_path): + result, output = _build( + tmp_path, f'{NODE_A}{NODE_B}' + ) + assert result.returncode == 0 + assert "Edge 'n0' -> 'n1' has no label" in output + + +def test_edge_to_missing_node_is_reported(tmp_path): + edge = 'c' + result, output = _build(tmp_path, f"{NODE_A}{NODE_B}{edge}") + assert result.returncode == 0 + assert "Edge 'n0' -> 'nX'" in output + + +def test_node_without_label_is_reported(tmp_path): + result, output = _build(tmp_path, f'{NODE_A}{NODE_B}') + assert result.returncode == 0 + assert "Node 'n9' has no label" in output + + +def test_valid_graph_has_no_skip_warnings(tmp_path): + edge = 'c' + result, output = _build(tmp_path, f"{NODE_A}{NODE_B}{edge}") + assert result.returncode == 0 + assert "was ignored" not in output + + +def test_label_in_second_data_element_is_used(tmp_path): + """yEd can write an empty (e.g. a description) before the graphics one.""" + + def node(i, label): + return ( + f'' + f"{label}" + ) + + edge = ( + '' + "c" + ) + result, output = _build(tmp_path, node(0, "a:a.py") + node(1, "b:b.py") + edge) + assert result.returncode == 0 + assert "was ignored" not in output + generated = {p.name for p in (tmp_path / "out" / "src").glob("*.py")} + assert {"a.py", "b.py"} <= generated