From 354a82a177073afa7b89a2956a3a0852a3b007f3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 27 Aug 2010 23:53:54 -0700 Subject: [PATCH] Make some minor changes to warden to reduce the array reversals --- lib/vagrant/action/warden.rb | 8 ++++---- test/vagrant/action/warden_test.rb | 22 ++++++---------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/lib/vagrant/action/warden.rb b/lib/vagrant/action/warden.rb index 7027e8f28..b1c69cc12 100644 --- a/lib/vagrant/action/warden.rb +++ b/lib/vagrant/action/warden.rb @@ -6,24 +6,24 @@ module Vagrant def initialize(actions, env) @stack = [] - @actions = actions.map { |m| finalize_action(m, env) }.reverse + @actions = actions.map { |m| finalize_action(m, env) } end def call(env) return if @actions.empty? # 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 begin_rescue(env) if env.error? end def begin_rescue(env) - @stack.reverse.each do |act| + @stack.each do |act| act.recover(env) if act.respond_to?(:recover) end - + exit if env.interrupted? # Erroneous environment resulted. Properly display error message. diff --git a/test/vagrant/action/warden_test.rb b/test/vagrant/action/warden_test.rb index ccdacb502..c4b81d6ba 100644 --- a/test/vagrant/action/warden_test.rb +++ b/test/vagrant/action/warden_test.rb @@ -14,7 +14,7 @@ class ActionWardenTest < Test::Unit::TestCase @klass.any_instance.expects(:finalize_action).with(m, {}).returns(m) end @warden = @klass.new(middleware, new_env) - assert_equal @warden.actions, [3,2,1] + assert_equal @warden.actions, [1,2,3] end end @@ -92,21 +92,11 @@ class ActionWardenTest < Test::Unit::TestCase context "recover" do should "call recover on all items in the stack" do - mock_action = rescueable_mock("action") - mock_action.expects(:recover).times(2) - @instance.stack = [mock_action, mock_action] - @instance.begin_rescue(new_env) - 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) + seq = sequence("sequence") + @instance.stack = [rescueable_mock("action"), rescueable_mock("another")] + @instance.stack.each do |action| + action.expects(:recover).with(new_env).in_sequence(seq) + end @instance.begin_rescue(new_env) end