test(hosts): render the host_agent fragments whether or not the plugin is enabled (#5122)
The unit app does not register plugin blueprints, so render_template could not find _host_vitals.html or _host_metrics.html (TemplateNotFound, CI run 8384). tests/_templates.render_plugin_template renders a plugin's template through the app's own Jinja environment and request context, with the plugin's template folder ahead of the app's loader. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+15
-1
@@ -1,6 +1,9 @@
|
||||
"""Where Steward's templates live, for the guards that read every one."""
|
||||
"""Where Steward's templates live, for the guards that read every one, and a
|
||||
way to render a plugin's template in a test."""
|
||||
import pathlib
|
||||
|
||||
from jinja2 import ChoiceLoader, FileSystemLoader
|
||||
|
||||
import steward
|
||||
|
||||
REPO = pathlib.Path(steward.__file__).parent.parent
|
||||
@@ -10,3 +13,14 @@ def template_files() -> list[pathlib.Path]:
|
||||
"""Every first-party .html template: core and each plugin's."""
|
||||
roots = [REPO / "steward" / "templates", *(REPO / "plugins").glob("*/templates")]
|
||||
return sorted(p for root in roots for p in root.rglob("*.html"))
|
||||
|
||||
|
||||
async def render_plugin_template(app, plugin: str, name: str, **ctx) -> str:
|
||||
"""Render plugins/<plugin>/templates/<name> through the app's own Jinja
|
||||
environment (its globals, filters and request context), whether or not the
|
||||
plugin is enabled in this app. Call inside app.test_request_context()."""
|
||||
env = app.jinja_env.overlay(loader=ChoiceLoader(
|
||||
[FileSystemLoader(str(REPO / "plugins" / plugin / "templates")), app.jinja_env.loader]))
|
||||
await app.update_template_context(ctx)
|
||||
template = env.get_template(name)
|
||||
return await template.render_async(**ctx) if env.is_async else template.render(**ctx)
|
||||
|
||||
Reference in New Issue
Block a user