Skip to content

Commit

Permalink
Allow and validate schema_version key in v1 recipes (#2207)
Browse files Browse the repository at this point in the history
* Accept `schema_version` key in v1 recipes

Allow the v1 recipes to contain a top-level `schema_version` key,
and expect it to be ordered first.  The key is defined in CEP 14:
https://conda.org/learn/ceps/cep-0014#schema-version

Fixes #2201

* Add `schema_version` validation

For the time being, just verify that `schema_version` is 1 when using
`recipe.yaml` format, and refuse to lint if it is not.  In the future,
the code will probably need to be adapted to change `recipe_version`
based on `schema_version`.

* Add a news entry

---------

Co-authored-by: Matthew R. Becker <[email protected]>
  • Loading branch information
mgorny and beckermr authored Jan 6, 2025
1 parent 9451ab2 commit 4af390e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions conda_smithy/lint_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ def lintify_meta_yaml(
recipe_name = "meta.yaml" if recipe_version == 0 else "recipe.yaml"
recipe_fname = os.path.join(recipe_dir or "", recipe_name)

if recipe_version == 1:
schema_version = meta.get("schema_version", 1)
if schema_version != 1:
lints.append(
f"Unsupported recipe.yaml schema version {schema_version}"
)
return lints, hints

sources_section = get_section(meta, "source", lints, recipe_version)
build_section = get_section(meta, "build", lints, recipe_version)
requirements_section = get_section(
Expand Down
2 changes: 2 additions & 0 deletions conda_smithy/linter/conda_recipe_v1_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
REQUIREMENTS_ORDER = ["build", "host", "run"]

EXPECTED_SINGLE_OUTPUT_SECTION_ORDER = [
"schema_version",
"context",
"package",
"source",
Expand All @@ -26,6 +27,7 @@
]

EXPECTED_MULTIPLE_OUTPUT_SECTION_ORDER = [
"schema_version",
"context",
"recipe",
"source",
Expand Down
23 changes: 23 additions & 0 deletions news/schema_version.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Added validation for ``schema_version`` key. (#2207)

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* ``schema_version`` key no longer causes linting failures. (#2207)

**Security:**

* <news item>
2 changes: 2 additions & 0 deletions tests/recipes/v1_recipes/torchvision.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
schema_version: 1

context:
version: 0.20.1
build_number: 2
Expand Down
17 changes: 17 additions & 0 deletions tests/test_lint_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3865,5 +3865,22 @@ def test_lint_recipe_parses_v1_duplicate_keys():
), hints


def test_lint_recipe_v1_invalid_schema_version():
with tempfile.TemporaryDirectory() as tmpdir:
with open(os.path.join(tmpdir, "recipe.yaml"), "w") as f:
f.write(
textwrap.dedent(
"""
schema_version: 2
package:
name: blah
"""
)
)
lints, hints = linter.main(tmpdir, return_hints=True, conda_forge=True)
assert lints == ["Unsupported recipe.yaml schema version 2"]


if __name__ == "__main__":
unittest.main()

0 comments on commit 4af390e

Please sign in to comment.