Skip to content

Commit

Permalink
misc: use doctest and type more
Browse files Browse the repository at this point in the history
  • Loading branch information
nichmor committed Jul 30, 2024
1 parent 6340ad2 commit 6718cba
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pytest = ">=8.2.2,<9"
syrupy = ">=4.6.1,<5"

[feature.tests.tasks]
tests = "pytest tests"
tests = "pytest --doctest-modules"
snapshot_update = "pytest --snapshot-update tests"

[feature.lint.dependencies]
Expand Down
33 changes: 18 additions & 15 deletions src/rattler_build_conda_compat/jinja.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any
from typing import Any, TypedDict

import jinja2
import yaml
Expand All @@ -9,7 +9,11 @@
from rattler_build_conda_compat.loader import load_yaml


class MissingUndefined(DebugUndefined):
class RecipeWithContext(TypedDict, total=False):
context: dict[str, str]


class _MissingUndefined(DebugUndefined):
def __str__(self) -> str:
"""
By default, `DebugUndefined` return values in the form `{{ value }}`.
Expand All @@ -29,7 +33,7 @@ def jinja_env() -> jinja2.Environment:
trim_blocks=True,
lstrip_blocks=True,
autoescape=True,
undefined=MissingUndefined,
undefined=_MissingUndefined,
)


Expand All @@ -48,21 +52,20 @@ def load_recipe_context(context: dict[str, str], jinja_env: jinja2.Environment)
return context


def eval_recipe_using_context(recipe_content: dict[str, Any]) -> dict[str, Any]:
def eval_recipe_using_context(recipe_content: RecipeWithContext) -> dict[str, Any]:
"""
Evaluate the recipe using known values from context section.
Unknown values are not evaluated and are kept as it is.
Example:
```yaml
context:
name: "my_value"
build:
string: ${{ name }}-${{ not_present_value }}
```
will be rendered as:
```yaml
build:
string: my_value-${{ not_present_value }}
Examples:
---
```python
>>> from pathlib import Path
>>> from rattler_build_conda_compat.loader import load_yaml
>>> recipe_content = load_yaml((Path().resolve() / "tests" / "data" / "eval_recipe_using_context.yaml").read_text())
>>> evaluated_context = eval_recipe_using_context(recipe_content)
>>> assert "my_value-${{ not_present_value }}" == evaluated_context["build"]["string"]
>>>
```
"""
env = jinja_env()
Expand Down
26 changes: 12 additions & 14 deletions tests/__snapshots__/test_jinja.ambr
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# serializer version: 1
# name: test_render_context
dict({
'build': dict({
'string': '${{ blas_variant }}${{ hash }}_foo-bla',
}),
'context': dict({
'name': 'foo',
'name_version': 'foo-bla',
'version': 'bla',
}),
'package': dict({
'name': 'foo',
'version': 'bla',
}),
})
'''
build:
string: ${{ blas_variant }}${{ hash }}_foo-bla
context:
name: foo
name_version: foo-bla
version: bla
package:
name: foo
version: bla

'''
# ---
4 changes: 4 additions & 0 deletions tests/data/eval_recipe_using_context.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
context:
name: "my_value"
build:
string: ${{ name }}-${{ not_present_value }}
5 changes: 4 additions & 1 deletion tests/test_jinja.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path

import yaml
from rattler_build_conda_compat.jinja import eval_recipe_using_context, load_yaml


Expand All @@ -9,4 +10,6 @@ def test_render_context(snapshot) -> None:
recipe_yaml = load_yaml(f)

rendered = eval_recipe_using_context(recipe_yaml)
assert rendered == snapshot
into_yaml = yaml.dump(rendered)

assert into_yaml == snapshot

0 comments on commit 6718cba

Please sign in to comment.