From 5f6e3acf4018aa0922338947a54a8f2903038742 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 7 Sep 2010 10:52:58 -0700 Subject: [PATCH] Require root path on environment load --- lib/vagrant/action/vm/nfs.rb | 2 +- lib/vagrant/command/helpers.rb | 6 ---- lib/vagrant/environment.rb | 1 + test/support/objects.rb | 13 ++++++++ test/test_helper.rb | 7 +++-- test/vagrant/action/vm/check_box_test.rb | 40 +++++++++++++++--------- test/vagrant/action/vm/nfs_test.rb | 5 +-- test/vagrant/command/helpers_test.rb | 16 ---------- test/vagrant/environment_test.rb | 20 +++--------- test/vagrant/hosts/bsd_test.rb | 8 ++--- test/vagrant/hosts/linux_test.rb | 7 ++--- 11 files changed, 60 insertions(+), 65 deletions(-) create mode 100644 test/support/objects.rb diff --git a/lib/vagrant/action/vm/nfs.rb b/lib/vagrant/action/vm/nfs.rb index 1cf3bc49c..c50bcf033 100644 --- a/lib/vagrant/action/vm/nfs.rb +++ b/lib/vagrant/action/vm/nfs.rb @@ -91,7 +91,7 @@ module Vagrant # The options on the hash get priority, then the default # values - value = opts[key] || @env["config"].nfs.send(key) + value = opts.has_key?(key) ? opts[key] : @env["config"].nfs.send(key) return value if value != :auto # Get UID/GID from folder if we've made it this far diff --git a/lib/vagrant/command/helpers.rb b/lib/vagrant/command/helpers.rb index 8427e6152..05f80f3d6 100644 --- a/lib/vagrant/command/helpers.rb +++ b/lib/vagrant/command/helpers.rb @@ -9,15 +9,9 @@ module Vagrant @env.ui = UI::Shell.new(@env, shell) if !@env.ui.is_a?(UI::Shell) end - def require_environment - raise Errors::NoEnvironmentError.new if !env.root_path - end - # This returns an array of {VM} objects depending on the arguments # given to the command. def target_vms - require_environment - @target_vms ||= begin if env.multivm? return env.vms.values if !self.name diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index d6149cbdb..ff1300314 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -209,6 +209,7 @@ module Vagrant def load! if !loaded? @loaded = true + raise Errors::NoEnvironmentError.new if !root_path self.class.check_virtualbox! load_config! load_vm! diff --git a/test/support/objects.rb b/test/support/objects.rb new file mode 100644 index 000000000..1f44f177d --- /dev/null +++ b/test/support/objects.rb @@ -0,0 +1,13 @@ +module VagrantTestHelpers + module Objects + # Returns a blank app (callable) and action environment with the + # given vagrant environment. + def action_env(v_env = nil) + v_env ||= vagrant_env + app = lambda { |env| } + env = Vagrant::Action::Environment.new(v_env) + env["vagrant.test"] = true + [app, env] + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 361393529..fe39779be 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -7,6 +7,7 @@ require 'contest' require 'mocha' require 'support/path' require 'support/environment' +require 'support/objects' # Try to load ruby debug since its useful if it is available. # But not a big deal if its not available (probably on a non-MRI @@ -25,6 +26,7 @@ I18n.load_path << File.expand_path("../locales/en.yml", __FILE__) class Test::Unit::TestCase include VagrantTestHelpers::Path include VagrantTestHelpers::Environment + include VagrantTestHelpers::Objects # Mocks an environment, setting it up with the given config. def mock_environment @@ -102,9 +104,10 @@ class Test::Unit::TestCase vm end - def mock_action_data + def mock_action_data(v_env=nil) + v_env ||= vagrant_env app = lambda { |env| } - env = Vagrant::Action::Environment.new(mock_environment) + env = Vagrant::Action::Environment.new(v_env) env["vagrant.test"] = true [app, env] end diff --git a/test/vagrant/action/vm/check_box_test.rb b/test/vagrant/action/vm/check_box_test.rb index 3cc1917ef..1c258ae9a 100644 --- a/test/vagrant/action/vm/check_box_test.rb +++ b/test/vagrant/action/vm/check_box_test.rb @@ -3,8 +3,6 @@ require 'test_helper' class CheckBoxVMActionTest < Test::Unit::TestCase setup do @klass = Vagrant::Action::VM::CheckBox - @app, @env = mock_action_data - @instance = @klass.new(@app, @env) end context "calling" do @@ -13,34 +11,48 @@ class CheckBoxVMActionTest < Test::Unit::TestCase end should "raise error if box not specified" do - @env.env.config.vm.box = nil - @app.expects(:call).never + app, env = action_env(vagrant_env(vagrantfile(<<-vf))) + config.vm.box = nil + vf + + instance = @klass.new(app, env) + app.expects(:call).never assert_raises(Vagrant::Errors::BoxNotSpecified) { - @instance.call(@env) + instance.call(env) } end should "error if box does not exist and URL not specified" do - @env.env.config.vm.box_url = nil - Vagrant::Box.expects(:find).with(@env.env, @env["config"].vm.box).returns(nil) + app, env = action_env(vagrant_env(vagrantfile(<<-vf))) + config.vm.box = "yo" + config.vm.box_url = nil + vf + + instance = @klass.new(app, env) + app.expects(:call).never + Vagrant::Box.expects(:find).with(env.env, env["config"].vm.box).returns(nil) - @app.expects(:call).never assert_raises(Vagrant::Errors::BoxSpecifiedDoesntExist) { - @instance.call(@env) + instance.call(env) } end should "attempt to download box and continue if URL specified" do + app, env = action_env(vagrant_env(vagrantfile(<<-vf))) + config.vm.box = "yo" + config.vm.box_url = "http://google.com" + vf + + instance = @klass.new(app, env) seq = sequence("seq") - @env.env.config.vm.box_url = "bar" Vagrant::Box.expects(:find).returns(nil) - Vagrant::Box.expects(:add).with(@env.env, @env["config"].vm.box, @env["config"].vm.box_url).in_sequence(seq) - @env.env.expects(:load_box!).in_sequence(seq) - @app.expects(:call).with(@env).once.in_sequence(seq) + Vagrant::Box.expects(:add).with(env.env, env["config"].vm.box, env["config"].vm.box_url).in_sequence(seq) + env.env.expects(:load_box!).in_sequence(seq) + app.expects(:call).with(env).once.in_sequence(seq) assert_nothing_raised { - @instance.call(@env) + instance.call(env) } end end diff --git a/test/vagrant/action/vm/nfs_test.rb b/test/vagrant/action/vm/nfs_test.rb index e27e0db5b..810bee84b 100644 --- a/test/vagrant/action/vm/nfs_test.rb +++ b/test/vagrant/action/vm/nfs_test.rb @@ -3,7 +3,7 @@ require "test_helper" class NFSVMActionTest < Test::Unit::TestCase setup do @klass = Vagrant::Action::VM::NFS - @app, @env = mock_action_data + @app, @env = action_env @vm = mock("vm") @vm.stubs(:system).returns(mock("system")) @@ -109,11 +109,12 @@ class NFSVMActionTest < Test::Unit::TestCase end should "return nil if the perm is not set" do + @env.env.config.nfs.map_uid = nil assert_nil @instance.prepare_permission(:uid, {:gid => 7}) end should "return nil if the perm explicitly says nil" do - assert_nil @instance.prepare_permission(:uid, {:uid => nil}) + assert_nil @instance.prepare_permission(:uid, {:map_uid => nil}) end should "return the set value if it is set" do diff --git a/test/vagrant/command/helpers_test.rb b/test/vagrant/command/helpers_test.rb index ca4fba480..d325eafdc 100644 --- a/test/vagrant/command/helpers_test.rb +++ b/test/vagrant/command/helpers_test.rb @@ -24,22 +24,6 @@ class CommandHelpersTest < Test::Unit::TestCase end end - context "requiring environment" do - setup do - @env = mock_environment - end - - should "raise an exception if no environment" do - @env.stubs(:root_path).returns(nil) - assert_raises(Vagrant::Errors::NoEnvironmentError) { command([], @env).require_environment } - end - - should "not raise an exception if there is an environment" do - @env.stubs(:root_path).returns(7) - assert_nothing_raised { command([], @env).require_environment } - end - end - context "vms from args" do setup do @env = mock_environment diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index a51b1bb6e..84a77ca69 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -433,12 +433,6 @@ class EnvironmentTest < Test::Unit::TestCase end context "loading box" do - should "do nothing if the root path is nil" do - env = @klass.new(:cwd => "/") - Vagrant::Box.expects(:find).never - env.load_box! - end - should "not load the box if its not set" do env = vagrant_env assert env.config.vm.box.nil? @@ -459,11 +453,7 @@ class EnvironmentTest < Test::Unit::TestCase context "loading the UUID out from the persisted dotfile" do setup do - @local_data = {} - - @env = mock_environment - @env.stubs(:root_path).returns("foo") - @env.stubs(:local_data).returns(@local_data) + @env = vagrant_env end should "blank the VMs" do @@ -474,13 +464,13 @@ class EnvironmentTest < Test::Unit::TestCase end should "load all the VMs from the dotfile" do - @local_data[:active] = { :foo => "bar", :bar => "baz" } + @env.local_data[:active] = { "foo" => "bar", "bar" => "baz" } results = {} - @local_data[:active].each do |key, value| + @env.local_data[:active].each do |key, value| vm = mock("vm#{key}") Vagrant::VM.expects(:find).with(value, @env, key.to_sym).returns(vm) - results[key] = vm + results[key.to_sym] = vm end @env.load_vm! @@ -502,7 +492,7 @@ class EnvironmentTest < Test::Unit::TestCase end should "uuid should be nil if local data contains nothing" do - assert @local_data.empty? # sanity + assert @env.local_data.empty? # sanity @env.load_vm! assert_nil @env.vm end diff --git a/test/vagrant/hosts/bsd_test.rb b/test/vagrant/hosts/bsd_test.rb index 481808258..8e73d15c8 100644 --- a/test/vagrant/hosts/bsd_test.rb +++ b/test/vagrant/hosts/bsd_test.rb @@ -3,10 +3,8 @@ require "test_helper" class BSDHostTest < Test::Unit::TestCase setup do @klass = Vagrant::Hosts::BSD - @env = mock_environment - @env.stubs(:vm).returns(Vagrant::VM.new(:env => @env)) - - @instance = @klass.new(@env) + @env = vagrant_env + @instance = @klass.new(@env.vms.values.first.env) end context "supporting nfs check" do @@ -39,7 +37,7 @@ class BSDHostTest < Test::Unit::TestCase should "output the lines of the rendered template" do output = %W[foo bar baz].join("\n") Vagrant::Util::TemplateRenderer.expects(:render).with("nfs/exports", - :uuid => @env.vm.uuid, + :uuid => @instance.env.vm.uuid, :ip => @ip, :folders => @folders).returns(output) diff --git a/test/vagrant/hosts/linux_test.rb b/test/vagrant/hosts/linux_test.rb index 31b267acd..5c1ff9a85 100644 --- a/test/vagrant/hosts/linux_test.rb +++ b/test/vagrant/hosts/linux_test.rb @@ -3,10 +3,9 @@ require "test_helper" class LinuxHostTest < Test::Unit::TestCase setup do @klass = Vagrant::Hosts::Linux - @env = mock_environment - @env.stubs(:vm).returns(Vagrant::VM.new(:env => @env)) + @env = vagrant_env - @instance = @klass.new(@env) + @instance = @klass.new(@env.vms.values.first.env) end context "supporting nfs check" do @@ -39,7 +38,7 @@ class LinuxHostTest < Test::Unit::TestCase should "output the lines of the rendered template" do output = %W[foo bar baz].join("\n") Vagrant::Util::TemplateRenderer.expects(:render).with("nfs/exports_linux", - :uuid => @env.vm.uuid, + :uuid => @instance.env.vm.uuid, :ip => @ip, :folders => @folders).returns(output)