Vagrant::Env sets up the home directory (specified in config) with proper subfolders if they don't exist.

This commit is contained in:
Mitchell Hashimoto 2010-02-22 01:03:08 -08:00
parent f1fda1f0fa
commit 2b1afa6f6f
2 changed files with 42 additions and 0 deletions

View File

@ -1,6 +1,7 @@
module Vagrant module Vagrant
class Env class Env
ROOTFILE_NAME = "Vagrantfile" ROOTFILE_NAME = "Vagrantfile"
HOME_SUBDIRS = ["tmp", "boxes"]
# Initialize class variables used # Initialize class variables used
@@persisted_vm = nil @@persisted_vm = nil
@ -16,6 +17,7 @@ module Vagrant
def load! def load!
load_root_path! load_root_path!
load_config! load_config!
load_home_directory!
load_vm! load_vm!
end end
@ -34,6 +36,20 @@ module Vagrant
Config.execute! Config.execute!
end end
def load_home_directory!
home_dir = File.expand_path(Vagrant.config.vagrant.home)
dirs = HOME_SUBDIRS.collect { |path| File.join(home_dir, path) }
dirs.unshift(home_dir)
dirs.each do |dir|
next if File.directory?(dir)
logger.info "Creating home directory since it doesn't exist: #{dir}"
FileUtils.mkdir_p(dir)
end
end
def load_vm! def load_vm!
File.open(dotfile_path) do |f| File.open(dotfile_path) do |f|
@@persisted_vm = Vagrant::VM.find(f.read) @@persisted_vm = Vagrant::VM.find(f.read)

View File

@ -27,6 +27,31 @@ class EnvTest < Test::Unit::TestCase
end end
end end
context "loading home directory" do
setup do
@home_dir = File.expand_path(Vagrant.config.vagrant.home)
File.stubs(:directory?).returns(true)
FileUtils.stubs(:mkdir_p)
end
should "create each directory if it doesn't exist" do
create_seq = sequence("create_seq")
File.stubs(:directory?).returns(false)
Vagrant::Env::HOME_SUBDIRS.each do |subdir|
FileUtils.expects(:mkdir_p).with(File.join(@home_dir, subdir)).in_sequence(create_seq)
end
Vagrant::Env.load_home_directory!
end
should "not create directories if they exist" do
File.stubs(:directory?).returns(true)
FileUtils.expects(:mkdir_p).never
Vagrant::Env.load_home_directory!
end
end
context "loading config" do context "loading config" do
setup do setup do
@root_path = "/foo" @root_path = "/foo"
@ -69,6 +94,7 @@ class EnvTest < Test::Unit::TestCase
Vagrant::Env.expects(:load_config!).once Vagrant::Env.expects(:load_config!).once
Vagrant::Env.expects(:load_vm!).once Vagrant::Env.expects(:load_vm!).once
Vagrant::Env.expects(:load_root_path!).once Vagrant::Env.expects(:load_root_path!).once
Vagrant::Env.expects(:load_home_directory!).once
Vagrant::Env.load! Vagrant::Env.load!
end end
end end