From 3ed54bfdf94a1ff485cf0ce430fd0ac784a41874 Mon Sep 17 00:00:00 2001 From: Michael Go Date: Mon, 4 Nov 2024 15:32:04 -0400 Subject: [PATCH] propagate Environment on new Context creation --- lib/liquid/context.rb | 3 ++- test/integration/context_test.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/liquid/context.rb b/lib/liquid/context.rb index 011235644..19daaeb73 100644 --- a/lib/liquid/context.rb +++ b/lib/liquid/context.rb @@ -19,7 +19,7 @@ class Context # rubocop:disable Metrics/ParameterLists def self.build(environment: Environment.default, environments: {}, outer_scope: {}, registers: {}, rethrow_errors: false, resource_limits: nil, static_environments: {}, &block) - new(environments, outer_scope, registers, rethrow_errors, resource_limits, static_environments, &block) + new(environments, outer_scope, registers, rethrow_errors, resource_limits, static_environments, environment, &block) end def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil, static_environments = {}, environment = Environment.default) @@ -143,6 +143,7 @@ def new_isolated_subcontext check_overflow self.class.build( + environment: @environment, resource_limits: resource_limits, static_environments: static_environments, registers: Registers.new(registers), diff --git a/test/integration/context_test.rb b/test/integration/context_test.rb index 03b33cf58..f6afaca52 100644 --- a/test/integration/context_test.rb +++ b/test/integration/context_test.rb @@ -672,6 +672,21 @@ def test_variable_to_liquid_returns_contextual_drop assert_includes(result, "unscoped_products_count: 5") end + def test_new_isolated_context_inherits_parent_environment + global_environment = Liquid::Environment.build(tags: {}) + context = Context.build(environment: global_environment) + + subcontext = context.new_isolated_subcontext + assert_equal(global_environment, subcontext.environment) + end + + def test_newly_built_context_inherits_parent_environment + global_environment = Liquid::Environment.build(tags: {}) + context = Context.build(environment: global_environment) + assert_equal(global_environment, context.environment) + assert(context.environment.tags.each.to_a.empty?) + end + private def assert_no_object_allocations