diff options
-rw-r--r-- | lib/ohai/dsl/plugin.rb | 10 | ||||
-rw-r--r-- | spec/unit/dsl/plugin_spec.rb | 32 |
2 files changed, 25 insertions, 17 deletions
diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb index 47eb87b5..adcb6f82 100644 --- a/lib/ohai/dsl/plugin.rb +++ b/lib/ohai/dsl/plugin.rb @@ -186,10 +186,14 @@ module Ohai private def safe_get_attribute(*keys) - keys.inject(@data) { |attrs, key| attrs[key] } - rescue NoMethodError, TypeError + keys.inject(@data) do |attrs, key| + unless attrs.nil? || attrs.is_a?(Array) || attrs.is_a?(Hash) + raise TypeError.new("Expected Hash but got #{attrs.class}.") + end + attrs[key] + end + rescue NoMethodError # NoMethodError occurs when trying to access a key on nil - # TypeError occurs when trying to access a key on a value that is not a Hash nil end diff --git a/spec/unit/dsl/plugin_spec.rb b/spec/unit/dsl/plugin_spec.rb index bd91eb30..4b225df5 100644 --- a/spec/unit/dsl/plugin_spec.rb +++ b/spec/unit/dsl/plugin_spec.rb @@ -169,11 +169,12 @@ shared_examples "Ohai::DSL::Plugin" do end describe "and an intermediate key is not a hash" do - it "returns nil" do - expect( - plugin.get_attribute("the_moarch", "arch_rival", + it "raises a TypeError" do + expect { + plugin.get_attribute("the_monarch", "arch_rival", "dr_venture", "since") - ).to be nil + }.to raise_error(TypeError, + "Expected Hash but got String.") end end end @@ -202,11 +203,12 @@ shared_examples "Ohai::DSL::Plugin" do end describe "and an intermediate key is not a hash" do - it "returns nil" do - expect( + it "raises a TypeError" do + expect { plugin.get_attribute(:the_monarch, :arch_rival, :dr_venture, :since) - ).to be nil + }.to raise_error(TypeError, + "Expected Hash but got String.") end end end @@ -274,11 +276,12 @@ shared_examples "Ohai::DSL::Plugin" do end describe "and an intermediate key is not a hash" do - it "returns false" do - expect( - plugin.attribute?("the_moarch", "arch_rival", + it "raises a TypeError" do + expect { + plugin.attribute?("the_monarch", "arch_rival", "dr_venture", "since") - ).to be false + }.to raise_error(TypeError, + "Expected Hash but got String.") end end end @@ -306,11 +309,12 @@ shared_examples "Ohai::DSL::Plugin" do end describe "and an intermediate key is not a hash" do - it "returns false" do - expect( + it "raises a TypeError" do + expect { plugin.attribute?(:the_monarch, :arch_rival, :dr_venture, :since) - ).to be false + }.to raise_error(TypeError, + "Expected Hash but got String.") end end end |