Skip to content

Commit 4081dae

Browse files
Merge branch 'main' into fix_validate
2 parents eb8dc40 + 1719c6d commit 4081dae

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/uwtools/config/jinja2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def render(
170170
:param searchpath: Paths to search for extra templates.
171171
:param values_needed: Just report variables needed to render the template?
172172
:param dry_run: Run in dry-run mode?
173-
:return: The rendered template, or None.
173+
:return: The unrendered template if values_needed is True, the rendered template, or None.
174174
"""
175175
_report(locals())
176176
values = _supplement_values(
@@ -180,11 +180,11 @@ def render(
180180
undeclared_variables = template.undeclared_variables
181181

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

185185
if values_needed:
186186
_values_needed(undeclared_variables)
187-
return None
187+
return str(template)
188188

189189
# Render the template. If there are missing values, report them and return an error to the
190190
# caller.

src/uwtools/tests/api/test_template.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# pylint: disable=missing-function-docstring,redefined-outer-name
22

3+
import logging
34
import os
45
from pathlib import Path
56
from unittest.mock import patch
@@ -8,6 +9,8 @@
89

910
from uwtools.api import template
1011
from uwtools.exceptions import UWTemplateRenderError
12+
from uwtools.logging import log
13+
from uwtools.tests.support import logged
1114

1215

1316
@fixture
@@ -25,6 +28,14 @@ def kwargs():
2528
}
2629

2730

31+
@fixture
32+
def template_file(tmp_path):
33+
path = tmp_path / "template.jinja2"
34+
with open(path, "w", encoding="utf-8") as f:
35+
f.write("roses are {{roses_color}}, violets are {{violets_color}}")
36+
return path
37+
38+
2839
def test_render(kwargs):
2940
with patch.object(template, "_render") as _render:
3041
template.render(**kwargs)
@@ -51,6 +62,13 @@ def test_render_to_str(kwargs):
5162
render.assert_called_once_with(**{**kwargs, "output_file": Path(os.devnull)})
5263

5364

65+
def test_render_values_needed(caplog, template_file):
66+
log.setLevel(logging.INFO)
67+
template.render(input_file=template_file, values_needed=True)
68+
for var in ("roses_color", "violets_color"):
69+
assert logged(caplog, f" {var}")
70+
71+
5472
def test_translate():
5573
kwargs: dict = {
5674
"input_file": "path1",

0 commit comments

Comments
 (0)