Environment is lazy loaded for `vagrant` binary.

This commit is contained in:
Mitchell Hashimoto 2010-09-04 11:10:20 -07:00
parent f24094bba8
commit 53aaa4f264
6 changed files with 50 additions and 18 deletions

View File

@ -5,7 +5,7 @@ require 'vagrant/cli'
env = Vagrant::Environment.new env = Vagrant::Environment.new
begin begin
Vagrant::CLI.start(ARGV, :env => env.load!) Vagrant::CLI.start(ARGV, :env => env)
rescue Vagrant::Errors::VagrantError => e rescue Vagrant::Errors::VagrantError => e
opts = { :_translate => false, :_prefix => false } opts = { :_translate => false, :_prefix => false }
env.ui.error e.message, opts if e.message env.ui.error e.message, opts if e.message

View File

@ -14,7 +14,6 @@ module Vagrant
attr_reader :vm_name # The name of the VM (internal name) which this environment represents attr_reader :vm_name # The name of the VM (internal name) which this environment represents
attr_reader :cwd attr_reader :cwd
attr_reader :config
attr_reader :box attr_reader :box
attr_accessor :vm attr_accessor :vm
attr_writer :ui attr_writer :ui
@ -84,6 +83,7 @@ module Vagrant
# Returns the VMs associated with this environment. # Returns the VMs associated with this environment.
def vms def vms
load! if !loaded?
@vms ||= {} @vms ||= {}
end end
@ -158,6 +158,14 @@ module Vagrant
@local_data ||= DataStore.new(dotfile_path) @local_data ||= DataStore.new(dotfile_path)
end 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_ # Accesses the logger for Vagrant. This logger is a _detailed_
# logger which should be used to log internals only. For outward # logger which should be used to log internals only. For outward
# facing information, use {#ui}. # facing information, use {#ui}.
@ -195,13 +203,13 @@ module Vagrant
# such as `vm`, `config`, etc. on this environment. The order this # such as `vm`, `config`, etc. on this environment. The order this
# method calls its other methods is very particular. # method calls its other methods is very particular.
def load! def load!
@loaded = true
self.class.check_virtualbox! self.class.check_virtualbox!
load_config! load_config!
load_home_directory! load_home_directory!
load_box! load_box!
load_config! load_config!
load_vm! load_vm!
@loaded = true
self self
end end

View File

@ -61,6 +61,7 @@ en:
is also printed below for convenience. is also printed below for convenience.
%{file} %{file}
virtualbox_invalid_ose: |- virtualbox_invalid_ose: |-
Vagrant has detected you're using an OSE ("Open Source Edition") of VirtualBox. 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 Vagrant currently doesn't support any of the OSE editions due to slight API

View File

@ -21,6 +21,7 @@ class Test::Unit::TestCase
# Mocks an environment, setting it up with the given config. # Mocks an environment, setting it up with the given config.
def mock_environment def mock_environment
environment = Vagrant::Environment.new environment = Vagrant::Environment.new
environment.instance_variable_set(:@loaded, true)
Vagrant::Config.reset!(environment) Vagrant::Config.reset!(environment)

View File

@ -280,6 +280,34 @@ class EnvironmentTest < Test::Unit::TestCase
end end
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 context "loading" do
setup do setup do
@env = mock_environment @env = mock_environment

View File

@ -2,12 +2,7 @@ require "test_helper"
class VMTest < Test::Unit::TestCase class VMTest < Test::Unit::TestCase
setup do setup do
@mock_vm = mock("vm") @env = vagrant_env
@persisted_vm = mock("persisted_vm")
@env = mock_environment
@env.stubs(:vm).returns(@persisted_vm)
Net::SSH.stubs(:start) Net::SSH.stubs(:start)
end end
@ -22,7 +17,7 @@ class VMTest < Test::Unit::TestCase
should "return a Vagrant::VM object for that VM if found" do should "return a Vagrant::VM object for that VM if found" do
VirtualBox::VM.expects(:find).with("foo").returns("bar") 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 result.is_a?(Vagrant::VM)
assert_equal "bar", result.vm assert_equal "bar", result.vm
end end
@ -31,6 +26,7 @@ class VMTest < Test::Unit::TestCase
context "vagrant VM instance" do context "vagrant VM instance" do
setup do setup do
@vm_name = "foo" @vm_name = "foo"
@mock_vm = mock("vm")
@vm = Vagrant::VM.new(:env => @env, :vm => @mock_vm, :vm_name => @vm_name) @vm = Vagrant::VM.new(:env => @env, :vm => @mock_vm, :vm_name => @vm_name)
@mock_vm.stubs(:uuid).returns("foo") @mock_vm.stubs(:uuid).returns("foo")
end end
@ -59,7 +55,7 @@ class VMTest < Test::Unit::TestCase
end end
should "add the VM to the active list" do should "add the VM to the active list" do
assert !@env.local_data[:active] assert @env.local_data.empty?
@vm.vm = @raw_vm @vm.vm = @raw_vm
assert_equal @raw_vm.uuid, @env.local_data[:active][@vm.name.to_sym] assert_equal @raw_vm.uuid, @env.local_data[:active][@vm.name.to_sym]
end end
@ -69,7 +65,10 @@ class VMTest < Test::Unit::TestCase
assert @env.local_data[:active].has_key?(@vm.name.to_sym) # sanity assert @env.local_data[:active].has_key?(@vm.name.to_sym) # sanity
@vm.vm = nil @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
end end
@ -82,17 +81,12 @@ class VMTest < Test::Unit::TestCase
Vagrant::SSH.stubs(:new).returns(@ssh) Vagrant::SSH.stubs(:new).returns(@ssh)
end 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) Vagrant::SSH.expects(:new).with(@vm.env).once.returns(@ssh)
@vm.ssh @vm.ssh
@vm.ssh @vm.ssh
@vm.ssh @vm.ssh
end end
should "use the same value once its loaded" do
result = @vm.ssh
assert_equal result, @vm.ssh
end
end end
context "loading associated system" do context "loading associated system" do