Skip to content

Commit 2554a12

Browse files
committed
fix(workflows): flag only a namespace reference as a never-evaluated switch
The validator reused condition_is_never_evaluated, which flags every braceless string because a condition is coerced by bool(). A switch is not: it matches its resolved value against case keys, and a case key is a literal. `expression: review` dispatches the `review:` case and whitespace strips to the "" key, so both were being rejected while the step runs them correctly. switch_expression_is_never_evaluated flags braceless text only when it opens by walking into a root _build_namespace supplies (inputs.mode, item[0], context.run_id), and keeps the verbatim-unclosable {{ case. Tests pin both directions, with the literal cases checked against what execute() actually dispatches.
1 parent 925ca82 commit 2554a12

3 files changed

Lines changed: 93 additions & 5 deletions

File tree

‎src/specify_cli/workflows/expressions.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,30 @@ def condition_is_never_evaluated(condition: Any) -> bool:
857857
return _first_unclosable_block(stripped) == "verbatim"
858858

859859

860+
def switch_expression_is_never_evaluated(expression: Any) -> bool:
861+
"""True when a switch *expression* is a reference written without its braces.
862+
863+
``condition_is_never_evaluated`` cannot be reused here as it stands. It flags
864+
every braceless string, because a condition is coerced by ``bool()`` and any
865+
non-empty text is therefore always true. A switch instead matches its resolved
866+
value against case keys, and a case key is a literal: ``expression: review``
867+
resolves to ``"review"`` and dispatches the ``review:`` case, and whitespace
868+
strips to the ``""`` key. Both are valid, if constant, switches.
869+
870+
What is never evaluated is text plainly meant as an expression -- one that opens
871+
by walking into a root ``_build_namespace`` supplies, such as ``inputs.mode``.
872+
It is matched against the case keys as its own source text, so it falls through
873+
to ``default`` on every run. An opening ``{{`` the interpolator emits verbatim
874+
is flagged for the same reason, exactly as it is for a condition.
875+
"""
876+
if not isinstance(expression, str):
877+
return False
878+
stripped = expression.strip()
879+
if "{{" in stripped:
880+
return _first_unclosable_block(stripped) == "verbatim"
881+
return _NAMESPACE_REFERENCE.match(stripped) is not None
882+
883+
860884
def condition_is_interpolated_to_text(condition: Any) -> bool:
861885
"""True when *condition* holds ``{{ }}`` blocks but is spliced into text, not evaluated.
862886
@@ -1086,6 +1110,11 @@ def _has_incomplete_operand(text: str) -> bool:
10861110
# None, so a correction built on one turns a truthy condition false.
10871111
_NAMESPACE_ROOTS = ("inputs", "steps", "item", "fan_in", "context")
10881112

1113+
# Text that opens by walking into one of those roots: `inputs.mode`, `item[0]`.
1114+
_NAMESPACE_REFERENCE = re.compile(
1115+
r"(?:%s)(?:\.[\w-]|\[\d)" % "|".join(_NAMESPACE_ROOTS)
1116+
)
1117+
10891118
def _is_path_segment(segment: str) -> bool:
10901119
"""Whether _resolve_dot_path can walk *segment*: a name, or a name it indexes."""
10911120
return bool(_PLAIN_SEGMENT.match(segment) or _INDEXED_SEGMENT.match(segment))

‎src/specify_cli/workflows/steps/switch/__init__.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from specify_cli.workflows.base import StepBase, StepContext, StepResult, StepStatus
88
from specify_cli.workflows.expressions import (
99
condition_has_malformed_expression_block,
10-
condition_is_never_evaluated,
1110
evaluate_expression,
11+
switch_expression_is_never_evaluated,
1212
)
1313

1414

@@ -119,10 +119,13 @@ def validate(self, config: dict[str, Any]) -> list[str]:
119119
# `do-while` already reject that shape on their `condition`; this is the same
120120
# fault on the same evaluator, one step type over.
121121
#
122-
# Only these two checks apply. A switch matches on strings, so a composite key
123-
# such as `{{ inputs.a }}-{{ inputs.b }}` is legitimate here even though the
124-
# same shape would be a fault in a boolean condition.
125-
elif condition_is_never_evaluated(config["expression"]):
122+
# A switch matches on strings, which moves both boundaries a condition has. A
123+
# braceless literal is a valid case key -- `expression: review` dispatches the
124+
# `review:` case -- so only text that opens with a namespace reference is
125+
# flagged, not every string without braces. And a composite key such as
126+
# `{{ inputs.a }}-{{ inputs.b }}` is legitimate here even though the same
127+
# shape would be a fault in a boolean condition.
128+
elif switch_expression_is_never_evaluated(config["expression"]):
126129
errors.append(
127130
f"Switch step {config.get('id', '?')!r}: 'expression' "
128131
f"{config['expression']!r} has no usable '{{ }}' block, so it is "

‎tests/test_workflows.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,6 +3692,62 @@ def test_expression_without_a_block_is_rejected(self):
36923692
assert len(errors) == 1
36933693
assert "never evaluated" in errors[0]
36943694

3695+
def test_every_namespace_root_written_without_a_block_is_rejected(self):
3696+
"""Each root `_build_namespace` supplies, walked into without braces."""
3697+
from specify_cli.workflows.steps.switch import SwitchStep
3698+
3699+
cases = {"review": [{"id": "r", "type": "command", "command": "echo"}]}
3700+
for expression in (
3701+
"inputs.mode",
3702+
"steps.check.output.stdout",
3703+
"item.name",
3704+
"item[0]",
3705+
"fan_in.results",
3706+
"context.run_id",
3707+
"inputs.mode | default('review')",
3708+
"inputs.mode == 'review'",
3709+
" inputs.mode ",
3710+
):
3711+
config = {"id": "route", "expression": expression, "cases": cases}
3712+
errors = [
3713+
e for e in SwitchStep().validate(config) if "'expression'" in e
3714+
]
3715+
assert len(errors) == 1, expression
3716+
assert "never evaluated" in errors[0], expression
3717+
3718+
def test_a_literal_expression_stays_accepted(self):
3719+
"""A switch matches on strings, and a case key is a literal.
3720+
3721+
So a braceless literal is a valid -- if constant -- switch, not a fault:
3722+
`expression: review` dispatches the `review:` case, and whitespace strips to
3723+
the `""` key. Only text that walks into a namespace root is flagged.
3724+
"""
3725+
from specify_cli.workflows.steps.switch import SwitchStep
3726+
from specify_cli.workflows.base import StepContext, StepStatus
3727+
3728+
review = [{"id": "r", "type": "command", "command": "echo"}]
3729+
blank = [{"id": "b", "type": "command", "command": "echo"}]
3730+
cases = {"review": review, "": blank, "approve me": review, "inputs": review}
3731+
3732+
# Ground truth first: each of these really does dispatch a declared case.
3733+
for expression, matched in (
3734+
("review", "review"),
3735+
(" ", ""),
3736+
("approve me", "approve me"),
3737+
("inputs", "inputs"),
3738+
):
3739+
config = {"id": "route", "expression": expression, "cases": cases}
3740+
result = SwitchStep().execute(config, StepContext(inputs={}))
3741+
assert result.status == StepStatus.COMPLETED, expression
3742+
assert result.output["matched_case"] == matched, expression
3743+
assert [
3744+
e for e in SwitchStep().validate(config) if "'expression'" in e
3745+
] == [], expression
3746+
3747+
# A name that merely starts like a root is not a reference into it.
3748+
config = {"id": "route", "expression": "inputsX.mode", "cases": cases}
3749+
assert [e for e in SwitchStep().validate(config) if "'expression'" in e] == []
3750+
36953751
def test_expression_with_an_unclosable_block_is_rejected(self):
36963752
"""Different fault, different message: the block is evaluated, but truncated."""
36973753
from specify_cli.workflows.steps.switch import SwitchStep

0 commit comments

Comments
 (0)