summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormcquin <claire@chef.io>2016-04-14 10:11:17 -0700
committermcquin <claire@chef.io>2016-04-19 11:23:24 -0700
commitdedee0100659e1badbc962923cffbdc0512e93d2 (patch)
tree606297228bfca201729afbe4dbe839f1c1fa8872
parent1e7b6a07ee9cd0098d15e2f18606853ad207b65b (diff)
downloadohai-OHAI-794/attribute-from-mash.tar.gz
Raise TypeError when intermediate key is not a HashOHAI-794/attribute-from-mash
-rw-r--r--lib/ohai/dsl/plugin.rb10
-rw-r--r--spec/unit/dsl/plugin_spec.rb32
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