Skip to content
This repository has been archived by the owner on Sep 19, 2020. It is now read-only.

Commit

Permalink
FC003: Don't warn if chef-solo-search is available
Browse files Browse the repository at this point in the history
Per the discussion on the Chef list if the edelight chef-solo-search library is
installed then we don't want to warn, refs #7.
  • Loading branch information
Andrew Crump committed Dec 29, 2011
1 parent 05ec5a6 commit 4fa49cc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
6 changes: 6 additions & 0 deletions features/003_check_for_chef_server.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Feature: Check for Chef Server
When I check the cookbook
Then the check for server warning 003 should be displayed

Scenario: Search with chef-solo-search
Given a cookbook with a single recipe that searches without checking if this is server
And another cookbook that has chef-solo-search installed
When I check the cookbook
Then the check for server warning 003 should not be displayed

Scenario: Search checking for server
Given a cookbook with a single recipe that searches but checks first to see if this is server
When I check the cookbook
Expand Down
14 changes: 13 additions & 1 deletion features/step_definitions/cookbook_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@
}
end

Given 'another cookbook that has chef-solo-search installed' do
write_library 'search', %q{
class Chef
class Recipe
def search(bag_name, query=nil, sort=nil, start=0, rows=1000, &block)
# https://github.com/edelight/chef-solo-search
end
end
end
}
end

Given 'I have installed the lint tool' do

end
Expand Down Expand Up @@ -477,7 +489,7 @@
end

Then 'the check for server warning 003 should not be displayed given we have checked' do
expect_warning("FC004", :line => 4, :expect_warning => false)
expect_warning("FC003", :line => 4, :expect_warning => false)
end

Then /^the file mode warning 006 should be (valid|invalid)$/ do |valid|
Expand Down
8 changes: 8 additions & 0 deletions features/support/cookbook_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ def write_definition(name, content)
write_file "cookbooks/example/definitions/#{name}.rb", content.strip
end

# Create a library with the provided content.
#
# @param [String] name The library name.
# @param [String] content The library content.
def write_library(name, content)
write_file "cookbooks/example/libraries/#{name}.rb", content.strip
end

# Create metdata with the provided content.
#
# @param [String] content The metadata content.
Expand Down
12 changes: 12 additions & 0 deletions lib/foodcritic/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ def checks_for_chef_solo?(ast)
count(descendant::ident[@value='solo']) > 0]}).empty?
end

# Is the chef-solo-search library available?
#
# @param [String] recipe_path The path to the current recipe
# @return [Boolean] True if the chef-solo-search library is available.
def chef_solo_search_supported?(recipe_path)
search_libs = Dir[File.join(Pathname.new(File.join(recipe_path, '../../..')).realpath, "**/libraries/search.rb")]
search_libs.any? do |lib|
! read_file(lib).xpath(%q{//class[count(descendant::const[@value='Chef' or @value='Recipe']) = 2]/
descendant::def/ident[@value='search']}).empty?
end
end

# Searches performed by the specified recipe.
#
# @param [Nokogiri::XML::Node] ast The AST of the cookbook recipe to check.
Expand Down
4 changes: 2 additions & 2 deletions lib/foodcritic/rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

rule "FC003", "Check whether you are running with chef server before using server-specific features" do
tags %w{portability solo}
recipe do |ast|
checks_for_chef_solo?(ast) ? [] : searches(ast).map{|s| match(s)}
recipe do |ast,filename|
searches(ast).map{|s| match(s)} unless checks_for_chef_solo?(ast) or chef_solo_search_supported?(filename)
end
end

Expand Down

0 comments on commit 4fa49cc

Please sign in to comment.