Environment#home_path is a Pathname object for easier manipulation

This commit is contained in:
Mitchell Hashimoto 2010-09-11 09:18:33 -07:00
parent 86465a36c0
commit 4b17ac0f89
4 changed files with 15 additions and 25 deletions

View File

@ -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)

View File

@ -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|

View File

@ -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

View File

@ -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!