From 4af390e763395f58f336107a6d3322da1a5d3f5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Mon, 6 Jan 2025 22:04:18 +0000 Subject: [PATCH] Allow and validate `schema_version` key in v1 recipes (#2207) * 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 --- conda_smithy/lint_recipe.py | 8 +++++++ conda_smithy/linter/conda_recipe_v1_linter.py | 2 ++ news/schema_version.rst | 23 +++++++++++++++++++ tests/recipes/v1_recipes/torchvision.yaml | 2 ++ tests/test_lint_recipe.py | 17 ++++++++++++++ 5 files changed, 52 insertions(+) create mode 100644 news/schema_version.rst diff --git a/conda_smithy/lint_recipe.py b/conda_smithy/lint_recipe.py index 8d633fa6d..2395b97a5 100644 --- a/conda_smithy/lint_recipe.py +++ b/conda_smithy/lint_recipe.py @@ -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( diff --git a/conda_smithy/linter/conda_recipe_v1_linter.py b/conda_smithy/linter/conda_recipe_v1_linter.py index 1a5792e2a..27746227c 100644 --- a/conda_smithy/linter/conda_recipe_v1_linter.py +++ b/conda_smithy/linter/conda_recipe_v1_linter.py @@ -15,6 +15,7 @@ REQUIREMENTS_ORDER = ["build", "host", "run"] EXPECTED_SINGLE_OUTPUT_SECTION_ORDER = [ + "schema_version", "context", "package", "source", @@ -26,6 +27,7 @@ ] EXPECTED_MULTIPLE_OUTPUT_SECTION_ORDER = [ + "schema_version", "context", "recipe", "source", diff --git a/news/schema_version.rst b/news/schema_version.rst new file mode 100644 index 000000000..cebb8be36 --- /dev/null +++ b/news/schema_version.rst @@ -0,0 +1,23 @@ +**Added:** + +* Added validation for ``schema_version`` key. (#2207) + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* ``schema_version`` key no longer causes linting failures. (#2207) + +**Security:** + +* diff --git a/tests/recipes/v1_recipes/torchvision.yaml b/tests/recipes/v1_recipes/torchvision.yaml index 0f943464b..b9dc9a97d 100644 --- a/tests/recipes/v1_recipes/torchvision.yaml +++ b/tests/recipes/v1_recipes/torchvision.yaml @@ -1,3 +1,5 @@ +schema_version: 1 + context: version: 0.20.1 build_number: 2 diff --git a/tests/test_lint_recipe.py b/tests/test_lint_recipe.py index 33d93e745..06bdfe550 100644 --- a/tests/test_lint_recipe.py +++ b/tests/test_lint_recipe.py @@ -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()