dup action exception and tests

This commit is contained in:
John Bender 2010-03-08 22:20:17 -08:00
parent c1b03da528
commit d550cc3a76
2 changed files with 39 additions and 3 deletions

View File

@ -69,11 +69,15 @@ module Vagrant
# to execute a single action on an instance. The syntax for executing a # to execute a single action on an instance. The syntax for executing a
# single method on an instance is the same as the {execute!} class method. # single method on an instance is the same as the {execute!} class method.
def execute!(single_action=nil, *args) def execute!(single_action=nil, *args)
if single_action if single_action
actions.clear actions.clear
add_action(single_action, *args) add_action(single_action, *args)
end end
# Raising it here might be too late and hard debug where the actions are comming from (meta actions)
raise DuplicateActionException.new if action_klasses.uniq.size < action_klasses.size
# Call the prepare method on each once its # Call the prepare method on each once its
# initialized, then call the execute! method # initialized, then call the execute! method
begin begin
@ -123,6 +127,12 @@ module Vagrant
end end
results results
end end
def action_klasses
actions.map { |a| a.class }
end
end end
class DuplicateActionException < Exception; end
end end
end end

View File

@ -1,8 +1,8 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper') require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ActionRunnerTest < Test::Unit::TestCase class ActionRunnerTest < Test::Unit::TestCase
def mock_fake_action def mock_fake_action(action_klass = nil, runner = nil)
action = mock("action") action = action_klass ? action_klass.new(runner) : mock("action")
action.stubs(:prepare) action.stubs(:prepare)
action.stubs(:execute!) action.stubs(:execute!)
action.stubs(:cleanup) action.stubs(:cleanup)
@ -129,6 +129,7 @@ class ActionRunnerTest < Test::Unit::TestCase
context "instance method execute" do context "instance method execute" do
setup do setup do
@runner = Vagrant::Actions::Runner.new @runner = Vagrant::Actions::Runner.new
@runner.stubs(:action_klasses).returns([Vagrant::Actions::Base])
end end
should "clear the actions and run a single action if given to execute!" do should "clear the actions and run a single action if given to execute!" do
@ -233,4 +234,29 @@ class ActionRunnerTest < Test::Unit::TestCase
assert @runner.actions.empty? assert @runner.actions.empty?
end end
end end
context "duplicate action exceptions" do
setup do
@runner = Vagrant::Actions::Runner.new
end
should "should be raised when a duplicate is added" do
action = mock_fake_action
2.times {@runner.actions << action }
assert_raise Vagrant::Actions::DuplicateActionException do
@runner.execute!
end
end
should "should not be raise when no duplicate actions are present" do
@runner.actions << mock_fake_action(Vagrant::Actions::Base, @runner)
@runner.actions << mock_fake_action(Vagrant::Actions::VM::Halt, @runner)
assert_nothing_raised { @runner.execute! }
end
should "should not raise when a single action is specified" do
assert_nothing_raised { @runner.execute!(Vagrant::Actions::Base) }
end
end
end end