diff --git a/bin/vagrant b/bin/vagrant index 4533b46ab..43df179b6 100755 --- a/bin/vagrant +++ b/bin/vagrant @@ -5,7 +5,7 @@ require 'vagrant/cli' env = Vagrant::Environment.new begin - Vagrant::CLI.start(ARGV, :env => env.load!) + Vagrant::CLI.start(ARGV, :env => env) rescue Vagrant::Errors::VagrantError => e opts = { :_translate => false, :_prefix => false } env.ui.error e.message, opts if e.message diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 64c9782e4..c675253ea 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -14,7 +14,6 @@ module Vagrant attr_reader :vm_name # The name of the VM (internal name) which this environment represents attr_reader :cwd - attr_reader :config attr_reader :box attr_accessor :vm attr_writer :ui @@ -84,6 +83,7 @@ module Vagrant # Returns the VMs associated with this environment. def vms + load! if !loaded? @vms ||= {} end @@ -158,6 +158,14 @@ module Vagrant @local_data ||= DataStore.new(dotfile_path) end + # The configuration object represented by this environment. This + # will trigger the environment to load if it hasn't loaded yet (see + # {#load!}). + def config + load! if !loaded? + @config + end + # Accesses the logger for Vagrant. This logger is a _detailed_ # logger which should be used to log internals only. For outward # facing information, use {#ui}. @@ -195,13 +203,13 @@ module Vagrant # such as `vm`, `config`, etc. on this environment. The order this # method calls its other methods is very particular. def load! + @loaded = true self.class.check_virtualbox! load_config! load_home_directory! load_box! load_config! load_vm! - @loaded = true self end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 3c41b53f6..416d63fbf 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -61,6 +61,7 @@ en: is also printed below for convenience. %{file} + virtualbox_invalid_ose: |- Vagrant has detected you're using an OSE ("Open Source Edition") of VirtualBox. Vagrant currently doesn't support any of the OSE editions due to slight API diff --git a/test/test_helper.rb b/test/test_helper.rb index 1114879a4..07dbb7f26 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -21,6 +21,7 @@ class Test::Unit::TestCase # Mocks an environment, setting it up with the given config. def mock_environment environment = Vagrant::Environment.new + environment.instance_variable_set(:@loaded, true) Vagrant::Config.reset!(environment) diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index 98f2e4ff2..899e4d45f 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -280,6 +280,34 @@ class EnvironmentTest < Test::Unit::TestCase end end + context "accessing the configuration" do + should "load the environment if its not already loaded" do + env = @klass.new(:cwd => vagrantfile) + env.expects(:load!).once + env.config + end + + should "not load the environment if its already loaded" do + env = vagrant_env + env.expects(:load!).never + env.config + end + end + + context "accessing the VMs hash" do + should "load the environment if its not already loaded" do + env = @klass.new(:cwd => vagrantfile) + env.expects(:load!).once + env.vms + end + + should "not load the environment if its already loaded" do + env = vagrant_env + env.expects(:load!).never + env.vms + end + end + context "loading" do setup do @env = mock_environment diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index 690c21809..6c3482a20 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -2,12 +2,7 @@ require "test_helper" class VMTest < Test::Unit::TestCase setup do - @mock_vm = mock("vm") - - @persisted_vm = mock("persisted_vm") - - @env = mock_environment - @env.stubs(:vm).returns(@persisted_vm) + @env = vagrant_env Net::SSH.stubs(:start) end @@ -22,7 +17,7 @@ class VMTest < Test::Unit::TestCase should "return a Vagrant::VM object for that VM if found" do VirtualBox::VM.expects(:find).with("foo").returns("bar") - result = Vagrant::VM.find("foo", mock_environment) + result = Vagrant::VM.find("foo", @env) assert result.is_a?(Vagrant::VM) assert_equal "bar", result.vm end @@ -31,6 +26,7 @@ class VMTest < Test::Unit::TestCase context "vagrant VM instance" do setup do @vm_name = "foo" + @mock_vm = mock("vm") @vm = Vagrant::VM.new(:env => @env, :vm => @mock_vm, :vm_name => @vm_name) @mock_vm.stubs(:uuid).returns("foo") end @@ -59,7 +55,7 @@ class VMTest < Test::Unit::TestCase end should "add the VM to the active list" do - assert !@env.local_data[:active] + assert @env.local_data.empty? @vm.vm = @raw_vm assert_equal @raw_vm.uuid, @env.local_data[:active][@vm.name.to_sym] end @@ -69,7 +65,10 @@ class VMTest < Test::Unit::TestCase assert @env.local_data[:active].has_key?(@vm.name.to_sym) # sanity @vm.vm = nil - assert !@env.local_data[:active].has_key?(@vm.name.to_sym) + + # This becomes empty because vm= will commit the local data which + # actually prunes out the empty values. + assert @env.local_data.empty? end end @@ -82,17 +81,12 @@ class VMTest < Test::Unit::TestCase Vagrant::SSH.stubs(:new).returns(@ssh) end - should "load it the first time" do + should "load it the first time, and only load it once" do Vagrant::SSH.expects(:new).with(@vm.env).once.returns(@ssh) @vm.ssh @vm.ssh @vm.ssh end - - should "use the same value once its loaded" do - result = @vm.ssh - assert_equal result, @vm.ssh - end end context "loading associated system" do