From db24291b3de837438fb8933bb28dc9dabaa3b75e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 8 Mar 2010 13:12:58 -0800 Subject: [PATCH] Fix bug where dotfile_path is a directory in the project path. Thanks brett_h --- lib/vagrant/actions/vm/up.rb | 11 +++++++++++ lib/vagrant/env.rb | 2 +- test/vagrant/actions/vm/up_test.rb | 22 ++++++++++++++++++++++ test/vagrant/env_test.rb | 17 ++++++++++++++--- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/vagrant/actions/vm/up.rb b/lib/vagrant/actions/vm/up.rb index 765df5db9..7f67a0f5f 100644 --- a/lib/vagrant/actions/vm/up.rb +++ b/lib/vagrant/actions/vm/up.rb @@ -3,6 +3,17 @@ module Vagrant module VM class Up < Base def prepare + # If the dotfile is not a file, raise error + if File.exist?(Env.dotfile_path) && !File.file?(Env.dotfile_path) + raise ActionException.new(<<-msg) +The dotfile which Vagrant uses to store the UUID of the project's +virtual machine already exists and is not a file! The dotfile is +currently configured to be `#{Env.dotfile_path}` + +To change this value, please see `config.vagrant.dotfile_name` +msg + end + # Up is a "meta-action" so it really just queues up a bunch # of other actions in its place: steps = [Import, ForwardPorts, SharedFolders, Start] diff --git a/lib/vagrant/env.rb b/lib/vagrant/env.rb index 38f4cb383..0c70ec2ce 100644 --- a/lib/vagrant/env.rb +++ b/lib/vagrant/env.rb @@ -71,7 +71,7 @@ module Vagrant end def load_vm! - return unless root_path + return if !root_path || !File.file?(dotfile_path) File.open(dotfile_path) do |f| @@persisted_vm = Vagrant::VM.find(f.read) diff --git a/test/vagrant/actions/vm/up_test.rb b/test/vagrant/actions/vm/up_test.rb index 5b2fd3cd7..1ce641a4e 100644 --- a/test/vagrant/actions/vm/up_test.rb +++ b/test/vagrant/actions/vm/up_test.rb @@ -8,6 +8,8 @@ class UpActionTest < Test::Unit::TestCase context "sub-actions" do setup do + File.stubs(:file?).returns(true) + File.stubs(:exist?).returns(true) @default_order = [Vagrant::Actions::VM::Import, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Start] end @@ -18,6 +20,26 @@ class UpActionTest < Test::Unit::TestCase end end + should "raise an ActionException if a dotfile exists but is not a file" do + File.expects(:file?).with(Vagrant::Env.dotfile_path).returns(false) + assert_raises(Vagrant::Actions::ActionException) { + @action.prepare + } + end + + should "not raise an ActionException if dotfile doesn't exist" do + setup_action_expectations + File.stubs(:exist?).returns(false) + assert_nothing_raised { @action.prepare } + end + + should "not raise an ActionException if dotfile exists but is a file" do + File.stubs(:file?).returns(true) + File.stubs(:exist?).returns(true) + setup_action_expectations + assert_nothing_raised { @action.prepare } + end + should "do the proper actions by default" do setup_action_expectations @action.prepare diff --git a/test/vagrant/env_test.rb b/test/vagrant/env_test.rb index b5205d515..7766f5f6d 100644 --- a/test/vagrant/env_test.rb +++ b/test/vagrant/env_test.rb @@ -6,6 +6,7 @@ class EnvTest < Test::Unit::TestCase filemock.expects(:read).returns("foo") Vagrant::VM.expects(:find).with("foo").returns(returnvalue) File.expects(:open).with(Vagrant::Env.dotfile_path).once.yields(filemock) + File.expects(:file?).with(Vagrant::Env.dotfile_path).once.returns(true) Vagrant::Env.load_vm! end @@ -155,7 +156,11 @@ class EnvTest < Test::Unit::TestCase end context "loading the UUID out from the persisted file" do - test "loading of the uuid from the dotfile" do + setup do + File.stubs(:file?).returns(true) + end + + should "loading of the uuid from the dotfile" do mock_persisted_vm assert_equal 'foovm', Vagrant::Env.persisted_vm end @@ -166,13 +171,19 @@ class EnvTest < Test::Unit::TestCase Vagrant::Env.load_vm! end - test "uuid should be nil if dotfile didn't exist" do + should "do nothing if dotfile is not a file" do + File.expects(:file?).returns(false) + File.expects(:open).never + Vagrant::Env.load_vm! + end + + should "uuid should be nil if dotfile didn't exist" do File.expects(:open).raises(Errno::ENOENT) Vagrant::Env.load_vm! assert_nil Vagrant::Env.persisted_vm end - test "should build up the dotfile out of the root path and the dotfile name" do + should "should build up the dotfile out of the root path and the dotfile name" do assert_equal File.join(Vagrant::Env.root_path, Vagrant.config.vagrant.dotfile_name), Vagrant::Env.dotfile_path end end