Skip to content

Commit

Permalink
FC027: Resource sets internal attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Crump committed Jun 2, 2012
1 parent 2500d5d commit 5fcadb7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
22 changes: 22 additions & 0 deletions features/027_check_for_internal_attribute_use.feature
@@ -0,0 +1,22 @@
Feature: Check for use of internal attributes

In order to avoid confusion about the expected state of a converged node
As a developer
I want to identify calls to 'internal' resource attributes

Scenario Outline: Access of internal attributes
Given a cookbook recipe that declares a <resource> resource with the <attribute> attribute set to <value>
When I check the cookbook
Then the resource sets internal attribute warning 027 should be <show_warning>

Examples:
| resource | attribute | value | show_warning |
| service | enabled | true | shown |
| service | enabled | false | shown |
| service | enabled | [].include?('foo') | shown |
| service | running | true | shown |
| service | running | false | shown |
| service | running | [].include?('foo') | shown |
| service | service_name | "foo" | not shown |
| my_lwrp | enabled | true | not shown |
| my_lwrp | running | true | not shown |
12 changes: 12 additions & 0 deletions features/step_definitions/cookbook_steps.rb
Expand Up @@ -25,6 +25,14 @@
}.strip
end

Given /^a cookbook recipe that declares a ([^ ]+) resource with the ([^ ]+) attribute set to (.*)$/ do |resource, attribute, value|
write_recipe %Q{
#{resource} "foo" do
#{attribute} #{value}
end
}
end

Given 'a cookbook provider that declares execute resources varying only in the command in separate actions' do
write_provider 'site', %q{
action :start do
Expand Down Expand Up @@ -1015,6 +1023,10 @@ def search(bag_name, query=nil, sort=nil, start=0, rows=1000, &block)
expect_output "cookbooks/example/recipes/default.rb"
end

Then /^the resource sets internal attribute warning 027 should be (not )?shown$/ do |should_not|
expect_warning('FC027', :line => nil, :expect_warning => should_not.nil?)
end

Then 'the review should include the matching rules' do
repl_review_includes_match?(@rule_code, @rule_name).should be_true
end
Expand Down
1 change: 1 addition & 0 deletions features/support/command_helpers.rb
Expand Up @@ -33,6 +33,7 @@ module CommandHelpers
'FC024' => 'Consider adding platform equivalents',
'FC025' => 'Prefer chef_gem to compile-time gem install',
'FC026' => 'Conditional execution block attribute contains only string',
'FC027' => 'Resource sets internal attribute',
'FCTEST001' => 'Test Rule'
}

Expand Down
10 changes: 10 additions & 0 deletions lib/foodcritic/rules.rb
Expand Up @@ -378,3 +378,13 @@
end
end
end

rule "FC027", "Resource sets internal attribute" do
tags %w{correctness}
recipe do |ast|
find_resources(ast, :type => :service).map do |service|
service unless (resource_attributes(service).keys &
['enabled', 'running']).empty?
end.compact
end
end

0 comments on commit 5fcadb7

Please sign in to comment.