Skip to content

Commit

Permalink
Merge pull request #2923 from regro/jinja2-set-sel-bug
Browse files Browse the repository at this point in the history
fix: make sure to put selectors in for all jinja2 vars
  • Loading branch information
beckermr authored Aug 9, 2024
2 parents 4b7ef4d + 24214a0 commit fe8218e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion conda_forge_tick/recipe_parser/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
# this regex matches any line with a selector
SELECTOR_RE = re.compile(r"^.*#\s*\[(.*)\]")

# this regex matches a lint that only has a selector on it
ONLY_SELECTOR_RE = re.compile(r"^\s*#\s*\[(.*)\]")

# this one matches bad yaml syntax with a selector on a multiline string start
BAD_MULTILINE_STRING_WITH_SELECTOR = re.compile(r"[^|#]*\|\s+#")

Expand All @@ -48,6 +51,10 @@ def _get_yaml_parser():
return parser


def _line_is_only_selector(line):
return ONLY_SELECTOR_RE.match(line) is not None


def _config_has_key_with_selectors(cfg: dict, key: str):
for _key in cfg:
if _key == key or _key.startswith(key + CONDA_SELECTOR):
Expand Down Expand Up @@ -96,7 +103,10 @@ def _parse_jinja2_variables(meta_yaml: str) -> dict:
n.node,
jinja2.nodes.Const,
):
if _config_has_key_with_selectors(jinja2_vals, n.target.name):
if _config_has_key_with_selectors(jinja2_vals, n.target.name) or (
(i < len(all_nodes) - 1)
and _line_is_only_selector(all_nodes[i + 1].nodes[0].data.strip())
):
# selectors!

# this block runs if we see the key for the
Expand Down
4 changes: 4 additions & 0 deletions tests/test_recipe_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

def test_parsing_ml_jinja2():
meta_yaml = """\
{% set namesel = 'val1' %} # [py2k]
{% set name = 'val1' %} # [py2k]
{% set name = 'val2' %}#[py3k and win]
{% set version = '4.5.6' %}
Expand Down Expand Up @@ -77,6 +78,7 @@ def test_parsing_ml_jinja2():
"""

meta_yaml_canonical = """\
{% set namesel = "val1" %} # [py2k]
{% set name = "val1" %} # [py2k]
{% set name = "val2" %} # [py3k and win]
{% set version = "4.5.6" %}
Expand Down Expand Up @@ -140,6 +142,7 @@ def test_parsing_ml_jinja2():
cm = CondaMetaYAML(meta_yaml)

# check the jinja2 keys
assert cm.jinja2_vars["namesel__###conda-selector###__py2k"] == "val1"
assert cm.jinja2_vars["name__###conda-selector###__py2k"] == "val1"
assert cm.jinja2_vars["name__###conda-selector###__py3k and win"] == "val2"
assert cm.jinja2_vars["version"] == "4.5.6"
Expand Down Expand Up @@ -212,6 +215,7 @@ def test_parsing_ml_jinja2():
true_new_meta_yaml = """\
{% set foo = "bar" %}
{% set xfoo = 10 %} # [win or osx]
{% set namesel = "val1" %} # [py2k]
{% set name = "val1" %} # [py2k]
{% set name = "val2" %} # [py3k and win]
{% set version = "4.5.6" %}
Expand Down

0 comments on commit fe8218e

Please sign in to comment.