diff --git a/lib/vagrant/env.rb b/lib/vagrant/env.rb index 70e851572..15b64107a 100644 --- a/lib/vagrant/env.rb +++ b/lib/vagrant/env.rb @@ -1,6 +1,7 @@ module Vagrant class Env ROOTFILE_NAME = "Vagrantfile" + HOME_SUBDIRS = ["tmp", "boxes"] # Initialize class variables used @@persisted_vm = nil @@ -16,6 +17,7 @@ module Vagrant def load! load_root_path! load_config! + load_home_directory! load_vm! end @@ -34,6 +36,20 @@ module Vagrant Config.execute! 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! File.open(dotfile_path) do |f| @@persisted_vm = Vagrant::VM.find(f.read) diff --git a/test/vagrant/env_test.rb b/test/vagrant/env_test.rb index dc0474eb9..7c4cb0410 100644 --- a/test/vagrant/env_test.rb +++ b/test/vagrant/env_test.rb @@ -27,6 +27,31 @@ class EnvTest < Test::Unit::TestCase 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 setup do @root_path = "/foo" @@ -69,6 +94,7 @@ class EnvTest < Test::Unit::TestCase Vagrant::Env.expects(:load_config!).once Vagrant::Env.expects(:load_vm!).once Vagrant::Env.expects(:load_root_path!).once + Vagrant::Env.expects(:load_home_directory!).once Vagrant::Env.load! end end