diff --git a/bin/vagrant-init b/bin/vagrant-init index bd2ae9791..22e11348f 100755 --- a/bin/vagrant-init +++ b/bin/vagrant-init @@ -22,6 +22,6 @@ Creates the initial files required for using vagrant. EOS run do |command| - Vagrant::Commands.init + Vagrant::Commands.init(command.argv[0]) end end diff --git a/lib/vagrant.rb b/lib/vagrant.rb index f58f77a5c..fbee248f8 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 ostruct}.each do |lib| + net/scp fileutils ostruct erb}.each do |lib| require lib end diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 044dc96cf..23dc8264f 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -11,7 +11,7 @@ module Vagrant # initial `Vagrantfile` into the current working directory so you can # begin using vagrant. The configuration file contains some documentation # to get you started. - def init + def init(default_box=nil) rootfile_path = File.join(Dir.pwd, Env::ROOTFILE_NAME) if File.exist?(rootfile_path) error_and_exit(<<-error) @@ -21,7 +21,10 @@ error end # Copy over the rootfile template into this directory - FileUtils.cp(File.join(PROJECT_ROOT, "templates", Env::ROOTFILE_NAME), rootfile_path) + default_box ||= "base" + File.open(rootfile_path, 'w+') do |f| + f.write(TemplateRenderer.render!(Env::ROOTFILE_NAME, :default_box => default_box)) + end end # Outputs the status of the current environment. This command outputs diff --git a/templates/Vagrantfile b/templates/Vagrantfile.erb similarity index 63% rename from templates/Vagrantfile rename to templates/Vagrantfile.erb index 94bdc8606..126f28526 100644 --- a/templates/Vagrantfile +++ b/templates/Vagrantfile.erb @@ -1,8 +1,8 @@ Vagrant::Config.run do |config| # All Vagrant configuration is done here. For a detailed explanation - # and listing of configuration options, please check the documentation + # and listing of configuration options, please view the documentation # online. # Every Vagrant virtual environment requires a box to build off of. - config.vm.box = "base" + config.vm.box = "<%= default_box %>" end diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 446f8977f..3bfc588ce 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -12,9 +12,12 @@ class CommandsTest < Test::Unit::TestCase context "init" do setup do - FileUtils.stubs(:cp) + @file = mock("file") + @file.stubs(:write) + File.stubs(:open).yields(@file) @rootfile_path = File.join(Dir.pwd, Vagrant::Env::ROOTFILE_NAME) - @template_path = File.join(PROJECT_ROOT, "templates", Vagrant::Env::ROOTFILE_NAME) + + Vagrant::TemplateRenderer.stubs(:render!) end should "error and exit if a rootfile already exists" do @@ -23,9 +26,23 @@ class CommandsTest < Test::Unit::TestCase Vagrant::Commands.init end - should "copy the templated rootfile to the current path" do - File.expects(:exist?).with(@rootfile_path).returns(false) - FileUtils.expects(:cp).with(@template_path, @rootfile_path).once + should "write to the rootfile path using the template renderer" do + result = "foo" + Vagrant::TemplateRenderer.expects(:render!).returns(result).once + @file.expects(:write).with(result).once + File.expects(:open).with(@rootfile_path, 'w+').yields(@file) + + Vagrant::Commands.init + end + + should "use the given base box if given" do + box = "zooo" + Vagrant::TemplateRenderer.expects(:render!).with(Vagrant::Env::ROOTFILE_NAME, :default_box => box) + Vagrant::Commands.init(box) + end + + should "use the default `base` if no box is given" do + Vagrant::TemplateRenderer.expects(:render!).with(Vagrant::Env::ROOTFILE_NAME, :default_box => "base") Vagrant::Commands.init end end