diff --git a/conda_forge_tick/recipe_parser/_parser.py b/conda_forge_tick/recipe_parser/_parser.py index 140c1e65d..6f6aa5f68 100644 --- a/conda_forge_tick/recipe_parser/_parser.py +++ b/conda_forge_tick/recipe_parser/_parser.py @@ -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+#") @@ -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): @@ -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 diff --git a/tests/test_recipe_parser.py b/tests/test_recipe_parser.py index 102587948..a8254fca1 100644 --- a/tests/test_recipe_parser.py +++ b/tests/test_recipe_parser.py @@ -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' %} @@ -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" %} @@ -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" @@ -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" %}