|
1 | 1 | """A string condition with no ``{{ }}`` block is never evaluated (always true).""" |
2 | 2 |
|
3 | 3 | import pytest |
| 4 | +import yaml |
4 | 5 |
|
5 | 6 | from specify_cli.workflows.base import StepContext |
6 | 7 | from specify_cli.workflows.expressions import ( |
7 | 8 | condition_is_never_evaluated, |
8 | 9 | evaluate_condition, |
| 10 | + format_condition_correction, |
9 | 11 | ) |
10 | 12 | from specify_cli.workflows.steps.do_while import DoWhileStep |
11 | 13 | from specify_cli.workflows.steps.if_then import IfThenStep |
@@ -65,3 +67,84 @@ def test_validator_accepts_evaluated_and_literal_conditions(step_cls, condition) |
65 | 67 | ) |
66 | 68 | def test_condition_is_never_evaluated(value, expected): |
67 | 69 | assert condition_is_never_evaluated(value) is expected |
| 70 | + |
| 71 | + |
| 72 | +# --- An unterminated ``{{`` is the same defect, not a different one ----------- |
| 73 | +# |
| 74 | +# ``_interpolate_expressions`` substitutes nothing when no ``}}`` follows the |
| 75 | +# opening ``{{`` (its ``raw_close == -1`` branch appends the tail verbatim), so |
| 76 | +# ``{{ inputs.count > 100`` is returned unchanged and coerced to true exactly |
| 77 | +# like a brace-less string. |
| 78 | + |
| 79 | +BACKSLASH = chr(92) |
| 80 | + |
| 81 | +NEVER_EVALUATED = [ |
| 82 | + "inputs.count > 100", # no delimiter at all |
| 83 | + "{{ inputs.count > 100", # opened, never closed |
| 84 | + "}} inputs.count > 100 {{", # reversed: the only '{{' is last |
| 85 | +] |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize("condition", NEVER_EVALUATED) |
| 89 | +def test_incomplete_block_is_silently_true_and_is_flagged(condition): |
| 90 | + ctx = StepContext(inputs={"count": 5, "name": "abc"}) |
| 91 | + assert evaluate_condition(condition, ctx) is True |
| 92 | + assert condition_is_never_evaluated(condition) is True |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.parametrize( |
| 96 | + "condition", |
| 97 | + [ |
| 98 | + "{{ inputs.count > 100 }}", |
| 99 | + "{{ inputs.a }} and {{ inputs.b }}", |
| 100 | + "{{ inputs.text | default('}}') }}", # literal '}}' inside an argument |
| 101 | + ], |
| 102 | +) |
| 103 | +def test_complete_block_is_not_flagged(condition): |
| 104 | + assert condition_is_never_evaluated(condition) is False |
| 105 | + |
| 106 | + |
| 107 | +# --- The suggested correction has to survive a YAML round trip --------------- |
| 108 | + |
| 109 | +TRICKY_CONDITIONS = [ |
| 110 | + "inputs.count > 100", |
| 111 | + 'inputs.name == "zzz"', # double quote |
| 112 | + "inputs.name == 'zzz'", # single quote |
| 113 | + 'inputs.a == "x" and inputs.b == \'y\'', # both |
| 114 | + "inputs.path == 'C:" + BACKSLASH + "tmp'", # backslash |
| 115 | + 'inputs.path == "C:' + BACKSLASH + 'tmp"', # backslash + quote |
| 116 | + '{{ inputs.name == "zzz"', # incomplete + quote |
| 117 | + "}} inputs.count > 100 {{", |
| 118 | +] |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.parametrize("condition", TRICKY_CONDITIONS) |
| 122 | +def test_correction_is_valid_yaml_and_round_trips(condition): |
| 123 | + """A correction the author cannot paste into their workflow is no correction.""" |
| 124 | + loaded = yaml.safe_load("condition: " + format_condition_correction(condition)) |
| 125 | + stripped = condition.strip().lstrip("{}").rstrip("{}").strip() |
| 126 | + assert loaded["condition"] == "{{ " + stripped + " }}" |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.parametrize("condition", TRICKY_CONDITIONS) |
| 130 | +def test_correction_does_not_trip_the_validator_again(condition): |
| 131 | + loaded = yaml.safe_load("condition: " + format_condition_correction(condition)) |
| 132 | + assert condition_is_never_evaluated(loaded["condition"]) is False |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.parametrize("condition", ["{{ inputs.count > 100", "}} a > 1 {{"]) |
| 136 | +def test_correction_replaces_a_stray_delimiter_instead_of_nesting_one(condition): |
| 137 | + corrected = format_condition_correction(condition) |
| 138 | + assert "{{ {{" not in corrected and "}} }}" not in corrected |
| 139 | + assert corrected.count("{{") == 1 and corrected.count("}}") == 1 |
| 140 | + |
| 141 | + |
| 142 | +@pytest.mark.parametrize("step_cls", STEP_CLASSES) |
| 143 | +@pytest.mark.parametrize("condition", ['inputs.name == "zzz"', "{{ inputs.count > 100"]) |
| 144 | +def test_validator_correction_is_yaml_safe(step_cls, condition): |
| 145 | + config = {"id": "s1", "condition": condition, "then": [], "steps": []} |
| 146 | + errors = [e for e in step_cls().validate(config) if "never evaluated" in e] |
| 147 | + assert len(errors) == 1 |
| 148 | + suggested = errors[0].split("Wrap the expression: ", 1)[1].rstrip(".") |
| 149 | + loaded = yaml.safe_load("condition: " + suggested) |
| 150 | + assert condition_is_never_evaluated(loaded["condition"]) is False |
0 commit comments