Make some minor changes to warden to reduce the array reversals
This commit is contained in:
parent
efbfd335ad
commit
354a82a177
|
@ -6,24 +6,24 @@ module Vagrant
|
||||||
|
|
||||||
def initialize(actions, env)
|
def initialize(actions, env)
|
||||||
@stack = []
|
@stack = []
|
||||||
@actions = actions.map { |m| finalize_action(m, env) }.reverse
|
@actions = actions.map { |m| finalize_action(m, env) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
return if @actions.empty?
|
return if @actions.empty?
|
||||||
|
|
||||||
# If the previous action passes and environment error on
|
# If the previous action passes and environment error on
|
||||||
@stack.push(@actions.pop).last.call(env) unless env.error?
|
@stack.unshift(@actions.shift).first.call(env) unless env.error?
|
||||||
|
|
||||||
# if the call action returned prematurely with an error
|
# if the call action returned prematurely with an error
|
||||||
begin_rescue(env) if env.error?
|
begin_rescue(env) if env.error?
|
||||||
end
|
end
|
||||||
|
|
||||||
def begin_rescue(env)
|
def begin_rescue(env)
|
||||||
@stack.reverse.each do |act|
|
@stack.each do |act|
|
||||||
act.recover(env) if act.respond_to?(:recover)
|
act.recover(env) if act.respond_to?(:recover)
|
||||||
end
|
end
|
||||||
|
|
||||||
exit if env.interrupted?
|
exit if env.interrupted?
|
||||||
|
|
||||||
# Erroneous environment resulted. Properly display error message.
|
# Erroneous environment resulted. Properly display error message.
|
||||||
|
|
|
@ -14,7 +14,7 @@ class ActionWardenTest < Test::Unit::TestCase
|
||||||
@klass.any_instance.expects(:finalize_action).with(m, {}).returns(m)
|
@klass.any_instance.expects(:finalize_action).with(m, {}).returns(m)
|
||||||
end
|
end
|
||||||
@warden = @klass.new(middleware, new_env)
|
@warden = @klass.new(middleware, new_env)
|
||||||
assert_equal @warden.actions, [3,2,1]
|
assert_equal @warden.actions, [1,2,3]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -92,21 +92,11 @@ class ActionWardenTest < Test::Unit::TestCase
|
||||||
|
|
||||||
context "recover" do
|
context "recover" do
|
||||||
should "call recover on all items in the stack" do
|
should "call recover on all items in the stack" do
|
||||||
mock_action = rescueable_mock("action")
|
seq = sequence("sequence")
|
||||||
mock_action.expects(:recover).times(2)
|
@instance.stack = [rescueable_mock("action"), rescueable_mock("another")]
|
||||||
@instance.stack = [mock_action, mock_action]
|
@instance.stack.each do |action|
|
||||||
@instance.begin_rescue(new_env)
|
action.expects(:recover).with(new_env).in_sequence(seq)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "call recover on stack in reversed order" do
|
|
||||||
seq = sequence("reverse")
|
|
||||||
first_mock_action = rescueable_mock("first")
|
|
||||||
second_mock_action = rescueable_mock("second")
|
|
||||||
|
|
||||||
@instance.stack = [first_mock_action, second_mock_action]
|
|
||||||
|
|
||||||
second_mock_action.expects(:recover).in_sequence(seq)
|
|
||||||
first_mock_action.expects(:recover).in_sequence(seq)
|
|
||||||
|
|
||||||
@instance.begin_rescue(new_env)
|
@instance.begin_rescue(new_env)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue