Init subcommand is working

This commit is contained in:
Mitchell Hashimoto 2010-04-13 14:03:50 -07:00
parent 9ce860cb40
commit 26c12a9a17
4 changed files with 71 additions and 2 deletions

View File

@ -5,6 +5,8 @@ module Vagrant
# This is the base command class which all sub-commands must
# inherit from.
class Base
include Util
attr_reader :env
class <<self

View File

@ -9,7 +9,23 @@ module Vagrant
opts.banner = "Usage: vagrant init [name]"
end
show_help
create_vagrantfile(args[0])
end
# Actually writes the initial Vagrantfile to the current working directory.
# The Vagrantfile will contain the base box configuration specified, or
# will just use "base" if none is specified.
#
# @param [String] default_box The default base box for this Vagrantfile
def create_vagrantfile(default_box=nil)
rootfile_path = File.join(Dir.pwd, Environment::ROOTFILE_NAME)
error_and_exit(:rootfile_already_exists) if File.exist?(rootfile_path)
# Copy over the rootfile template into this directory
default_box ||= "base"
File.open(rootfile_path, 'w+') do |f|
f.write(TemplateRenderer.render(Environment::ROOTFILE_NAME, :default_box => default_box))
end
end
end
end

View File

@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class CommandsBastTest < Test::Unit::TestCase
class CommandsBaseTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Commands::Base
end

View File

@ -0,0 +1,51 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class CommandsInitTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Commands::Init
@env = mock_environment
@instance = @klass.new(@env)
end
context "execute" do
# TODO
end
context "creating the vagrantfile" do
setup do
@file = mock("file")
@file.stubs(:write)
File.stubs(:open).yields(@file)
@rootfile_path = File.join(Dir.pwd, Vagrant::Environment::ROOTFILE_NAME)
Vagrant::Util::TemplateRenderer.stubs(:render)
end
should "error and exit if a rootfile already exists" do
File.expects(:exist?).with(@rootfile_path).returns(true)
@instance.expects(:error_and_exit).with(:rootfile_already_exists).once
@instance.create_vagrantfile
end
should "write to the rootfile path using the template renderer" do
result = "foo"
Vagrant::Util::TemplateRenderer.expects(:render).returns(result).once
@file.expects(:write).with(result).once
File.expects(:open).with(@rootfile_path, 'w+').yields(@file)
@instance.create_vagrantfile
end
should "use the given base box if given" do
box = "zooo"
Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => box)
@instance.create_vagrantfile(box)
end
should "use the default `base` if no box is given" do
Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => "base")
@instance.create_vagrantfile
end
end
end