💫 Toolkit to help you get started with SDD or any other process!
fix(workflows): reject falsy non-mapping step.yml in step add (#4321)
Noor ul aind00176e2f6ec
diff --git a/src/specify_cli/workflows/_commands.py b/src/specify_cli/workflows/_commands.pyindex 716b6c8a1..f275bfe09 100644--- a/src/specify_cli/workflows/_commands.py+++ b/src/specify_cli/workflows/_commands.py@@ -3364,12 +3364,31 @@ def workflow_step_add(try:import yaml as _yaml- meta = _yaml.safe_load(step_yml_content.decode("utf-8")) or {}+ step_yml_text = step_yml_content.decode("utf-8")+ # ``safe_load`` returns None for BOTH an empty document and an+ # explicit null scalar (``null``, ``~``, ``NULL``), so it cannot+ # tell them apart on its own. ``compose`` yields no node only for+ # a genuinely empty document.+ node = _yaml.compose(step_yml_text)+ meta = _yaml.safe_load(step_yml_text)+ is_empty_document = node is None or (+ meta is None+ and isinstance(node, _yaml.nodes.ScalarNode)+ and node.value == ""+ and node.start_mark.index == node.end_mark.index+ )except Exception as exc:console.print(f"[red]Error:[/red] Invalid step.yml: {exc}")raise typer.Exit(1)- if not isinstance(meta, dict):+ # Do NOT coerce with ``or {}`` here: that also turns a FALSY non-mapping+ # (top-level ``[]``, ``false``, ``0``, ``''``, or an explicit ``null``)+ # into ``{}`` and silently bypasses this shape check, surfacing the+ # unrelated "missing 'step.type_key'" error below instead of the real+ # problem. Only a genuinely empty document defaults to ``{}``.+ if meta is None and is_empty_document:+ meta = {}+ elif not isinstance(meta, dict):console.print("[red]Error:[/red] step.yml must be a YAML mapping")raise typer.Exit(1)diff --git a/tests/test_workflows.py b/tests/test_workflows.pyindex 9b1c17881..980cc21a6 100644--- a/tests/test_workflows.py+++ b/tests/test_workflows.py@@ -10671,6 +10671,79 @@ class TestWorkflowStepAddCLI:project_dir / ".specify" / "workflows" / "steps" / "my-step").exists()+ @pytest.mark.parametrize(+ "step_yml_body", [b"[]", b"false", b"0", b"''", b"null", b"~", b"NULL"]+ )+ def test_add_rejects_falsy_non_mapping_step_yml(+ self, project_dir, monkeypatch, step_yml_body+ ):+ """A FALSY non-mapping step.yml document ([], false, 0, '') must be+ reported as "step.yml must be a YAML mapping", not silently coerced by+ ``or {}`` into {} and then misreported as the unrelated "missing+ 'step.type_key'" error — matching how a TRUTHY non-mapping document+ (e.g. a bare string) already reports the mapping-shape error. An+ explicit null scalar (null/~/NULL) parses to the same ``None`` as a+ genuinely empty document, so it must be distinguished (via+ ``yaml.compose``) and rejected too, rather than defaulting to {}."""+ from typer.testing import CliRunner+ from specify_cli import app+ from specify_cli.workflows.catalog import StepCatalog+ from specify_cli.authentication import http as auth_http++ monkeypatch.chdir(project_dir)+ monkeypatch.setattr(+ StepCatalog,+ "get_step_info",+ lambda self, step_id: {+ "id": step_id,+ "name": "Test Step",+ "url": "https://example.com/step.yml",+ "init_url": "https://example.com/__init__.py",+ "_install_allowed": True,+ },+ )++ class _FakeResponse:+ def __init__(self, url):+ self.url = url+ self.body = step_yml_body if url.endswith("step.yml") else b""+ self.offset = 0++ def __enter__(self):+ return self++ def __exit__(self, exc_type, exc, tb):+ return False++ def getheader(self, name):+ return None++ def geturl(self):+ return self.url++ def read(self, size=-1):+ if size < 0:+ size = len(self.body) - self.offset+ chunk = self.body[self.offset : self.offset + size]+ self.offset += len(chunk)+ return chunk++ monkeypatch.setattr(+ auth_http,+ "open_url",+ lambda url, timeout=30, redirect_validator=None: _FakeResponse(url),+ )++ result = CliRunner().invoke(+ app, ["workflow", "step", "add", "my-step"]+ )++ assert result.exit_code != 0+ assert "step.yml must be a YAML mapping" in result.output+ assert not (+ project_dir / ".specify" / "workflows" / "steps" / "my-step"+ ).exists()+@pytest.mark.parametrize(("catalog_fields", "expected"),[