test: render plugin templates through render_template with the plugin's folder in front (#5122)
Calling app.update_template_context directly failed in CI (run 8386: "missing 1 required positional argument: 'context'"). Rather than reproduce Quart's render path, the helper puts the plugin's template folder ahead of the app's loader for the one render and calls quart.render_template, which supplies the request context as it does for every page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+12
-8
@@ -3,6 +3,7 @@ way to render a plugin's template in a test."""
|
|||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
from jinja2 import ChoiceLoader, FileSystemLoader
|
from jinja2 import ChoiceLoader, FileSystemLoader
|
||||||
|
from quart import render_template
|
||||||
|
|
||||||
import steward
|
import steward
|
||||||
|
|
||||||
@@ -16,11 +17,14 @@ def template_files() -> list[pathlib.Path]:
|
|||||||
|
|
||||||
|
|
||||||
async def render_plugin_template(app, plugin: str, name: str, **ctx) -> str:
|
async def render_plugin_template(app, plugin: str, name: str, **ctx) -> str:
|
||||||
"""Render plugins/<plugin>/templates/<name> through the app's own Jinja
|
"""Render plugins/<plugin>/templates/<name> with quart.render_template,
|
||||||
environment (its globals, filters and request context), whether or not the
|
whether or not the plugin is enabled in this app: the plugin's template
|
||||||
plugin is enabled in this app. Call inside app.test_request_context()."""
|
folder goes ahead of the app's loader for the one render. Call inside
|
||||||
env = app.jinja_env.overlay(loader=ChoiceLoader(
|
app.test_request_context()."""
|
||||||
[FileSystemLoader(str(REPO / "plugins" / plugin / "templates")), app.jinja_env.loader]))
|
loader = app.jinja_env.loader
|
||||||
await app.update_template_context(ctx)
|
app.jinja_env.loader = ChoiceLoader(
|
||||||
template = env.get_template(name)
|
[FileSystemLoader(str(REPO / "plugins" / plugin / "templates")), loader])
|
||||||
return await template.render_async(**ctx) if env.is_async else template.render(**ctx)
|
try:
|
||||||
|
return await render_template(name, **ctx)
|
||||||
|
finally:
|
||||||
|
app.jinja_env.loader = loader
|
||||||
|
|||||||
Reference in New Issue
Block a user