From 4e732631d83556cc8fb90c06d67772b128d94995 Mon Sep 17 00:00:00 2001 From: John Bender Date: Sun, 25 Jul 2010 22:29:13 -0700 Subject: [PATCH] tests for begin rescue --- lib/vagrant/action/warden.rb | 4 +-- test/vagrant/action/warden_test.rb | 39 +++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/action/warden.rb b/lib/vagrant/action/warden.rb index a380eeb09..b054a58bd 100644 --- a/lib/vagrant/action/warden.rb +++ b/lib/vagrant/action/warden.rb @@ -19,8 +19,8 @@ module Vagrant end def begin_rescue(env) - @stack.reverse.each do |action| - action.rescue(env) if actions.respond_to?(:rescue) + @stack.reverse.each do |act| + act.rescue(env) if act.respond_to?(:rescue) end end diff --git a/test/vagrant/action/warden_test.rb b/test/vagrant/action/warden_test.rb index b3629e371..f4f63a15c 100644 --- a/test/vagrant/action/warden_test.rb +++ b/test/vagrant/action/warden_test.rb @@ -19,7 +19,7 @@ class ActionWardenTest < Test::Unit::TestCase context "setting up middleware" do should "make non-classes lambdas" do - env = Vagrant::Action::Environment.new(nil) + env = new_env env.expects(:foo).once func = lambda { |x| x.foo } @@ -53,9 +53,46 @@ class ActionWardenTest < Test::Unit::TestCase @instance.actions << action @instance.call(new_env) end + + should "begin rescue on environment error" do + env = new_env + env.error!(:foo) + @instance.expects(:begin_rescue) + @instance.actions << lambda {} + @instance.actions.first.expects(:call).never + @instance.call(env) + end + end + + context "rescue" do + should "call rescue on all items in the stack" do + mock_action = rescueable_mock("action") + mock_action.expects(:rescue).times(2) + @instance.stack = [mock_action, mock_action] + @instance.begin_rescue(new_env) + end + + should "call rescue 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(:rescue).in_sequence(seq) + first_mock_action.expects(:rescue).in_sequence(seq) + + @instance.begin_rescue(new_env) + end end def new_env Vagrant::Action::Environment.new(nil) end + + def rescueable_mock(name) + mock_action = mock(name) + mock_action.stubs(:respond_to?).with(:rescue).returns(true) + mock_action + end end