From 0f708121427523f670a30970ab82963cad9a2dd6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 3 Sep 2010 00:48:38 -0700 Subject: [PATCH] Load the host on demand in the environment --- lib/vagrant/environment.rb | 12 ++++------ test/vagrant/action/vm/nfs_helpers_test.rb | 1 + test/vagrant/environment_test.rb | 28 +++++++++++----------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index b846f8b7b..d364742cc 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -16,7 +16,6 @@ module Vagrant attr_reader :cwd attr_reader :root_path attr_reader :config - attr_reader :host attr_reader :box attr_accessor :vm attr_writer :ui @@ -134,6 +133,11 @@ module Vagrant end end + # Returns the host object associated with this environment. + def host + @host ||= Hosts::Base.load(self, config.vagrant.host) + end + # Returns the {Action} class for this environment which allows actions # to be executed (middleware chains) in the context of this environment. def actions @@ -178,7 +182,6 @@ module Vagrant load_root_path! load_config! load_home_directory! - load_host! load_box! load_config! self.class.check_virtualbox! @@ -265,11 +268,6 @@ module Vagrant end end - # Loads the host class for this environment. - def load_host! - @host = Hosts::Base.load(self, config.vagrant.host) - end - # Loads the specified box for this environment. def load_box! return unless root_path diff --git a/test/vagrant/action/vm/nfs_helpers_test.rb b/test/vagrant/action/vm/nfs_helpers_test.rb index 173b2403c..0456e9c4d 100644 --- a/test/vagrant/action/vm/nfs_helpers_test.rb +++ b/test/vagrant/action/vm/nfs_helpers_test.rb @@ -18,6 +18,7 @@ class NFSHelpersVMActionTest < Test::Unit::TestCase end should "not do anything if host is nil" do + @env.env.stubs(:host).returns(nil) assert_nothing_raised { @instance.clear_nfs_exports(@env) } end end diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index ff2f1612d..05c935486 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -234,6 +234,20 @@ class EnvironmentTest < Test::Unit::TestCase end end + context "accessing host" do + setup do + @env = mock_environment + end + + should "load the host once" do + result = mock("result") + Vagrant::Hosts::Base.expects(:load).with(@env, @env.config.vagrant.host).once.returns(result) + assert_equal result, @env.host + assert_equal result, @env.host + assert_equal result, @env.host + end + end + context "accessing actions" do setup do @env = mock_environment @@ -301,7 +315,6 @@ class EnvironmentTest < Test::Unit::TestCase @env.expects(:load_root_path!).once.in_sequence(call_seq) @env.expects(:load_config!).once.in_sequence(call_seq) @env.expects(:load_home_directory!).once.in_sequence(call_seq) - @env.expects(:load_host!).once.in_sequence(call_seq) @env.expects(:load_box!).once.in_sequence(call_seq) @env.expects(:load_config!).once.in_sequence(call_seq) @klass.expects(:check_virtualbox!).once.in_sequence(call_seq) @@ -505,19 +518,6 @@ class EnvironmentTest < Test::Unit::TestCase end end - context "loading host" do - setup do - @env = mock_environment - end - - should "load the host by calling the load method on Host::Base" do - result = mock("result") - Vagrant::Hosts::Base.expects(:load).with(@env, @env.config.vagrant.host).once.returns(result) - @env.load_host! - assert_equal result, @env.host - end - end - context "loading box" do setup do @box = mock("box")