diff --git a/mkconcore.py b/mkconcore.py index a1f44ef..606d331 100644 --- a/mkconcore.py +++ b/mkconcore.py @@ -89,9 +89,10 @@ def safe_name(value, context, allow_path=False): pattern = r'[\x00-\x1F\x7F*?"<>|;&`$\'()]' else: # blocks path traversal (/, \, :) in addition to shell metacharacters - pattern = r'[\x00-\x1F\x7F\\/:*?"<>|;&`$\'()]' + pattern = r'[\x00-\x1F\x7F\\/:*?"<>|;&`$\'() ]' if re.search(pattern, value): - raise ValueError(f"Unsafe {context}: '{value}' contains illegal characters.") + hint = " Spaces are not allowed in names; use '_' instead." if " " in value and not allow_path else "" + raise ValueError(f"Unsafe {context}: '{value}' contains illegal characters.{hint}") return value def safe_relpath(value, context): diff --git a/tests/test_mkconcore_name_spaces.py b/tests/test_mkconcore_name_spaces.py new file mode 100644 index 0000000..286e24c --- /dev/null +++ b/tests/test_mkconcore_name_spaces.py @@ -0,0 +1,60 @@ +import subprocess +import sys +from pathlib import Path + +import pytest + +MKCONCORE = Path(__file__).resolve().parent.parent / "mkconcore.py" + +GRAPH = """ + + + {node}:a.py + plant:b.py + {edge} + back + + +""" + + +def _run(tmp_path, node, edge, outdir="out"): + 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(GRAPH.format(node=node, edge=edge)) + return subprocess.run( + [ + sys.executable, + str(MKCONCORE), + str(wf), + str(src), + str(tmp_path / outdir), + "posix", + ], + cwd=tmp_path, + capture_output=True, + text=True, + ) + + +@pytest.mark.parametrize( + "node,edge", [("my ctrl", "u"), ("ctrl", "my edge")], ids=["node", "edge"] +) +def test_spaces_in_names_are_rejected_with_a_hint(tmp_path, node, edge): + result = _run(tmp_path, node, edge) + assert result.returncode != 0 + assert "Spaces are not allowed in names" in result.stderr + result.stdout + + +def test_names_without_spaces_still_build(tmp_path): + assert _run(tmp_path, "my_ctrl", "u").returncode == 0 + + +def test_output_directory_with_spaces_still_works(tmp_path): + result = _run(tmp_path, "ctrl", "u", outdir="my study out") + assert result.returncode == 0 + assert (tmp_path / "my study out" / "build").exists()