StackedProcRunner abstraction which will be used for VM customization (in addition to configuration)

This commit is contained in:
Mitchell Hashimoto 2010-03-15 14:19:28 -07:00
parent 1f216da06b
commit 130d584322
3 changed files with 77 additions and 1 deletions

View File

@ -8,7 +8,7 @@ PROJECT_ROOT = File.join(libdir, '..') unless defined?(PROJECT_ROOT)
end
# The vagrant specific files which must be loaded prior to the rest
%w{vagrant/util vagrant/actions/base vagrant/downloaders/base vagrant/actions/collection
%w{vagrant/util vagrant/stacked_proc_runner vagrant/actions/base vagrant/downloaders/base vagrant/actions/collection
vagrant/actions/runner vagrant/config vagrant/provisioners/base vagrant/provisioners/chef}.each do |f|
require File.expand_path(f, libdir)
end

View File

@ -0,0 +1,33 @@
module Vagrant
# Represents the "stacked proc runner" behavior which is used a
# couple places within Vagrant. This allows procs to "stack" on
# each other, then all execute in a single action. An example of
# its uses can be seen in the {Config} class.
module StackedProcRunner
# Returns the proc stack. This should always be called as the
# accessor of the stack. The instance variable itself should _never_
# be used.
#
# @return [Array<Proc>]
def proc_stack
@_proc_stack ||= []
end
# Adds (pushes) a proc to the stack. The actual proc added here is
# not executed, but merely stored.
#
# @param [Proc] block
def push_proc(&block)
proc_stack << block
end
# Executes all the procs on the stack, passing in the given arguments.
# The stack is not cleared afterwords. It is up to the user of this
# mixin to clear the stack by calling `proc_stack.clear`.
def run_procs!(*args)
proc_stack.each do |proc|
proc.call(*args)
end
end
end
end

View File

@ -0,0 +1,43 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class StackedProcRunnerTest < Test::Unit::TestCase
class TestClass
include Vagrant::StackedProcRunner
end
setup do
@instance = TestClass.new
@instance.proc_stack.clear
end
should "not run the procs right away" do
obj = mock("obj")
obj.expects(:foo).never
@instance.push_proc { |config| obj.foo }
@instance.push_proc { |config| obj.foo }
@instance.push_proc { |config| obj.foo }
end
should "run the blocks when run_procs! is ran" do
obj = mock("obj")
obj.expects(:foo).times(2)
@instance.push_proc { obj.foo }
@instance.push_proc { obj.foo }
@instance.run_procs!
end
should "run the blocks with the same arguments" do
passed_config = mock("config")
@instance.push_proc { |config| assert passed_config.equal?(config) }
@instance.push_proc { |config| assert passed_config.equal?(config) }
@instance.run_procs!(passed_config)
end
should "not clear the blocks after running" do
obj = mock("obj")
obj.expects(:foo).times(2)
@instance.push_proc { obj.foo }
@instance.run_procs!
@instance.run_procs!
end
end