set context's template_name with template's name

This commit is contained in:
Michael Go 2023-02-27 20:48:41 -04:00
parent 428c66ffac
commit 24dceef552
7 changed files with 63 additions and 57 deletions

View File

@ -16,12 +16,7 @@ module Liquid
template = template_factory.for(template_name)
partial = template.parse(source, parse_context)
partial.name ||= if context.registers[:file_system]&.respond_to?(:actual_template_name)
context.registers[:file_system].actual_template_name(template_name)
else
template_name
end
partial.name ||= template_name
cached_partials[template_name] = partial
ensure

View File

@ -72,9 +72,9 @@ module Liquid
old_partial = context.partial
begin
context.template_name = partial.name if partial.name
context.template_name = partial.name
context.partial = true
context.stack do
@attributes.each do |key, value|
context[key] = context.evaluate(value)

View File

@ -76,9 +76,7 @@ module Liquid
render_partial_func = ->(var, forloop) {
inner_context = context.new_isolated_subcontext
inner_context.template_name = partial.name if partial.name
inner_context.template_name = partial.name
inner_context.partial = true
inner_context['forloop'] = forloop if forloop

View File

@ -357,16 +357,24 @@ class IncludeTagTest < Minitest::Test
)
end
def test_include_tag_renders_actual_template_name_for_error
original_file_system = Liquid::Template.file_system
Liquid::Template.file_system = MemoryFileSystem.new(
'/some/path/snippets/foo.liquid' => "{{ foo.standard_error }}",
def test_render_tag_renders_error_with_template_name
assert_template_result(
'Liquid error (foo line 1): standard error',
"{% include 'foo' with errors %}",
{ 'errors' => ErrorDrop.new },
partials: { 'foo' => '{{ foo.standard_error }}' },
render_errors: true,
)
end
template = Liquid::Template.parse("{% include 'foo' with errors %}", line_numbers: true)
assert_equal('Liquid error (/some/path/snippets/foo line 1): standard error', template.render('errors' => ErrorDrop.new))
ensure
Liquid::Template.file_system = original_file_system
def test_render_tag_renders_error_with_template_name_from_template_factory
assert_template_result(
'Liquid error (some/path/foo line 1): standard error',
"{% include 'foo' with errors %}",
{ 'errors' => ErrorDrop.new },
partials: { 'foo' => '{{ foo.standard_error }}' },
template_factory: StubTemplateFactory.new,
render_errors: true,
)
end
end # IncludeTagTest

View File

@ -265,18 +265,24 @@ class RenderTagTest < Minitest::Test
)
end
def test_render_tag_renders_actual_template_name_for_error
original_file_system = Liquid::Template.file_system
context = Liquid::Context.new('errors' => ErrorDrop.new)
context.registers[:file_system] = MemoryFileSystem.new(
'/some/path/snippets/foo.liquid' => "{{ foo.standard_error }}",
def test_render_tag_renders_error_with_template_name
assert_template_result(
'Liquid error (foo line 1): standard error',
"{% render 'foo' with errors %}",
{ 'errors' => ErrorDrop.new },
partials: { 'foo' => '{{ foo.standard_error }}' },
render_errors: true,
)
end
template = Liquid::Template.parse("{% render 'foo' with errors %}", line_numbers: true)
assert_equal('Liquid error (/some/path/snippets/foo line 1): standard error', template.render(context))
ensure
Liquid::Template.file_system = original_file_system
def test_render_tag_renders_error_with_template_name_from_template_factory
assert_template_result(
'Liquid error (some/path/foo line 1): standard error',
"{% render 'foo' with errors %}",
{ 'errors' => ErrorDrop.new },
partials: { 'foo' => '{{ foo.standard_error }}' },
template_factory: StubTemplateFactory.new,
render_errors: true,
)
end
end

View File

@ -39,11 +39,12 @@ module Minitest
def assert_template_result(
expected, template, assigns = {},
message: nil, partials: nil, error_mode: nil, render_errors: false
message: nil, partials: nil, error_mode: nil, render_errors: false,
template_factory: nil
)
template = Liquid::Template.parse(template, line_numbers: true, error_mode: error_mode&.to_sym)
file_system = StubFileSystem.new(partials || {})
registers = Liquid::Registers.new(file_system: file_system)
registers = Liquid::Registers.new(file_system: file_system, template_factory: template_factory)
context = Liquid::Context.build(static_environments: assigns, rethrow_errors: !render_errors, registers: registers)
output = template.render(context)
assert_equal(expected, output, message)
@ -209,30 +210,10 @@ class StubTemplateFactory
@count = 0
end
def for(_template_name)
def for(template_name)
@count += 1
Liquid::Template.new
end
end
class MemoryFileSystem
def initialize(values)
# values is a hash of template_path => template_source
@snippets = {}
values.each do |file_path, source|
key = file_path.split('/').last.delete_suffix(".liquid")
@snippets[key] = {
source: source,
actual_template_name: file_path,
}
end
end
def read_template_file(template_name)
@snippets[template_name][:source]
end
def actual_template_name(template_name)
@snippets[template_name][:actual_template_name].delete_suffix(".liquid")
template = Liquid::Template.new
template.name = "some/path/" + template_name
template
end
end

View File

@ -156,4 +156,22 @@ class PartialCacheUnitTest < Minitest::Test
assert_equal(1, shared_file_system.file_read_count)
end
def test_uses_template_name_from_template_factory
template_factory = StubTemplateFactory.new
context = Liquid::Context.build(
registers: {
file_system: StubFileSystem.new('my_partial' => 'my partial body'),
template_factory: template_factory,
},
)
partial = Liquid::PartialCache.load(
'my_partial',
context: context,
parse_context: Liquid::ParseContext.new,
)
assert_equal('some/path/my_partial', partial.name)
end
end