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

Commit

Permalink
FC026: Conditional contains only string, refs #30.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Crump committed May 21, 2012
1 parent e3b2941 commit 243ec01
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions features/026_check_for_conditional_block_string.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Feature: Check for conditional attribute blocks with only strings

In order to avoid wrongly actioning a resource
As a developer
I want to identify conditional attribute blocks that consist only of strings

Scenario Outline:
Given a cookbook recipe that declares a resource with a <conditional_attribute>
When I check the cookbook
Then the conditional block contains only string warning 026 should be <show_warning>

Examples:
| conditional_attribute | show_warning |
| not_if { "ls foo" } | shown |
| not_if do "ls foo" end | shown |
| only_if { "ls #{node['foo']['path']}" } | shown |
| not_if { "ls #{foo.method()}" } | shown |
| only_if { foo.bar } | not shown |
| not_if { foo.to_s } | not shown |
| not_if { File.exists?("/etc/foo") } | not shown |
4 changes: 4 additions & 0 deletions features/step_definitions/cookbook_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,10 @@ def search(bag_name, query=nil, sort=nil, start=0, rows=1000, &block)
expect_warning('FC024', :line => should_not.nil? ? @expected_line : nil, :expect_warning => should_not.nil?)
end

Then /^the conditional block contains only string warning 026 should be (shown|not shown)$/ do |show_warning|
expect_warning('FC026', :line => nil, :expect_warning => show_warning == 'shown')
end

Then /^the conditional string looks like ruby warning 020 should be (shown|not shown)$/ do |show_warning|
expect_warning('FC020', :line => nil, :expect_warning => show_warning == 'shown')
end
Expand Down
1 change: 1 addition & 0 deletions features/support/command_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module CommandHelpers
'FC023' => 'Prefer conditional attributes',
'FC024' => 'Consider adding platform equivalents',
'FC025' => 'Prefer chef_gem to compile-time gem install',
'FC026' => 'Conditional execution block attribute contains only string',
'FCTEST001' => 'Test Rule'
}

Expand Down
16 changes: 16 additions & 0 deletions lib/foodcritic/rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,19 @@
end
end
end

rule "FC026", "Conditional execution block attribute contains only string" do
tags %w{correctness}
applies_to {|version| version >= gem_version("0.7.4")}
recipe do |ast|
find_resources(ast).map{|r| resource_attributes(r)}.map do |resource|
[resource['not_if'], resource['only_if']]
end.flatten.compact.select do |condition|
condition.respond_to?(:xpath) and
! condition.xpath('descendant::string_literal').empty? and
! condition.xpath('stmts_add/string_literal').empty? and
condition.xpath('descendant::stmts_add[count(ancestor::
string_literal) = 0]').size == 1
end
end
end

0 comments on commit 243ec01

Please sign in to comment.