From d550cc3a763101cb21ad4806d0ed4d9ac8960eb0 Mon Sep 17 00:00:00 2001 From: John Bender Date: Mon, 8 Mar 2010 22:20:17 -0800 Subject: [PATCH] dup action exception and tests --- lib/vagrant/actions/runner.rb | 12 +++++++++++- test/vagrant/actions/runner_test.rb | 30 +++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/actions/runner.rb b/lib/vagrant/actions/runner.rb index 6ed23b2ef..920685cc3 100644 --- a/lib/vagrant/actions/runner.rb +++ b/lib/vagrant/actions/runner.rb @@ -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 \ No newline at end of file +end diff --git a/test/vagrant/actions/runner_test.rb b/test/vagrant/actions/runner_test.rb index 05740bfd9..5d63f970d 100644 --- a/test/vagrant/actions/runner_test.rb +++ b/test/vagrant/actions/runner_test.rb @@ -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