Skip to content

Commit

Permalink
Find the plugins for the first parent for a given attribute in CLI if…
Browse files Browse the repository at this point in the history
… there are no plugins that provide any of the subattributes.
  • Loading branch information
sersut committed Jan 15, 2014
1 parent 7769246 commit ec77e7d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
24 changes: 20 additions & 4 deletions lib/ohai/provides_map.rb
Expand Up @@ -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 = []
Expand Down
9 changes: 7 additions & 2 deletions spec/unit/provides_map_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit ec77e7d

Please sign in to comment.