summaryrefslogtreecommitdiff
path: root/spec/unit/mixin/template_spec.rb
diff options
context:
space:
mode:
authorAndrea Campi <andrea.campi@zephirworks.com>2012-11-18 23:24:49 +0100
committerBryan McLellan <btm@opscode.com>2012-12-14 11:10:28 -0800
commit3dd9365d285bd1bd316be6270e108a229595668a (patch)
tree7de2d64c7f8f30d5fe163af6ca686c569c8202db /spec/unit/mixin/template_spec.rb
parent5509897b9f0a536f6b3768f28f569d450ff53232 (diff)
downloadchef-3dd9365d285bd1bd316be6270e108a229595668a.tar.gz
[CHEF-3249] Refactor template name resolution out to a separate class for DRYness and readability. While here let's also extend the API to support local templates, as well as templates from other cookbooks.
Diffstat (limited to 'spec/unit/mixin/template_spec.rb')
-rw-r--r--spec/unit/mixin/template_spec.rb56
1 files changed, 48 insertions, 8 deletions
diff --git a/spec/unit/mixin/template_spec.rb b/spec/unit/mixin/template_spec.rb
index ebe46cbca4..aa1dc2f076 100644
--- a/spec/unit/mixin/template_spec.rb
+++ b/spec/unit/mixin/template_spec.rb
@@ -66,23 +66,63 @@ describe Chef::Mixin::Template, "render_template" do
@provider.current_resource = @current_resource
@access_controls = mock("access controls")
@provider.stub!(:access_controls).and_return(@access_controls)
+
+ @template_context = {}
+ @template_context[:node] = @node
+ @template_context[:template_finder] = Chef::Provider::TemplateFinder.new(@run_context, @resource.cookbook_name, @node)
end
it "should provide a render method" do
- _run_context = @run_context
- context = {}
- context[:node] = @node
- context[:partial_resolver] = lambda do |partial_name|
- @provider.instance_eval do
- cookbook = run_context.cookbook_collection[resource_cookbook]
- partial_location = cookbook.preferred_filename_on_disk_location(node, :templates, partial_name)
+ @provider.render_template("before {<%= render 'test.erb' %>} after", @template_context) do |tmp|
+ tmp.open.read.should == "before {We could be diving for pearls!\n} after"
+ end
+ end
+
+ it "should render local files" do
+ begin
+ tf = Tempfile.new("partial")
+ tf.puts "test"
+ tf.rewind
+
+ @provider.render_template("before {<%= render '#{tf.path}', :local => true %>} after", @template_context) do |tmp|
+ tmp.open.read.should == "before {test\n} after"
end
+ ensure
+ tf.close
end
+ end
+
+ it "should render partials from a different cookbook" do
+ @template_context[:template_finder] = Chef::Provider::TemplateFinder.new(@run_context, 'apache2', @node)
- @provider.render_template("before {<%= render 'test.erb' %>} after", context) do |tmp|
+ @provider.render_template("before {<%= render 'test.erb', :cookbook => 'openldap' %>} after", @template_context) do |tmp|
tmp.open.read.should == "before {We could be diving for pearls!\n} after"
end
end
+
+ it "should pass the node to partials" do
+ @node.normal[:slappiness] = "happiness"
+
+ @provider.render_template("before {<%= render 'openldap_stuff.conf.erb' %>} after", @template_context) do |tmp|
+ tmp.open.read.should == "before {slappiness is happiness} after"
+ end
+ end
+
+ it "should pass variables to partials" do
+ @provider.render_template("before {<%= render 'openldap_variable_stuff.conf.erb', :variables => {:secret => 'whatever' } %>} after", @template_context) do |tmp|
+ tmp.open.read.should == "before {super secret is whatever} after"
+ end
+ end
+
+ it "should pass nil for missing variables in partials" do
+ @provider.render_template("before {<%= render 'openldap_variable_stuff.conf.erb', :variables => {} %>} after", @template_context) do |tmp|
+ tmp.open.read.should == "before {super secret is } after"
+ end
+
+ @provider.render_template("before {<%= render 'openldap_variable_stuff.conf.erb' %>} after", @template_context) do |tmp|
+ tmp.open.read.should == "before {super secret is } after"
+ end
+ end
end
describe "when an exception is raised in the template" do