Skip to content

Commit

Permalink
add more tests and functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Aug 5, 2024
1 parent dcbbee7 commit 806530e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/rattler_build_conda_compat/modify_recipe.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
from __future__ import annotations

import copy
import hashlib
import io
import re
import logging
from typing import TYPE_CHECKING, Any, Literal

import requests
from ruamel.yaml import YAML

from rattler_build_conda_compat.jinja.jinja import jinja_env, load_recipe_context
from rattler_build_conda_compat.recipe_sources import get_all_sources, Source
from rattler_build_conda_compat.recipe_sources import Source, get_all_sources

if TYPE_CHECKING:
from pathlib import Path

logger = logging.getLogger(__name__)

yaml = YAML()
yaml.preserve_quotes = True
yaml.width = 4096
yaml.indent(mapping=2, sequence=2, offset=0)


def _update_build_number_in_context(recipe: dict[str, Any], new_build_number: int) -> bool:
Expand Down Expand Up @@ -109,6 +115,7 @@ def update_hash(source: Source, url: str, hash_: Hash | None) -> None:
else:
# download and hash the file
hasher = hashlib.sha256()
logger.info("Retrieving and hashing %s", url)
with requests.get(url, stream=True, timeout=100) as r:
for chunk in r.iter_content(chunk_size=4096):
hasher.update(chunk)
Expand Down Expand Up @@ -142,8 +149,10 @@ def update_version(file: Path, new_version: str, hash_: Hash | None) -> str:

# set up the jinja context
env = jinja_env()
context = data.get("context", {})
context = copy.deepcopy(data.get("context", {}))
context_variables = load_recipe_context(context, env)
# for r-recipes we add the default `cran_mirror` variable
context_variables["cran_mirror"] = "https://cran.r-project.org"

for source in get_all_sources(data):
# render the whole URL and find the hash
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions tests/data/version/test_3/recipe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
context:
name: pytest-aio
version: 1.8.1

package:
name: ${{ name|lower }}
version: ${{ version }}

source:
url: https://pypi.io/packages/source/${{ name[0] }}/${{ name }}/${{ name.replace('-', '_') }}-${{ version }}.tar.gz
sha256: 97dcbc1c5ac991705f32bb2cf72f9ba94a8889fd0295d29ed4d7252b3e158684
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ package:

source:
url:
- ${{ cran_mirror }}/src/contrib/systemfit_{{ version }}.tar.gz
- ${{ cran_mirror }}/src/contrib/Archive/systemfit/systemfit_{{ version }}.tar.gz
- ${{ cran_mirror }}/src/contrib/systemfit_${{ version }}.tar.gz
- ${{ cran_mirror }}/src/contrib/Archive/systemfit/systemfit_${{ version }}.tar.gz
sha256: 5994fbb81f1678325862414f58328cdc2c46d47efa1f23218e9416a4da431ce2
14 changes: 14 additions & 0 deletions tests/data/version/test_4/recipe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
context:
version: "1.1-26"
posix: ${{ 'm2' if win else '' }}
native: ${{ 'm2w64' if win else '' }}

package:
name: r-systemfit
version: ${{ version|replace("-", "_") }}

source:
url:
- ${{ cran_mirror }}/src/contrib/systemfit_${{ version }}.tar.gz
- ${{ cran_mirror }}/src/contrib/Archive/systemfit/systemfit_${{ version }}.tar.gz
sha256: a99a59787dc5556afe9a1a153f2a3a8047aa7d357aab450101e20ab1f329f758
12 changes: 11 additions & 1 deletion tests/test_recipe_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ def test_build_number_mod(data_dir: Path) -> None:

def test_version_mod(data_dir: Path) -> None:
tests = data_dir / "version"
test_recipes = tests.glob("**/recipe.yaml")
test_recipes = [tests / "test_1/recipe.yaml", tests / "test_2/recipe.yaml"]
for recipe in test_recipes:
result = update_version(recipe, "0.25.0", None)
expected = recipe.parent / "expected.yaml"
assert result == expected.read_text()

test_python = tests / "test_3/recipe.yaml"
result = update_version(test_python, "1.9.0", None)
expected = test_python.parent / "expected.yaml"
assert result == expected.read_text()

test_cran = tests / "test_4/recipe.yaml"
result = update_version(test_cran, "1.1-30", None)
expected = test_cran.parent / "expected.yaml"
assert result == expected.read_text()

0 comments on commit 806530e

Please sign in to comment.