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
# single method on an instance is the same as the {execute!} class method.
def execute!(single_action=nil, *args)
if single_action
actions.clear
add_action(single_action, *args)
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
# initialized, then call the execute! method
begin
@ -123,6 +127,12 @@ module Vagrant
end
results
end
def action_klasses
actions.map { |a| a.class }
end
end
class DuplicateActionException < Exception; end
end
end
end

View File

@ -1,8 +1,8 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ActionRunnerTest < Test::Unit::TestCase
def mock_fake_action
action = mock("action")
def mock_fake_action(action_klass = nil, runner = nil)
action = action_klass ? action_klass.new(runner) : mock("action")
action.stubs(:prepare)
action.stubs(:execute!)
action.stubs(:cleanup)
@ -129,6 +129,7 @@ class ActionRunnerTest < Test::Unit::TestCase
context "instance method execute" do
setup do
@runner = Vagrant::Actions::Runner.new
@runner.stubs(:action_klasses).returns([Vagrant::Actions::Base])
end
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?
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