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
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

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 :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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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