Skip to content

Commit

Permalink
The asseturl filter should register a build dependency on the asset
Browse files Browse the repository at this point in the history
  • Loading branch information
dairiki committed Mar 30, 2022
1 parent 37be44d commit f4d54f9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lektor/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ def get_asset_url(asset):
ctx = get_ctx()
if ctx is None:
raise RuntimeError("No context found")
asset = site_proxy.get_asset(asset)
asset = ctx.pad.get_asset(asset)
if asset is None:
return Undefined("Asset not found")
info = ctx.build_state.get_file_info(asset.source_filename)
return "%s?h=%s" % (
ctx.source.url_to("!" + asset.url_path),
info.checksum[:8],
)
return ctx.get_asset_url(asset)


@LocalProxy
Expand Down Expand Up @@ -168,6 +164,18 @@ def url_to(self, path, alt=None, absolute=None, external=None):
rv = self.source.url_to(path, alt=alt, absolute=True)
return self.pad.make_url(rv, self.base_url, absolute, external)

def get_asset_url(self, asset):
"""Calculates the asset URL relative to the current record."""
if self.source is None:
raise RuntimeError(
"Can only generate paths to assets if "
"the context has a source document set."
)
asset_url = self.source.url_to("!" + asset.url_path)
info = self.build_state.get_file_info(asset.source_filename)
self.record_dependency(asset.source_filename)
return f"{asset_url}?h={info.checksum[:8]}"

def sub_artifact(self, *args, **kwargs):
"""Decorator version of :func:`add_sub_artifact`."""

Expand Down
27 changes: 27 additions & 0 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

from markers import imagemagick


Expand Down Expand Up @@ -271,3 +273,28 @@ def test_slug_contains_slash(pad, builder):
prog, _ = builder.build(record)
(artifact,) = prog.artifacts
assert artifact.artifact_name == "extra/long/path/index.html"


def test_asseturl_dependency_tracking_integration(
scratch_project_data, scratch_pad, scratch_builder
):
scratch_project_data.joinpath("templates/page.html").write_text(
"{{ '/asset.txt'|asseturl }}", "utf-8"
)
asset_txt = scratch_project_data / "assets/asset.txt"
asset_txt.parent.mkdir(exist_ok=True)
asset_txt.write_text("an asset\n", "utf-8")

prog, build_state = scratch_builder.build(scratch_pad.root)
assert len(build_state.updated_artifacts) == 1
output = Path(prog.artifacts[0].dst_filename)
asset_url = output.read_text(encoding="utf-8").rstrip()

prog, build_state = scratch_builder.build(scratch_pad.root)
assert len(build_state.updated_artifacts) == 0

asset_txt.write_text("updated asset\n", "utf-8")
prog, build_state = scratch_builder.build(scratch_pad.root)
updated_asset_url = output.read_text(encoding="utf-8").rstrip()
assert updated_asset_url != asset_url
assert len(build_state.updated_artifacts) == 1

0 comments on commit f4d54f9

Please sign in to comment.