Skip to content

Commit

Permalink
On Windows, prefer USERPROFILE for home directory path
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Apr 6, 2013
1 parent 744c879 commit fbdd46a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## 1.2.0 (unreleased)

BACKWARDS INCOMPATIBILITIES:

- WINDOWS USERS: Vagrant now defaults to using the 'USERPROFILE' environmental
variable for the home directory if it is set. This means that the default
location for the Vagrant home directory is now `%USERPROFILE%/.vagrant.d`.
On Cygwin, this will cause existing Cygwin users to "lose" their boxes.
To work around this, either set `VAGRANT_HOME` to your Cygwin ".vagrant.d"
folder or move your ".vagrant.d" folder to `USERPROFILE`. The latter is
recommended for long-term support.

FEATURES:

- Providers can now parallelize! If they explicitly support it, Vagrant
Expand Down
20 changes: 18 additions & 2 deletions lib/vagrant/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module Vagrant
# defined as basically a folder with a "Vagrantfile." This class allows
# access to the VMs, CLI, etc. all in the scope of this environment.
class Environment
DEFAULT_HOME = "~/.vagrant.d"
DEFAULT_LOCAL_DATA = ".vagrant"

# The `cwd` that this environment represents
Expand Down Expand Up @@ -554,7 +553,7 @@ def lock
def setup_home_path
@home_path = Pathname.new(File.expand_path(@home_path ||
ENV["VAGRANT_HOME"] ||
DEFAULT_HOME))
default_home_path))
@logger.info("Home path: #{@home_path}")

# Setup the list of child directories that need to be created if they
Expand Down Expand Up @@ -654,6 +653,23 @@ def copy_insecure_private_key
end
end

# This returns the default home directory path for Vagrant, which
# can differ depending on the system.
#
# @return [Pathname]
def default_home_path
path = "~/.vagrant.d"

# On Windows, we default ot the USERPROFILE directory if it
# is available. This is more compatible with Cygwin and sharing
# the home directory across shells.
if Util::Platform.windows? && ENV["USERPROFILE"]
path = "#{ENV["USERPROFILE"]}/.vagrant.d"
end

Pathname.new(path)
end

# Finds the Vagrantfile in the given directory.
#
# @param [Pathname] path Path to search in.
Expand Down
34 changes: 31 additions & 3 deletions test/unit/vagrant/environment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,37 @@
end
end

it "is set to the DEFAULT_HOME by default" do
expected = Pathname.new(File.expand_path(described_class::DEFAULT_HOME))
described_class.new.home_path.should == expected
context "default home path" do
before :each do
Vagrant::Util::Platform.stub(:windows? => false)
end

it "is set to '~/.vagrant.d' by default" do
expected = Pathname.new(File.expand_path("~/.vagrant.d"))
described_class.new.home_path.should == expected
end

it "is set to '~/.vagrant.d' if on Windows but no USERPROFILE" do
Vagrant::Util::Platform.stub(:windows? => true)

expected = Pathname.new(File.expand_path("~/.vagrant.d"))

with_temp_env("USERPROFILE" => nil) do
described_class.new.home_path.should == expected
end
end

it "is set to '%USERPROFILE%/.vagrant.d' if on Windows and USERPROFILE is set" do
Vagrant::Util::Platform.stub(:windows? => true)

Dir.mktmpdir do |dir|
expected = Pathname.new(File.expand_path("#{dir}/.vagrant.d"))

with_temp_env("USERPROFILE" => dir) do
described_class.new.home_path.should == expected
end
end
end
end

it "throws an exception if inaccessible" do
Expand Down

0 comments on commit fbdd46a

Please sign in to comment.