From 9731b1d53f9bdf9fead61aeaff012c8470533246 Mon Sep 17 00:00:00 2001 From: Dan Singerman Date: Wed, 11 Jan 2023 13:59:37 +0000 Subject: [PATCH 1/2] pass strict_variables and stricit_filters to render context --- lib/liquid/context.rb | 2 ++ test/integration/context_test.rb | 14 ++++++++++++++ test/integration/tags/render_tag_test.rb | 22 ++++++++++++++++++++++ test/test_helper.rb | 4 ++-- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/liquid/context.rb b/lib/liquid/context.rb index 36d3868b6..ceaaff702 100644 --- a/lib/liquid/context.rb +++ b/lib/liquid/context.rb @@ -153,6 +153,8 @@ def new_isolated_subcontext subcontext.errors = errors subcontext.warnings = warnings subcontext.disabled_tags = @disabled_tags + subcontext.strict_filters = strict_filters + subcontext.strict_variables = strict_variables end end diff --git a/test/integration/context_test.rb b/test/integration/context_test.rb index 698aaa427..75a276a07 100644 --- a/test/integration/context_test.rb +++ b/test/integration/context_test.rb @@ -551,6 +551,20 @@ def my_filter(*) assert_equal('my filter result', template.render(subcontext)) end + def test_new_isolated_subcontext_inherits_strict_filters + super_context = Context.new + super_context.strict_filters = true + subcontext = super_context.new_isolated_subcontext + assert_equal(true, subcontext.strict_filters) + end + + def test_new_isolated_subcontext_inherits_strict_variables + super_context = Context.new + super_context.strict_variables = true + subcontext = super_context.new_isolated_subcontext + assert_equal(true, subcontext.strict_variables) + end + def test_disables_tag_specified context = Context.new context.with_disabled_tags(%w(foo bar)) do diff --git a/test/integration/tags/render_tag_test.rb b/test/integration/tags/render_tag_test.rb index 28dbb0de0..122b682a0 100644 --- a/test/integration/tags/render_tag_test.rb +++ b/test/integration/tags/render_tag_test.rb @@ -206,4 +206,26 @@ def test_render_tag_with_drop 'loop' => "{{ value }}", }) end + + def test_render_tag_catches_strict_variables_errors + assert_raises(Liquid::UndefinedVariable) do + assert_template_result("123", + '{% render "partial" %}', + partials: { + 'partial' => "{{ undefined_variable }}", + }, + strict_variables: true) + end + end + + def test_render_tag_catches_strict_filters_errors + assert_raises(Liquid::UndefinedFilter) do + assert_template_result("123", + '{% render "partial" %}', + partials: { + 'partial' => "{{ '123' | undefined_filter }}", + }, + strict_filters: true) + end + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 67900df8f..3b51f3bfb 100755 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -39,13 +39,13 @@ module Assertions 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, strict_variables: nil, strict_filters: 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) context = Liquid::Context.build(static_environments: assigns, rethrow_errors: !render_errors, registers: registers) - output = template.render(context) + output = template.render(context, strict_variables: strict_variables, strict_filters: strict_filters) assert_equal(expected, output, message) end From 4af7e3db48460026079468f7e773a31d71a0b217 Mon Sep 17 00:00:00 2001 From: Dan Singerman Date: Wed, 11 Jan 2023 14:04:52 +0000 Subject: [PATCH 2/2] better test naming --- test/integration/tags/render_tag_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/tags/render_tag_test.rb b/test/integration/tags/render_tag_test.rb index 122b682a0..8d92a5489 100644 --- a/test/integration/tags/render_tag_test.rb +++ b/test/integration/tags/render_tag_test.rb @@ -207,7 +207,7 @@ def test_render_tag_with_drop }) end - def test_render_tag_catches_strict_variables_errors + def test_render_tag_raises_strict_variables_errors assert_raises(Liquid::UndefinedVariable) do assert_template_result("123", '{% render "partial" %}', @@ -218,7 +218,7 @@ def test_render_tag_catches_strict_variables_errors end end - def test_render_tag_catches_strict_filters_errors + def test_render_tag_raises_strict_filters_errors assert_raises(Liquid::UndefinedFilter) do assert_template_result("123", '{% render "partial" %}',