tests added for basic warden call forwarding
This commit is contained in:
parent
2c1da9566c
commit
114511742e
|
@ -1,11 +1,11 @@
|
||||||
module Vagrant
|
module Vagrant
|
||||||
class Action
|
class Action
|
||||||
class Warden
|
class Warden
|
||||||
attr_accessor :actions
|
attr_accessor :actions, :stack
|
||||||
|
|
||||||
def initialize(middleware, env)
|
def initialize(actions, env)
|
||||||
@stack = []
|
@stack = []
|
||||||
@actions = middleware.map { |m| finalize_middleware(m, env) }.reverse
|
@actions = actions.map { |m| finalize_action(m, env) }.reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
|
@ -13,11 +13,11 @@ module Vagrant
|
||||||
@stack.push(@actions.pop).last.call(env)
|
@stack.push(@actions.pop).last.call(env)
|
||||||
end
|
end
|
||||||
|
|
||||||
def finalize_middleware(middleware, env)
|
def finalize_action(action, env)
|
||||||
klass, args, block = middleware
|
klass, args, block = action
|
||||||
|
|
||||||
if klass.is_a?(Class)
|
if klass.is_a?(Class)
|
||||||
# A middleware klass which is to be instantiated with the
|
# A action klass which is to be instantiated with the
|
||||||
# app, env, and any arguments given
|
# app, env, and any arguments given
|
||||||
klass.new(self, env, *args, &block)
|
klass.new(self, env, *args, &block)
|
||||||
elsif klass.respond_to?(:call)
|
elsif klass.respond_to?(:call)
|
||||||
|
@ -28,7 +28,7 @@ module Vagrant
|
||||||
self.call(e)
|
self.call(e)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise "Invalid middleware: #{middleware.inspect}"
|
raise "Invalid action: #{action.inspect}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class ActionWardenTest < Test::Unit::TestCase
|
||||||
|
setup do
|
||||||
|
@klass = Vagrant::Action::Warden
|
||||||
|
@instance = @klass.new([], {})
|
||||||
|
end
|
||||||
|
|
||||||
|
context "initializing" do
|
||||||
|
should "finalize the middleware" do
|
||||||
|
middleware = [1,2,3]
|
||||||
|
middleware.each do |m|
|
||||||
|
@klass.any_instance.expects(:finalize_action).with(m, {}).returns(m)
|
||||||
|
end
|
||||||
|
@warden = @klass.new(middleware, {})
|
||||||
|
assert_equal @warden.actions, [3,2,1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "setting up middleware" do
|
||||||
|
should "make non-classes lambdas" do
|
||||||
|
env = Vagrant::Action::Environment.new(nil)
|
||||||
|
env.expects(:foo).once
|
||||||
|
|
||||||
|
func = lambda { |x| x.foo }
|
||||||
|
@instance.finalize_action(func, env).call(env)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "raise exception if given invalid middleware" do
|
||||||
|
assert_raises(RuntimeError) {
|
||||||
|
@instance.finalize_action(7, nil)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "calling" do
|
||||||
|
should "return if there are no actions to execute" do
|
||||||
|
@instance.actions.expects(:pop).never
|
||||||
|
assert !@instance.call({})
|
||||||
|
end
|
||||||
|
|
||||||
|
should "move the last action to the front of the stack" do
|
||||||
|
@instance.actions << lambda {}
|
||||||
|
assert @instance.stack.empty?
|
||||||
|
@instance.call({})
|
||||||
|
assert !@instance.stack.empty?
|
||||||
|
assert @instance.actions.empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
should "call the next action" do
|
||||||
|
action = mock('action')
|
||||||
|
action.expects(:call).with({})
|
||||||
|
@instance.actions << action
|
||||||
|
@instance.call({})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue