Permalink
Browse files

Find the plugins for the first parent for a given attribute in CLI if…

… there are no plugins that provide any of the subattributes.
  • Loading branch information...
1 parent 7769246 commit ec77e7d31f27cc0463f5186558f596bdb900a691 sersut committed Jan 15, 2014
Showing with 27 additions and 6 deletions.
  1. +20 −4 lib/ohai/provides_map.rb
  2. +7 −2 spec/unit/provides_map_spec.rb
View
24 lib/ohai/provides_map.rb
@@ -60,19 +60,35 @@ def find_providers_for(attributes)
plugins.uniq
end
- # gather plugins providing each of the attributes listed along
- # with providers of subattributes
+ # This function is used to fetch the plugins for the attributes specified
+ # in the CLI options to Ohai.
+ # It first attempts to find the plugins for the attributes
+ # or the sub attributes given.
+ # If it can't find any, it looks for plugins that might
+ # provide the parents of a given attribute and returns the
+ # first parent found.
def deep_find_providers_for(attributes)
plugins = []
attributes.each do |attribute|
attrs = select_subtree(@map, attribute)
- raise Ohai::Exceptions::AttributeNotFound, "No such attribute: \'#{attribute}\'" unless attrs
+
+ unless attrs
+ attrs = select_closest_subtree(@map, attribute)
+
+ unless attrs
+ raise Ohai::Exceptions::AttributeNotFound, "No such attribute: \'#{attribute}\'"
+ end
+ end
+
collect_plugins_in(attrs, plugins)
end
+
plugins.uniq
end
- # gather plugins providing each of the attributes listed, or the
+ # This function is used to fetch the plugins from
+ # 'depends "languages"' statements in plugins.
+ # It gathers plugins providing each of the attributes listed, or the
# plugins providing the closest parent attribute
def find_closest_providers_for(attributes)
plugins = []
View
9 spec/unit/provides_map_spec.rb
@@ -116,20 +116,25 @@
end
end
- describe "when looking for attribute and subattribute providers" do
+ describe "when looking for providers of attributes specified in CLI" do
before do
provides_map.set_providers_for(plugin_1, ["cat/whiskers"])
provides_map.set_providers_for(plugin_2, ["cat/paws", "cat/paws/toes"])
provides_map.set_providers_for(plugin_3, ["cat/mouth/teeth"])
end
- it "should find providers for subattributes even when the attribute doesn't have a provider" do
+ it "should find providers for subattributes if any exists when the attribute doesn't have a provider" do
providers = provides_map.deep_find_providers_for(["cat"])
expect(providers).to have(3).plugins
expect(providers).to include(plugin_1)
expect(providers).to include(plugin_2)
expect(providers).to include(plugin_3)
end
+
+ it "should find providers for the first parent attribute when the attribute or any subattributes doesn't have a provider" do
+ providers = provides_map.deep_find_providers_for(["cat/paws/front"])
+ expect(providers).to eq([plugin_2])
+ end
end
describe "when looking for the closest providers" do

0 comments on commit ec77e7d

Please sign in to comment.