Lazy load the environment "actions." Removes one more load from the environment load chain

This commit is contained in:
Mitchell Hashimoto 2010-09-03 00:26:14 -07:00
parent f72979df79
commit 01df63ef6e
2 changed files with 26 additions and 49 deletions

View File

@ -13,13 +13,12 @@ module Vagrant
attr_reader :parent # Parent environment (in the case of multi-VMs)
attr_reader :vm_name # The name of the VM (internal name) which this environment represents
attr_writer :cwd
attr_reader :cwd
attr_reader :root_path
attr_reader :config
attr_reader :host
attr_reader :box
attr_accessor :vm
attr_reader :actions
attr_writer :ui
#---------------------------------------------------------------
@ -45,16 +44,14 @@ module Vagrant
end
def initialize(opts=nil)
defaults = {
opts = {
:parent => nil,
:vm_name => nil,
:vm => nil,
:cwd => nil
}
:cwd => Dir.pwd
}.merge(opts || {})
opts = defaults.merge(opts || {})
defaults.each do |key, value|
opts.each do |key, value|
instance_variable_set("@#{key}".to_sym, opts[key])
end
end
@ -63,14 +60,6 @@ module Vagrant
# Helpers
#---------------------------------------------------------------
# Specifies the "current working directory" for this environment.
# This is vital in determining the root path and therefore the
# dotfile, rootpath vagrantfile, etc. This defaults to the
# actual cwd (`Dir.pwd`).
def cwd
@cwd || Dir.pwd
end
# The path to the `dotfile`, which contains the persisted UUID of
# the VM if it exists.
def dotfile_path
@ -145,6 +134,12 @@ module Vagrant
end
end
# Returns the {Action} class for this environment which allows actions
# to be executed (middleware chains) in the context of this environment.
def actions
@actions ||= Action.new(self)
end
# Loads on initial access and reads data from the global data store.
# The global data store is global to Vagrant everywhere (in every environment),
# so it can be used to store system-wide information. Note that "system-wide"
@ -188,7 +183,6 @@ module Vagrant
load_config!
self.class.check_virtualbox!
load_vm!
load_actions!
self
end
@ -312,12 +306,5 @@ module Vagrant
vms[name] = Vagrant::VM.new(:vm_name => name, :env => self)
end
end
# Loads the instance of {Action} for this environment. This allows
# users of the instance to run action sequences in the context of
# this environment.
def load_actions!
@actions = Action.new(self)
end
end
end

View File

@ -75,17 +75,6 @@ class EnvironmentTest < Test::Unit::TestCase
@env = mock_environment
end
context "cwd" do
should "default to Dir.pwd" do
assert_equal Dir.pwd, @env.cwd
end
should "return cwd if set" do
@env.cwd = "foo"
assert_equal "foo", @env.cwd
end
end
context "dotfile path" do
setup do
@env.stubs(:root_path).returns("foo")
@ -245,6 +234,20 @@ class EnvironmentTest < Test::Unit::TestCase
end
end
context "accessing actions" do
setup do
@env = mock_environment
end
should "initialize the Action object with the given environment" do
result = mock("result")
Vagrant::Action.expects(:new).with(@env).returns(result).once
assert_equal result, @env.actions
assert_equal result, @env.actions
assert_equal result, @env.actions
end
end
context "global data" do
setup do
@env = mock_environment
@ -303,14 +306,13 @@ class EnvironmentTest < Test::Unit::TestCase
@env.expects(:load_config!).once.in_sequence(call_seq)
@klass.expects(:check_virtualbox!).once.in_sequence(call_seq)
@env.expects(:load_vm!).once.in_sequence(call_seq)
@env.expects(:load_actions!).once.in_sequence(call_seq)
assert_equal @env, @env.load!
end
end
context "loading the root path" do
setup do
@env.cwd = "/foo"
@env.stubs(:cwd).returns("/foo")
end
should "default the path to the cwd instance var if nil" do
@ -626,17 +628,5 @@ class EnvironmentTest < Test::Unit::TestCase
end
end
context "loading actions" do
setup do
@env = mock_environment
end
should "initialize the Action object with the given environment" do
result = mock("result")
Vagrant::Action.expects(:new).with(@env).returns(result)
@env.load_actions!
assert_equal result, @env.actions
end
end
end
end