From 4b17ac0f8927df5fd8a7e71f16cea89c34740282 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 11 Sep 2010 09:18:33 -0700 Subject: [PATCH] Environment#home_path is a Pathname object for easier manipulation --- lib/vagrant/config/vagrant.rb | 6 +----- lib/vagrant/environment.rb | 13 +++++++------ test/vagrant/config/vagrant_test.rb | 6 ++---- test/vagrant/environment_test.rb | 15 +++++---------- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/lib/vagrant/config/vagrant.rb b/lib/vagrant/config/vagrant.rb index c518fd811..e06ef85dc 100644 --- a/lib/vagrant/config/vagrant.rb +++ b/lib/vagrant/config/vagrant.rb @@ -5,17 +5,13 @@ module Vagrant attr_accessor :dotfile_name attr_accessor :log_output - attr_writer :home + attr_accessor :home attr_accessor :host def initialize @home = nil end - def home - File.expand_path(@home) - end - def validate(errors) [:dotfile_name, :home, :host].each do |field| errors.add("vagrant.config.common.error_empty", :field => field) if !instance_variable_get("@#{field}".to_sym) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 0aa32a1cf..0c2c05a77 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -58,19 +58,20 @@ module Vagrant root_path.join(config.vagrant.dotfile_name) rescue nil end - # The path to the home directory, which is usually in `~/.vagrant/~ + # The path to the home directory, expanded relative to the root path, + # and converted into a Pathname object. def home_path - config ? config.vagrant.home : nil + @_home_path ||= Pathname.new(File.expand_path(config.vagrant.home, root_path)) end # The path to the Vagrant tmp directory def tmp_path - File.join(home_path, "tmp") + home_path.join("tmp") end # The path to the Vagrant boxes directory def boxes_path - File.join(home_path, "boxes") + home_path.join("boxes") end # Returns the name of the resource which this environment represents. @@ -260,8 +261,8 @@ module Vagrant # within the home directory if they're not already created. def load_home_directory! # Setup the array of necessary home directories - dirs = HOME_SUBDIRS.collect { |subdir| File.join(home_path, subdir) } - dirs.unshift(home_path) + dirs = [home_path] + dirs += HOME_SUBDIRS.collect { |subdir| home_path.join(subdir) } # Go through each required directory, creating it if it doesn't exist dirs.each do |dir| diff --git a/test/vagrant/config/vagrant_test.rb b/test/vagrant/config/vagrant_test.rb index c9feafe0e..761c3a4fb 100644 --- a/test/vagrant/config/vagrant_test.rb +++ b/test/vagrant/config/vagrant_test.rb @@ -5,9 +5,7 @@ class ConfigVagrantTest < Test::Unit::TestCase @config = Vagrant::Config::VagrantConfig.new end - should "expand the path if home is not nil" do - @config.home = "foo" - File.expects(:expand_path).with("foo").once.returns("result") - assert_equal "result", @config.home + context "validation" do + # TODO end end diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index 81892640f..ddd75573a 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -59,25 +59,21 @@ class EnvironmentTest < Test::Unit::TestCase end context "home path" do - should "return nil if config is not yet loaded" do - @env.stubs(:config).returns(nil) - assert_nil @env.home_path - end - should "return the home path if it loaded" do - assert_equal @env.config.vagrant.home, @env.home_path + expected = Pathname.new(File.expand_path(@env.config.vagrant.home, @env.root_path)) + assert_equal expected, @env.home_path end end context "temp path" do should "return the home path joined with 'tmp'" do - assert_equal File.join(@env.home_path, "tmp"), @env.tmp_path + assert_equal @env.home_path.join("tmp"), @env.tmp_path end end context "boxes path" do should "return the home path joined with 'tmp'" do - assert_equal File.join(@env.home_path, "boxes"), @env.boxes_path + assert_equal @env.home_path.join("boxes"), @env.boxes_path end end end @@ -387,7 +383,6 @@ class EnvironmentTest < Test::Unit::TestCase context "loading home directory" do setup do @env = vagrant_env - @home_dir = File.expand_path(@env.config.vagrant.home) File.stubs(:directory?).returns(true) FileUtils.stubs(:mkdir_p) @@ -397,7 +392,7 @@ class EnvironmentTest < Test::Unit::TestCase create_seq = sequence("create_seq") File.stubs(:directory?).returns(false) @klass::HOME_SUBDIRS.each do |subdir| - FileUtils.expects(:mkdir_p).with(File.join(@home_dir, subdir)).in_sequence(create_seq) + FileUtils.expects(:mkdir_p).with(@env.home_path.join(subdir)).in_sequence(create_seq) end @env.load_home_directory!