Change default home directory to ~/.vagrant.d [closes GH-333]

This commit is contained in:
Mitchell Hashimoto 2011-07-09 18:54:35 -07:00
parent dcfc87a547
commit 442584fbac
6 changed files with 76 additions and 1 deletions

View File

@ -26,6 +26,7 @@
- `package` won't delete previously existing file. [GH-408]
- Vagrantfile can be lowercase now. [GH-399]
- Only one copy of Vagrant may be running at any given time. [GH-364]
- Default home directory for Vagrant moved to `~/.vagrant.d` [GH-333]
## 0.7.6 (July 2, 2011)

View File

@ -8,7 +8,7 @@ module Vagrant
class Environment
HOME_SUBDIRS = ["tmp", "boxes", "logs"]
DEFAULT_VM = :default
DEFAULT_HOME = "~/.vagrant"
DEFAULT_HOME = "~/.vagrant.d"
# Parent environment (in the case of multi-VMs)
attr_reader :parent
@ -97,7 +97,29 @@ module Vagrant
#
# @return [Pathname]
def home_path
return @_home_path if defined?(@_home_path)
@_home_path ||= Pathname.new(File.expand_path(ENV["VAGRANT_HOME"] || DEFAULT_HOME))
# This is the old default that Vagrant used to be put things into
# up until Vagrant 0.8.0. We keep around an automatic migration
# script here in case any old users upgrade.
old_home = File.expand_path("~/.vagrant")
if File.exists?(old_home) && File.directory?(old_home)
# We can't migrate if the home directory already exists
if File.exists?(@_home_path)
ui.warn I18n.t("vagrant.general.home_dir_migration_failed",
:old => old_home,
:new => @_home_path.to_s)
else
# If the new home path doesn't exist, simply transition to it
ui.info I18n.t("vagrant.general.moving_home_dir", :directory => @_home_path)
FileUtils.mv(old_home, @_home_path)
end
end
# Return the home path
@_home_path
end
# The path to the Vagrant tmp directory

View File

@ -158,6 +158,11 @@ module Vagrant
error_key(:environment_locked)
end
class HomeDirectoryMigrationFailed < VagrantError
status_code(53)
error_key(:home_dir_migration_failed)
end
class ForwardPortAutolistEmpty < VagrantError
status_code(27)
error_key(:auto_empty, "vagrant.actions.vm.forward_ports")

View File

@ -2,6 +2,14 @@ en:
vagrant:
general:
creating_home_dir: "Creating home directory since it doesn't exist: %{directory}"
moving_home_dir: "Moving old Vagrant home directory to new location: %{directory}"
home_dir_migration_failed: |-
Both an old and new Vagrant home directory exist. Only the new one will
be used. Please merge the old directory into the new directory if you'd
like to use the old data as well.
Old: %{old}
New: %{new}
#-------------------------------------------------------------------------------
# Translations for exception classes

View File

@ -13,6 +13,9 @@ begin
rescue LoadError
end
# Set the home directory to some temporary directory
ENV["HOME"] = Vagrant.source_root.join("test", "tmp", "home").to_s
# Add the I18n locale for tests
I18n.load_path << File.expand_path("../locales/en.yml", __FILE__)

View File

@ -58,6 +58,12 @@ class EnvironmentTest < Test::Unit::TestCase
context "home path" do
setup do
@env = @klass.new
# Make a fake home directory for helping with tests
@home_path = tmp_path.join("home")
ENV["HOME"] = @home_path.to_s
FileUtils.rm_rf(@home_path)
FileUtils.mkdir_p(@home_path)
end
should "return the home path if it loaded" do
@ -73,6 +79,36 @@ class EnvironmentTest < Test::Unit::TestCase
expected = Pathname.new(File.expand_path(ENV["VAGRANT_HOME"]))
assert_equal expected, @env.home_path
end
should "move the old home directory to the new location" do
new_path = @home_path.join(".vagrant.d")
old_path = @home_path.join(".vagrant")
old_path.mkdir
# Get the home path
ENV["VAGRANT_HOME"] = new_path.to_s
assert !new_path.exist?
assert_equal new_path, @env.home_path
assert !old_path.exist?
assert new_path.exist?
end
should "not move the old home directory if the new one already exists" do
new_path = @home_path.join(".vagrant.d")
new_path.mkdir
old_path = @home_path.join(".vagrant")
old_path.mkdir
# Get the home path
ENV["VAGRANT_HOME"] = new_path.to_s
assert new_path.exist?
assert_equal new_path, @env.home_path
assert old_path.exist?
assert new_path.exist?
end
end
context "temp path" do