Skip to content

Commit

Permalink
Warn when loading a plugin with arguments or a block if the plugin do…
Browse files Browse the repository at this point in the history
…es not accept arguments or block

Previously, if the plugin didn't have a load_dependencies or
configure method, any arguments or block passed to it would be
ignored.  That can cause problems if the plugin has a configure
or load_dependencies method added later that supports a block or
optional arguments.
  • Loading branch information
jeremyevans committed Jul 14, 2020
1 parent 3651c11 commit ad075de
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
@@ -1,3 +1,7 @@
= master

* Warn when loading a plugin with arguments or a block if the plugin does not accept arguments or block (jeremyevans)

= 3.34.0 (2020-07-14)

* Remove unnecessary conditionals (jeremyevans)
Expand Down
6 changes: 6 additions & 0 deletions lib/roda.rb
Expand Up @@ -272,6 +272,12 @@ def plugin(plugin, *args, &block)
raise RodaError, "Cannot add a plugin to a frozen Roda class" if frozen?
plugin = RodaPlugins.load_plugin(plugin) if plugin.is_a?(Symbol)
raise RodaError, "Invalid plugin type: #{plugin.class.inspect}" unless plugin.is_a?(Module)

if !plugin.respond_to?(:load_dependencies) && !plugin.respond_to?(:configure) && (!args.empty? || block)
# RODA4: switch from warning to error
RodaPlugins.warn("Plugin #{plugin} does not accept arguments or a block, but arguments or a block was passed when loading this. This will raise an error in Roda 4.")
end

plugin.load_dependencies(self, *args, &block) if plugin.respond_to?(:load_dependencies)
include(plugin::InstanceMethods) if defined?(plugin::InstanceMethods)
extend(plugin::ClassMethods) if defined?(plugin::ClassMethods)
Expand Down
7 changes: 7 additions & 0 deletions spec/plugin_spec.rb
Expand Up @@ -71,6 +71,13 @@ def self.configure(mod, prefix)
body.must_equal '1'
end

it "should warn if attempting to load a plugin with arguments or a block" do
Roda::RodaPlugins.register_plugin(:foo, Module.new)
proc{app.plugin :foo, 1}.must_output(nil, /does not accept arguments or a block/)
@app = nil
proc{app.plugin(:foo){}}.must_output(nil, /does not accept arguments or a block/)
end

it "should raise error if attempting to load an invalid plugin" do
proc{app(:banana)}.must_raise LoadError

Expand Down

0 comments on commit ad075de

Please sign in to comment.