diff --git a/src/rattler_build_conda_compat/render.py b/src/rattler_build_conda_compat/render.py index f58aac0..bb0bc26 100644 --- a/src/rattler_build_conda_compat/render.py +++ b/src/rattler_build_conda_compat/render.py @@ -83,7 +83,7 @@ def name(self) -> str: - ValueError: If the name is not lowercase or missing. """ - name = _get_recipe_metadata(self.meta, "name") + name = _get_recipe_metadata(self.meta, "name", rendered=self._rendered) if not name: raise ValueError(f"Error: package/name missing in: {self.meta_path!r}") @@ -105,7 +105,7 @@ def version(self) -> str: - CondaBuildUserError: If the `version` contains bad characters. - ValueError: If the version starts with a period or version is missing. """ - version = _get_recipe_metadata(self.meta, "version") + version = _get_recipe_metadata(self.meta, "version", rendered=self._rendered) if not version: raise ValueError(f"Error: package/version missing in: {self.meta_path!r}") @@ -255,9 +255,9 @@ def render_recipe( def render( - recipe_path, - config=None, - variants=None, + recipe_path: os.PathLike, + config: Optional[Config] = None, + variants: Optional[Dict[str, Any] | None] = None, **kwargs, ): """Given path to a recipe, return the MetaData object(s) representing that recipe, with jinja2 diff --git a/src/rattler_build_conda_compat/utils.py b/src/rattler_build_conda_compat/utils.py index 97f3170..6411106 100644 --- a/src/rattler_build_conda_compat/utils.py +++ b/src/rattler_build_conda_compat/utils.py @@ -177,11 +177,15 @@ def has_recipe(recipe_dir: Path) -> bool: _Metadata = Literal["name", "version"] -def _get_recipe_metadata(meta: dict[str, Any], field: _Metadata) -> str: +def _get_recipe_metadata(meta: dict[str, Any], field: _Metadata, rendered: bool = False) -> str: """ - Get recipe metadata ( name or version ). + Get recipe metadata ( name or version ) It will extract from recipe or package section, depending on the presence of multiple outputs. + If recipe was rendered by rattler-build, they are always present under recipe.package field. """ + if rendered: + return meta.get("recipe", {}).get("package", {}).get(field, "") + if "outputs" in meta: return meta.get("recipe", {}).get(field, "") else: diff --git a/tests/conftest.py b/tests/conftest.py index 7873347..0a27b81 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -72,6 +72,11 @@ def rich_recipe() -> Path: return Path("tests/data/rich_recipe.yaml") +@pytest.fixture() +def multiple_outputs() -> Path: + return Path("tests/data/multiple_outputs.yaml") + + @pytest.fixture() def feedstock_dir_with_recipe(tmpdir: Path) -> Path: feedstock_dir = tmpdir / "feedstock" diff --git a/tests/data/mamba_recipe.yaml b/tests/data/mamba_recipe.yaml index 27af2a9..a7eb853 100644 --- a/tests/data/mamba_recipe.yaml +++ b/tests/data/mamba_recipe.yaml @@ -41,7 +41,7 @@ outputs: run: - libsolv >=0.7.23 run_exports: - - ${{ pin_subpackage('libmamba', max_pin='x.x') }} + - ${{ pin_subpackage('libmamba', upper_bound='x.x') }} ignore_run_exports: by_name: - spdlog diff --git a/tests/data/multiple_outputs.yaml b/tests/data/multiple_outputs.yaml new file mode 100644 index 0000000..e3999ec --- /dev/null +++ b/tests/data/multiple_outputs.yaml @@ -0,0 +1,121 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/prefix-dev/recipe-format/main/schema.json + +context: + name: mamba + libmamba_version: "1.5.8" + libmambapy_version: "1.5.8" + mamba_version: "1.5.8" + release: "2024.03.25" + build_number: 2 + +recipe: + name: mamba-split + version: ${{ mamba_version }} + +source: + url: https://github.com/mamba-org/mamba/archive/refs/tags/${{ release }}.tar.gz + sha256: 6ddaf4b0758eb7ca1250f427bc40c2c3ede43257a60bac54e4320a4de66759a6 + +build: + number: ${{ build_number }} + +outputs: + - package: + name: libmamba + version: ${{ libmamba_version }} + build: + script: + - ${{ "build_mamba.sh" if unix }} + - ${{ "build_mamba.bat" if win }} + requirements: + build: + - ${{ compiler('cxx') }} + - cmake + - ninja + - ${{ "python" if win }} + host: + - libsolv >=0.7.23 + - libcurl >=8.4.0 + - fmt + - ${{ "winreg" if win }} + run: + - libsolv >=0.7.23 + run_exports: + - ${{ pin_subpackage('libmamba', upper_bound='x.x') }} + ignore_run_exports: + by_name: + - spdlog + - python + tests: + - script: + - if: unix + then: + - test -d ${PREFIX}/include/mamba # [unix] + else: + - if not exist %LIBRARY_PREFIX%\include\mamba\version.hpp (exit 1) # [win] + + - package: + name: libmambapy + version: ${{ libmambapy_version }} + build: + script: + - ${{ "build_mamba.sh" if unix }} + - ${{ "build_mamba.bat" if win }} + # string: py_sup${{ python | version_to_buildstring }}h${{ hash }}_${{ build_number }} + requirements: + build: + - ${{ compiler('cxx') }} + - cmake + - ninja + - if: build_platform != target_platform + then: + - python + - cross-python_${{ target_platform }} + - pybind11 + - pybind11-abi + host: + - python + - nlohmann_json + - ${{ pin_subpackage('libmamba', exact=True) }} + run: + - python + - ${{ pin_subpackage('libmamba', exact=True) }} + run_exports: + - ${{ pin_subpackage('libmambapy', upper_bound='x.x') }} + ignore_run_exports: + by_name: + - spdlog + + - package: + name: mamba + version: ${{ mamba_version }} + build: + script: + - ${{ "build_mamba.sh" if unix }} + - ${{ "build_mamba.bat" if win }} + - ${{ "test_mamba.bat" if target_platform == win }} + string: py${{ python | version_to_buildstring }}h${{ hash }}_${{ build_number }} + python: + entry_points: + - mamba = mamba.mamba:main + requirements: + build: + - if: build_platform != target_platform + then: + - python + - cross-python_${{ target_platform }} + run: + - python + - conda >=23.9,<24 + - ${{ pin_subpackage('libmambapy', exact=True) }} + + +about: + homepage: https://github.com/mamba-org/mamba + license: BSD-3-Clause + license_file: LICENSE + license_family: BSD + summary: A fast drop-in alternative to conda, using libsolv for dependency resolution + description: Mamba, the Fast Cross-Platform Package Manager + + repository: https://github.com/mamba-org/mamba diff --git a/tests/test_rattler_render.py b/tests/test_rattler_render.py index 7c130d2..5d32a4f 100644 --- a/tests/test_rattler_render.py +++ b/tests/test_rattler_render.py @@ -65,3 +65,27 @@ def test_metadata_for_multiple_output(feedstock_dir_with_recipe: Path, mamba_rec assert rattler_metadata.name() == "mamba-split" assert rattler_metadata.version() == "1.5.8" + + +def test_metadata_when_rendering_single_output( + feedstock_dir_with_recipe: Path, rich_recipe: Path +) -> None: + recipe_path = feedstock_dir_with_recipe / "recipe" / "recipe.yaml" + (recipe_path).write_text(rich_recipe.read_text(), encoding="utf8") + + rendered = render(str(recipe_path), platform="linux", arch="64") + + assert rendered[0][0].name() == "rich" + assert rendered[0][0].version() == "13.4.2" + + +def test_metadata_when_rendering_multiple_output( + feedstock_dir_with_recipe: Path, multiple_outputs: Path +) -> None: + recipe_path = feedstock_dir_with_recipe / "recipe" / "recipe.yaml" + (recipe_path).write_text(multiple_outputs.read_text(), encoding="utf8") + + rendered = render(str(recipe_path), platform="linux", arch="64") + + assert rendered[0][0].name() == "libmamba" + assert rendered[0][0].version() == "1.5.8"