diff options
author | Claire McQuin <claire@opscode.com> | 2013-12-18 09:55:49 -0800 |
---|---|---|
committer | Claire McQuin <claire@opscode.com> | 2013-12-19 10:46:44 -0800 |
commit | 1caba374d347c40cc3071b4f45041c8b9de8a786 (patch) | |
tree | 9248f7c6dfc92474148aa5a6ac0a94a0bba4c360 | |
parent | 70db970d8ca954c0fc470ae006f7f895935a41e0 (diff) | |
download | ohai-1caba374d347c40cc3071b4f45041c8b9de8a786.tar.gz |
tune syntax error message output, handle invalid plugin names that aren't syntax errors
-rw-r--r-- | lib/ohai/dsl/plugin.rb | 6 | ||||
-rw-r--r-- | lib/ohai/exception.rb | 1 | ||||
-rw-r--r-- | lib/ohai/loader.rb | 2 | ||||
-rw-r--r-- | spec/unit/dsl/plugin_spec.rb | 20 | ||||
-rw-r--r-- | spec/unit/loader_spec.rb | 24 |
5 files changed, 53 insertions, 0 deletions
diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb index 6ebc3b31..1d23d7ed 100644 --- a/lib/ohai/dsl/plugin.rb +++ b/lib/ohai/dsl/plugin.rb @@ -27,6 +27,10 @@ module Ohai # For plugin namespacing module NamedPlugin + def self.valid_name?(name) + name.is_a?(Symbol) && name.to_s.match(/^[^A-Z]|_/).nil? + end + # dealing with ruby 1.8 if Module.method(:const_defined?).arity == 1 def self.strict_const_defined?(const) @@ -40,6 +44,8 @@ module Ohai end def self.plugin(name, &block) + raise Ohai::Exceptions::InvalidPluginName, "#{name} is not a valid plugin name. A valid plugin name is a symbol which begins with a capital letter and contains no underscores" unless NamedPlugin.valid_name?(name) + plugin = nil if NamedPlugin.strict_const_defined?(name) diff --git a/lib/ohai/exception.rb b/lib/ohai/exception.rb index 144a70df..4e8ddfaf 100644 --- a/lib/ohai/exception.rb +++ b/lib/ohai/exception.rb @@ -19,6 +19,7 @@ module Ohai module Exceptions class Exec < RuntimeError; end + class InvalidPluginName < Exception; end class IllegalPluginDefinition < Exception; end class AttributeNotFound < Exception; end class ProviderNotFound < Exception; end diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb index b8b5768c..ed6f33a2 100644 --- a/lib/ohai/loader.rb +++ b/lib/ohai/loader.rb @@ -74,6 +74,8 @@ For more information visit here: docs.opscode.com/ohai_custom.html") plugin = klass.new(@controller.data) unless klass.nil? rescue SystemExit, Interrupt raise + rescue Ohai::Exceptions::InvalidPluginName => e + Ohai::Log.warn("Invalid name for plugin at #{plugin_path}: #{e.message}") rescue Ohai::Exceptions::IllegalPluginDefinition => e Ohai::Log.warn("Plugin at #{plugin_path} is not properly defined: #{e.inspect}") rescue NoMethodError => e diff --git a/spec/unit/dsl/plugin_spec.rb b/spec/unit/dsl/plugin_spec.rb index 62a8fcc2..94081b13 100644 --- a/spec/unit/dsl/plugin_spec.rb +++ b/spec/unit/dsl/plugin_spec.rb @@ -91,6 +91,26 @@ describe Ohai::DSL::Plugin::VersionVII do @name = :Test end + describe "when the plugin is named incorrectly" do + context "because the plugin name doesn't start with a capital letter" do + it "should raise an Ohai::Exceptions::InvalidPluginName exception" do + expect{ Ohai.plugin(:badName) { } }.to raise_error(Ohai::Exceptions::InvalidPluginName, /badName is not a valid plugin name/) + end + end + + context "because the plugin name contains an underscore" do + it "should raise an Ohai::Exceptions::InvalidPluginName exception" do + expect{ Ohai.plugin(:Bad_Name) { } }.to raise_error(Ohai::Exceptions::InvalidPluginName, /Bad_Name is not a valid plugin name/) + end + end + + context "because the plugin name isn't a symbol" do + it "should raise an Ohai::Exceptions::InvalidPluginName exception" do + expect{ Ohai.plugin(1138) { } }.to raise_error(Ohai::Exceptions::InvalidPluginName, /1138 is not a valid plugin name/) + end + end + end + describe "#version" do it "should save the plugin version as :version7" do plugin = Ohai.plugin(@name) { } diff --git a/spec/unit/loader_spec.rb b/spec/unit/loader_spec.rb index ced5bb6b..16767952 100644 --- a/spec/unit/loader_spec.rb +++ b/spec/unit/loader_spec.rb @@ -102,6 +102,19 @@ Ohai.plugin(:1nval!d) do end EOF + with_plugin("no_end.rb", <<EOF) +Ohai.plugin(:NoEnd) do + provides "fish_oil" + collect_data do +end +EOF + + with_plugin("bad_name.rb", <<EOF) +Ohai.plugin(:you_give_plugins_a_bad_name) do + provides "that/one/song" +end +EOF + describe "load_plugin() method" do describe "when the plugin uses Ohai.plugin instead of Ohai.plugins" do it "should log an unsupported operation warning" do @@ -168,6 +181,17 @@ EOF expect{ @loader.load_plugin(path_to("no_end.rb")) }.not_to raise_error end end + + describe "when the plugin has an invalid name" do + it "should log an invalid plugin name warning" do + Ohai::Log.should_receive(:warn).with(/Invalid name for plugin/) + @loader.load_plugin(path_to("bad_name.rb")) + end + + it "should not raise an error" do + expect{ @loader.load_plugin(path_to("bad_name.rb")) }.not_to raise_error + end + end end end end |