From edc1bbec8cb998c8ae03a884727a36bf6f5364a8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 17 Mar 2010 17:00:46 -0700 Subject: [PATCH] TemplateRenderer added in preparation for supporting ERB templates for some things... --- lib/vagrant.rb | 2 +- lib/vagrant/template_renderer.rb | 36 +++++++++ test/vagrant/template_renderer_test.rb | 105 +++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 lib/vagrant/template_renderer.rb create mode 100644 test/vagrant/template_renderer_test.rb diff --git a/lib/vagrant.rb b/lib/vagrant.rb index e3b56fccd..f58f77a5c 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -3,7 +3,7 @@ PROJECT_ROOT = File.join(libdir, '..') unless defined?(PROJECT_ROOT) # The libs which must be loaded prior to the rest %w{tempfile open-uri json pathname logger uri net/http virtualbox net/ssh archive/tar/minitar - net/scp fileutils}.each do |lib| + net/scp fileutils ostruct}.each do |lib| require lib end diff --git a/lib/vagrant/template_renderer.rb b/lib/vagrant/template_renderer.rb new file mode 100644 index 000000000..88f1ed615 --- /dev/null +++ b/lib/vagrant/template_renderer.rb @@ -0,0 +1,36 @@ +module Vagrant + # This class is used to render the ERB templates in the + # `GEM_ROOT/templates` directory. + class TemplateRenderer < OpenStruct + class < :baz}) + assert_equal :baz, r.bar + end + end + + context "rendering" do + setup do + @template = "foo" + @r = Vagrant::TemplateRenderer.new(@template) + @r.stubs(:full_template_path).returns(@template + "!") + + @contents = "bar" + + @file = mock("file") + @file.stubs(:read).returns(@contents) + File.stubs(:open).yields(@file) + + @erb = mock("erb") + @erb.stubs(:result) + end + + should "open the template file for reading" do + File.expects(:open).with(@r.full_template_path, 'r').once + @r.render + end + + should "create an ERB object with the file contents" do + ERB.expects(:new).with(@file.read).returns(@erb) + @r.render + end + + should "render the ERB file and return the results" do + result = mock("result") + ERB.expects(:new).returns(@erb) + @erb.expects(:result).with(anything).once.returns(result) + assert_equal result, @r.render + end + + should "render the ERB file in the context of the renderer" do + result = "bar" + template = "<%= foo %>" + @r.foo = result + @file.expects(:read).returns(template) + assert_equal result, @r.render + end + end + + context "the full template path" do + setup do + @template = "foo" + @r = Vagrant::TemplateRenderer.new(@template) + end + + should "be the ERB file in the templates directory" do + result = File.join(PROJECT_ROOT, "templates", "#{@template}.erb") + assert_equal result, @r.full_template_path + end + end + + context "class-level render! method" do + setup do + @template = "foo" + @r = Vagrant::TemplateRenderer.new(@template) + @r.stubs(:render) + + Vagrant::TemplateRenderer.stubs(:new).with(@template).returns(@r) + end + + should "use the first argument as the template" do + template = "foo" + Vagrant::TemplateRenderer.expects(:new).with(template).returns(@r) + Vagrant::TemplateRenderer.render!(template) + end + + should "send in additional argument to the renderer" do + template = "foo" + data = {:hey => :foo} + Vagrant::TemplateRenderer.expects(:new).with(template, data).returns(@r) + Vagrant::TemplateRenderer.render!(template, data) + end + + should "yield a block if given with the renderer as the argument" do + @r.expects(:yielded=).with(true).once + Vagrant::TemplateRenderer.render!(@template) do |r| + r.yielded = true + end + end + + should "render the result" do + result = mock('result') + @r.expects(:render).returns(result) + assert_equal result, Vagrant::TemplateRenderer.render!(@template) + end + end +end