Skip to content

Commit

Permalink
feat: add undefined dict wrapper and some testing (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored Jan 9, 2025
1 parent 4eb31fe commit e55c366
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/rattler_build_conda_compat/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ def load_yaml(content: str) -> Any: # noqa: ANN401
return yaml.load(f)


class UndefinedDictWrapper:
"""
A wrapper around a dictionary that returns False for missing keys.
"""

def __init__(self, data: dict[str, Any]) -> None:
self.data = data

def __getitem__(self, key: str) -> Any: # noqa: ANN401
if key not in self.data:
return False
return self.data[key]


def _eval_selector(
condition: str, namespace: dict[str, Any], *, allow_missing_selector: bool = False
) -> bool:
Expand All @@ -57,7 +71,7 @@ def _eval_selector(
cleaned_selector = selector.strip("(").rstrip(")")
namespace[cleaned_selector] = True

return eval(condition, namespace) # noqa: S307
return eval(condition, {}, UndefinedDictWrapper(namespace)) # type: ignore[arg-type] # noqa: S307


def _render_recipe(
Expand Down
33 changes: 27 additions & 6 deletions tests/__snapshots__/test_jinja.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@
version: 5.1.0

source:
url: https://github.com/jrouwe/JoltPhysics/archive/refs/tags/v5.1.0.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095
- url: https://github.com/jrouwe/JoltPhysics/archive/refs/tags/v5.1.0.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095
- if: win
then:
url: https://win.com/jolt-physics.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095

requirements:
build:
- cxx_compiler_stub
- c_stdlib_stub
- cmake
- ninja
host:
- compatible_pin cmake
- subpackage_pin ${{ jolt_physics }}

build:
number: 0
Expand Down Expand Up @@ -88,15 +95,22 @@
version: 5.1.0

source:
url: https://github.com/jrouwe/JoltPhysics/archive/refs/tags/v5.1.0.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095
- url: https://github.com/jrouwe/JoltPhysics/archive/refs/tags/v5.1.0.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095
- if: win
then:
url: https://win.com/jolt-physics.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095

requirements:
build:
- cxx_compiler_stub
- c_stdlib_stub
- cmake
- ninja
host:
- compatible_pin cmake
- subpackage_pin ${{ jolt_physics }}

build:
number: 0
Expand Down Expand Up @@ -151,15 +165,22 @@
version: 5.1.0

source:
url: https://github.com/jrouwe/JoltPhysics/archive/refs/tags/v5.1.0.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095
- url: https://github.com/jrouwe/JoltPhysics/archive/refs/tags/v5.1.0.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095
- if: win
then:
url: https://win.com/jolt-physics.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095

requirements:
build:
- cxx_compiler_stub
- c_stdlib_stub
- cmake
- ninja
host:
- compatible_pin cmake
- subpackage_pin ${{ jolt_physics }}

build:
number: 0
Expand Down
11 changes: 9 additions & 2 deletions tests/data/jolt-physics/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ package:
version: ${{ version }}

source:
url: https://github.com/jrouwe/JoltPhysics/archive/refs/tags/v${{ version }}.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095
- url: https://github.com/jrouwe/JoltPhysics/archive/refs/tags/v${{ version }}.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095
- if: win
then:
url: https://win.com/jolt-physics.zip
sha256: 10fcc863ae2b9d48c2f22d8b0204034820e57a55f858b7c388ac9579d8cf4095

requirements:
build:
- ${{ compiler("cxx") }}
- ${{ stdlib("c") }}
- cmake
- ninja
host:
- ${{ pin_compatible('cmake') }}
- ${{ pin_subpackage(jolt_physics) }}

build:
number: 0
Expand Down

0 comments on commit e55c366

Please sign in to comment.