From 61944219728a10b251cb95421aeaf131af2a8d3e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 7 Mar 2010 12:44:00 -0800 Subject: [PATCH] `vagrant box` no longer requires a Vagrantfile in the current directory. --- lib/vagrant/commands.rb | 2 +- lib/vagrant/env.rb | 10 ++++++---- test/vagrant/commands_test.rb | 2 +- test/vagrant/env_test.rb | 14 +++++++++++++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index fd87251a6..33dbc3fd4 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -128,7 +128,7 @@ error # which action to take and calls the respective action method # (see {box_add} and {box_remove}) def box(argv) - Env.load! + Env.load!(:suppress_errors => true) sub_commands = ["add", "remove"] diff --git a/lib/vagrant/env.rb b/lib/vagrant/env.rb index bdae6d84f..cfca33770 100644 --- a/lib/vagrant/env.rb +++ b/lib/vagrant/env.rb @@ -14,13 +14,13 @@ module Vagrant def box; @@box; end def persisted_vm; @@persisted_vm; end def root_path; @@root_path; end - def dotfile_path; File.join(root_path, Vagrant.config.vagrant.dotfile_name); end + def dotfile_path;File.join(root_path, Vagrant.config.vagrant.dotfile_name); end def home_path; File.expand_path(Vagrant.config.vagrant.home); end def tmp_path; File.join(home_path, "tmp"); end def boxes_path; File.join(home_path, "boxes"); end - def load! - load_root_path! + def load!(opts={}) + load_root_path!(Pathname.new(Dir.pwd), opts) load_config! load_home_directory! load_box! @@ -31,7 +31,7 @@ module Vagrant # Prepare load paths for config files load_paths = [File.join(PROJECT_ROOT, "config", "default.rb")] load_paths << File.join(box.directory, ROOTFILE_NAME) if box - load_paths << File.join(root_path, ROOTFILE_NAME) + load_paths << File.join(root_path, ROOTFILE_NAME) if root_path # Then clear out the old data Config.reset! @@ -69,6 +69,8 @@ module Vagrant end def load_vm! + return unless root_path + File.open(dotfile_path) do |f| @@persisted_vm = Vagrant::VM.find(f.read) end diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 95b9939ca..d986eb3cf 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -194,7 +194,7 @@ class CommandsTest < Test::Unit::TestCase end should "load the environment" do - Vagrant::Env.expects(:load!).once + Vagrant::Env.expects(:load!).with(:suppress_errors => true).once Vagrant::Commands.box(["add"]) end diff --git a/test/vagrant/env_test.rb b/test/vagrant/env_test.rb index ec192debe..20531c6af 100644 --- a/test/vagrant/env_test.rb +++ b/test/vagrant/env_test.rb @@ -78,6 +78,12 @@ class EnvTest < Test::Unit::TestCase Vagrant::Env.load_config! end + should "not load from the root path if nil" do + Vagrant::Env.stubs(:root_path).returns(nil) + File.expects(:exist?).with(File.join(@root_path, Vagrant::Env::ROOTFILE_NAME)).never + Vagrant::Env.load_config! + end + should "not load from the box directory if it is nil" do Vagrant::Env.expects(:box).once.returns(nil) Vagrant::Env.load_config! @@ -115,7 +121,7 @@ class EnvTest < Test::Unit::TestCase test "load! should load the config and set the persisted_uid" do Vagrant::Env.expects(:load_config!).once Vagrant::Env.expects(:load_vm!).once - Vagrant::Env.expects(:load_root_path!).once + Vagrant::Env.expects(:load_root_path!).with(Pathname.new(Dir.pwd), {}).once Vagrant::Env.expects(:load_home_directory!).once Vagrant::Env.expects(:load_box!).once Vagrant::Env.load! @@ -144,6 +150,12 @@ class EnvTest < Test::Unit::TestCase assert_equal 'foovm', Vagrant::Env.persisted_vm end + should "do nothing if the root path is nil" do + File.expects(:open).never + Vagrant::Env.stubs(:root_path).returns(nil) + Vagrant::Env.load_vm! + end + test "uuid should be nil if dotfile didn't exist" do File.expects(:open).raises(Errno::ENOENT) Vagrant::Env.load_vm!