Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for DelayedEvaluator/lazy templates #14

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/chefspec/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,36 @@ def content_from_template(chef_run, template)

if Chef::Mixin::Template.const_defined?(:TemplateContext) # Chef 11+
template_context = Chef::Mixin::Template::TemplateContext.new([])

# this is from lib/chef/provider/template/content.rb roughly
# Deal with any DelayedEvaluator values in the template variables.
visitor = lambda do |obj|
case obj
when Hash
# If this is an Attribute object, we need to change class otherwise
# we get the immutable behavior. This could probably be fixed by
# using Hash#transform_values once we only support Ruby 2.4.
obj_class = obj.is_a?(Chef::Node::ImmutableMash) ? Mash : obj.class
# Avoid mutating hashes in the resource in case we're changing anything.
obj.each_with_object(obj_class.new) do |(key, value), memo|
memo[key] = visitor.call(value)
end
when Array
# Avoid mutating arrays in the resource in case we're changing anything.
obj.map { |value| visitor.call(value) }
when Chef::DelayedEvaluator
resource.instance_eval(&obj)
else
obj
end
end

variables = visitor.call(template.variables)

template_context.update({
node: chef_run.node,
template_finder: template_finder(chef_run, cookbook_name),
}.merge(template.variables))
}.merge(variables))
if template.respond_to?(:helper_modules) # Chef 11.4+
template_context._extend_modules(template.helper_modules)
end
Expand Down
18 changes: 17 additions & 1 deletion spec/unit/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end

let(:chef_run) { double("chef_run", { node: "node" }) }
let(:resource) { double("resource", { cookbook: "cookbook", source: "source", variables: {} }) }
let(:resource) { double("resource", { cookbook: "cookbook", source: "source", variables: { x: "y", foo: Chef::DelayedEvaluator.new { "bar" } } }) }
subject { described_class.new(chef_run, resource) }

describe "#content" do
Expand Down Expand Up @@ -66,4 +66,20 @@
expect(subject.content).to eq("rendered template content")
end
end

describe "content_from_template with lazy/DelayedEvaluator" do
it "renders the template by extending modules for rendering paritals within the template" do
cookbook_collection = {}
cookbook_collection["cookbook"] = double("", { preferred_filename_on_disk_location: "/template/location" } )
allow(subject).to receive(:cookbook_collection).with("node").and_return(cookbook_collection)
allow(subject).to receive(:template_finder)

allow(resource).to receive(:helper_modules).and_return([Module.new])
allow(resource).to receive(:resource_name).and_return("template")

allow(IO).to receive(:binread).and_return('rendered template <%= @foo %> content')

expect(subject.content).to eq("rendered template bar content")
end
end
end