Raise a useful exception when an invalid callable is ran

This commit is contained in:
Mitchell Hashimoto 2010-07-27 19:35:57 -07:00
parent 2e189eb2c4
commit d9331c9406
3 changed files with 23 additions and 0 deletions

View File

@ -45,6 +45,7 @@ module Vagrant
def run(callable, options=nil) def run(callable, options=nil)
callable = Builder.new.use(callable) if callable.kind_of?(Class) callable = Builder.new.use(callable) if callable.kind_of?(Class)
callable = self.class.actions[callable] if callable.kind_of?(Symbol) callable = self.class.actions[callable] if callable.kind_of?(Symbol)
raise Exceptions::UncallableAction.new(callable) if !callable
action_environment = Action::Environment.new(env) action_environment = Action::Environment.new(env)
action_environment.merge!(options || {}) action_environment.merge!(options || {})

View File

@ -0,0 +1,17 @@
module Vagrant
module Exceptions
# Raised when an action sequence is trying to be run for an uncallable
# action (not a lambda, middleware, or registered sequence).
class UncallableAction < Exception
def initialize(callable)
super()
@callable = callable
end
def to_s
@callable.inspect
end
end
end
end

View File

@ -31,6 +31,11 @@ class ActionTest < Test::Unit::TestCase
@klass.actions.clear @klass.actions.clear
end end
should "raise an exception if a nil action is given" do
assert_raises(Vagrant::Exceptions::UncallableAction) { @instance.run(nil) }
assert_raises(Vagrant::Exceptions::UncallableAction) { @instance.run(:dontexist) }
end
should "run the callable item with the proper context" do should "run the callable item with the proper context" do
callable = mock("callable") callable = mock("callable")
callable.expects(:call).with() do |env| callable.expects(:call).with() do |env|