From 114511742e6a2f0eac9b2011696b1237eed60d56 Mon Sep 17 00:00:00 2001 From: John Bender Date: Sat, 24 Jul 2010 00:24:26 -0700 Subject: [PATCH] tests added for basic warden call forwarding --- lib/vagrant/action/warden.rb | 14 ++++---- test/vagrant/action/warden_test.rb | 57 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 test/vagrant/action/warden_test.rb diff --git a/lib/vagrant/action/warden.rb b/lib/vagrant/action/warden.rb index 18159a3b5..aa13afce5 100644 --- a/lib/vagrant/action/warden.rb +++ b/lib/vagrant/action/warden.rb @@ -1,11 +1,11 @@ module Vagrant class Action class Warden - attr_accessor :actions + attr_accessor :actions, :stack - def initialize(middleware, env) + def initialize(actions, env) @stack = [] - @actions = middleware.map { |m| finalize_middleware(m, env) }.reverse + @actions = actions.map { |m| finalize_action(m, env) }.reverse end def call(env) @@ -13,11 +13,11 @@ module Vagrant @stack.push(@actions.pop).last.call(env) end - def finalize_middleware(middleware, env) - klass, args, block = middleware + def finalize_action(action, env) + klass, args, block = action 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 klass.new(self, env, *args, &block) elsif klass.respond_to?(:call) @@ -28,7 +28,7 @@ module Vagrant self.call(e) end else - raise "Invalid middleware: #{middleware.inspect}" + raise "Invalid action: #{action.inspect}" end end end diff --git a/test/vagrant/action/warden_test.rb b/test/vagrant/action/warden_test.rb new file mode 100644 index 000000000..41e5e79f0 --- /dev/null +++ b/test/vagrant/action/warden_test.rb @@ -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