diff options
author | Claire McQuin <claire@opscode.com> | 2013-12-17 17:09:03 -0800 |
---|---|---|
committer | Claire McQuin <claire@opscode.com> | 2013-12-19 10:44:34 -0800 |
commit | 70db970d8ca954c0fc470ae006f7f895935a41e0 (patch) | |
tree | b4e5bef6fcb45338464e176e3dacc19b8d7ead89 | |
parent | ba504dfe9315168d1ebeeca9bed4c382eef1c4a5 (diff) | |
download | ohai-70db970d8ca954c0fc470ae006f7f895935a41e0.tar.gz |
rescue from syntax error in loader
-rw-r--r-- | lib/ohai/loader.rb | 9 | ||||
-rw-r--r-- | spec/unit/loader_spec.rb | 88 |
2 files changed, 81 insertions, 16 deletions
diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb index 12fd8087..b8b5768c 100644 --- a/lib/ohai/loader.rb +++ b/lib/ohai/loader.rb @@ -74,10 +74,17 @@ 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::IllegalPluginDefinition => e + rescue Ohai::Exceptions::IllegalPluginDefinition => e Ohai::Log.warn("Plugin at #{plugin_path} is not properly defined: #{e.inspect}") rescue NoMethodError => e Ohai::Log.warn("[UNSUPPORTED OPERATION] Plugin at #{plugin_path} used unsupported operation \'#{e.name.to_s}\'") + rescue SyntaxError => e + # grab the part of the error message that follows "<main>:line#: syntax error" + # example: "<main>:3: syntax error, unexpected $end, expecting keyword_end" + # regex will grab ", unexpected $end, expecting keyword_end" + message_regex = /(,[^,]+)+$/ + message = message_regex.match(e.message)[0] + Ohai::Log.warn("Plugin at #{plugin_path} threw syntax error#{message}") rescue Exception, Errno::ENOENT => e Ohai::Log.warn("Plugin at #{plugin_path} threw exception #{e.inspect} #{e.backtrace.join("\n")}") end diff --git a/spec/unit/loader_spec.rb b/spec/unit/loader_spec.rb index 5547f652..ced5bb6b 100644 --- a/spec/unit/loader_spec.rb +++ b/spec/unit/loader_spec.rb @@ -69,13 +69,15 @@ EOF end when_plugins_directory "contains invalid plugins" do - with_plugin("no_method.rb", <<EOF) -Ohai.plugin(:Zoo) do - provides 'seals' + with_plugin("extra_s.rb", <<EOF) +Ohai.plugins(:ExtraS) do + provides "the_letter_s" end +EOF -Ohai.blah(:Nasty) do - provides 'seals' + with_plugin("no_method.rb", <<EOF) +Ohai.plugin(:NoMethod) do + really_wants "this_attribute" end EOF @@ -94,22 +96,78 @@ Ohai.plugin(:Zoo) do end EOF + with_plugin("bad_symbol.rb", <<EOF) +Ohai.plugin(:1nval!d) do + provides "not_a_symbol" +end +EOF + describe "load_plugin() method" do - it "should log a warning when plugin tries to call an unexisting method" do - Ohai::Log.should_receive(:warn).with(/used unsupported operation/) - lambda { @loader.load_plugin(path_to("no_method.rb")) }.should_not raise_error + describe "when the plugin uses Ohai.plugin instead of Ohai.plugins" do + it "should log an unsupported operation warning" do + Ohai::Log.should_receive(:warn).with(/used unsupported operation/) + @loader.load_plugin(path_to("extra_s.rb")) + end + + it "should not raise an error" do + expect{ @loader.load_plugin(path_to("extra_s.rb")) }.not_to raise_error + end end - it "should log a warning for illegal plugins" do - Ohai::Log.should_receive(:warn).with(/not properly defined/) - lambda { @loader.load_plugin(path_to("illegal_def.rb")) }.should_not raise_error + describe "when the plugin tries to call an unexisting method" do + it "shoud log an unsupported operation warning" do + Ohai::Log.should_receive(:warn).with(/used unsupported operation/) + @loader.load_plugin(path_to("no_method.rb")) + end + + it "should not raise an error" do + expect{ @loader.load_plugin(path_to("no_method.rb")) }.not_to raise_error + end end - it "should not raise an error during an unexpected exception" do - Ohai::Log.should_receive(:warn).with(/threw exception/) - lambda { @loader.load_plugin(path_to("unexpected_error.rb")) }.should_not raise_error + describe "when the plugin defines collect_data on the same platform more than once" do + it "shoud log an illegal plugin definition warning" do + Ohai::Log.should_receive(:warn).with(/not properly defined/) + @loader.load_plugin(path_to("illegal_def.rb")) + end + + it "should not raise an error" do + expect{ @loader.load_plugin(path_to("illegal_def.rb")) }.not_to raise_error + end + end + + describe "when an unexpected error is encountered" do + it "should log a warning" do + Ohai::Log.should_receive(:warn).with(/threw exception/) + @loader.load_plugin(path_to("unexpected_error.rb")) + end + + it "should not raise an error" do + expect{ @loader.load_plugin(path_to("unexpected_error.rb")) }.not_to raise_error + end + end + + describe "when the plugin name symbol has bad syntax" do + it "should log a syntax error warning" do + Ohai::Log.should_receive(:warn).with(/threw syntax error, unexpected tINTEGER/) + @loader.load_plugin(path_to("bad_symbol.rb")) + end + + it "should not raise an error" do + expect{ @loader.load_plugin(path_to("bad_symbol.rb")) }.not_to raise_error + end + end + + describe "when the plugin forgets an 'end'" do + it "should log a syntax error warning" do + Ohai::Log.should_receive(:warn).with(/threw syntax error, unexpected \$end/) + @loader.load_plugin(path_to("no_end.rb")) + end + + it "should not raise an error" do + expect{ @loader.load_plugin(path_to("no_end.rb")) }.not_to raise_error + end end end end - end |