Skip to content

Commit

Permalink
Merge branch 'main' into extra_upp_namlist_option
Browse files Browse the repository at this point in the history
  • Loading branch information
christinaholtNOAA authored Aug 16, 2024
2 parents f9842b7 + dfdb466 commit 5f731e9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/uwtools/config/jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def render(
:param searchpath: Paths to search for extra templates.
:param values_needed: Just report variables needed to render the template?
:param dry_run: Run in dry-run mode?
:return: The rendered template, or None.
:return: The unrendered template if values_needed is True, the rendered template, or None.
"""
_report(locals())
values = _supplement_values(
Expand All @@ -180,11 +180,11 @@ def render(
undeclared_variables = template.undeclared_variables

# If a report of variables required to render the template was requested, make that report and
# then return.
# then return the unrendered template.

if values_needed:
_values_needed(undeclared_variables)
return None
return str(template)

# Render the template. If there are missing values, report them and return an error to the
# caller.
Expand Down
2 changes: 1 addition & 1 deletion src/uwtools/drivers/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def _validate(self, schema_file: Optional[Path] = None) -> None:
validate_internal(
schema_name=self.driver_name.replace("_", "-"), config=self._config_intermediate
)
validate_internal(schema_name=STR.platform, config=self.config_full)
validate_internal(schema_name=STR.platform, config=self._config_intermediate)

def _write_runscript(self, path: Path, envvars: Optional[dict[str, str]] = None) -> None:
"""
Expand Down
18 changes: 18 additions & 0 deletions src/uwtools/tests/api/test_template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pylint: disable=missing-function-docstring,redefined-outer-name

import logging
import os
from pathlib import Path
from unittest.mock import patch
Expand All @@ -8,6 +9,8 @@

from uwtools.api import template
from uwtools.exceptions import UWTemplateRenderError
from uwtools.logging import log
from uwtools.tests.support import logged


@fixture
Expand All @@ -25,6 +28,14 @@ def kwargs():
}


@fixture
def template_file(tmp_path):
path = tmp_path / "template.jinja2"
with open(path, "w", encoding="utf-8") as f:
f.write("roses are {{roses_color}}, violets are {{violets_color}}")
return path


def test_render(kwargs):
with patch.object(template, "_render") as _render:
template.render(**kwargs)
Expand All @@ -51,6 +62,13 @@ def test_render_to_str(kwargs):
render.assert_called_once_with(**{**kwargs, "output_file": Path(os.devnull)})


def test_render_values_needed(caplog, template_file):
log.setLevel(logging.INFO)
template.render(input_file=template_file, values_needed=True)
for var in ("roses_color", "violets_color"):
assert logged(caplog, f" {var}")


def test_translate():
kwargs: dict = {
"input_file": "path1",
Expand Down

0 comments on commit 5f731e9

Please sign in to comment.