From 4fa49cce8af2aa7c6b30f9be7340fa24ae661db5 Mon Sep 17 00:00:00 2001 From: Andrew Crump Date: Thu, 29 Dec 2011 14:48:45 +0000 Subject: [PATCH] FC003: Don't warn if chef-solo-search is available 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. --- features/003_check_for_chef_server.feature | 6 ++++++ features/step_definitions/cookbook_steps.rb | 14 +++++++++++++- features/support/cookbook_helpers.rb | 8 ++++++++ lib/foodcritic/helpers.rb | 12 ++++++++++++ lib/foodcritic/rules.rb | 4 ++-- 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/features/003_check_for_chef_server.feature b/features/003_check_for_chef_server.feature index 134c4363..d860705d 100644 --- a/features/003_check_for_chef_server.feature +++ b/features/003_check_for_chef_server.feature @@ -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 diff --git a/features/step_definitions/cookbook_steps.rb b/features/step_definitions/cookbook_steps.rb index 708da6c7..2c2c2e22 100644 --- a/features/step_definitions/cookbook_steps.rb +++ b/features/step_definitions/cookbook_steps.rb @@ -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 @@ -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| diff --git a/features/support/cookbook_helpers.rb b/features/support/cookbook_helpers.rb index 88cf3b68..e1a22a99 100644 --- a/features/support/cookbook_helpers.rb +++ b/features/support/cookbook_helpers.rb @@ -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. diff --git a/lib/foodcritic/helpers.rb b/lib/foodcritic/helpers.rb index 426db0ec..93175d9e 100644 --- a/lib/foodcritic/helpers.rb +++ b/lib/foodcritic/helpers.rb @@ -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. diff --git a/lib/foodcritic/rules.rb b/lib/foodcritic/rules.rb index 67ce2575..dde67fec 100644 --- a/lib/foodcritic/rules.rb +++ b/lib/foodcritic/rules.rb @@ -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