From 24dceef552179f2efb0acefcfa3e03203614e7c5 Mon Sep 17 00:00:00 2001 From: Michael Go Date: Mon, 27 Feb 2023 20:48:41 -0400 Subject: [PATCH] set context's template_name with template's name --- lib/liquid/partial_cache.rb | 7 +---- lib/liquid/tags/include.rb | 4 +-- lib/liquid/tags/render.rb | 4 +-- test/integration/tags/include_tag_test.rb | 26 +++++++++++------- test/integration/tags/render_tag_test.rb | 28 +++++++++++-------- test/test_helper.rb | 33 +++++------------------ test/unit/partial_cache_unit_test.rb | 18 +++++++++++++ 7 files changed, 63 insertions(+), 57 deletions(-) diff --git a/lib/liquid/partial_cache.rb b/lib/liquid/partial_cache.rb index 7f94f76b..1ef52788 100644 --- a/lib/liquid/partial_cache.rb +++ b/lib/liquid/partial_cache.rb @@ -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 diff --git a/lib/liquid/tags/include.rb b/lib/liquid/tags/include.rb index 98f64fdb..7b9685ee 100644 --- a/lib/liquid/tags/include.rb +++ b/lib/liquid/tags/include.rb @@ -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) diff --git a/lib/liquid/tags/render.rb b/lib/liquid/tags/render.rb index 4f2dfafa..b9ae58ea 100644 --- a/lib/liquid/tags/render.rb +++ b/lib/liquid/tags/render.rb @@ -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 diff --git a/test/integration/tags/include_tag_test.rb b/test/integration/tags/include_tag_test.rb index 245acf0f..b86d40ee 100644 --- a/test/integration/tags/include_tag_test.rb +++ b/test/integration/tags/include_tag_test.rb @@ -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 diff --git a/test/integration/tags/render_tag_test.rb b/test/integration/tags/render_tag_test.rb index f24a5ef9..01485cfa 100644 --- a/test/integration/tags/render_tag_test.rb +++ b/test/integration/tags/render_tag_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 840b888a..34d85aeb 100755 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 diff --git a/test/unit/partial_cache_unit_test.rb b/test/unit/partial_cache_unit_test.rb index 6bf8c5cd..6a20efbb 100644 --- a/test/unit/partial_cache_unit_test.rb +++ b/test/unit/partial_cache_unit_test.rb @@ -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