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

Commit

Permalink
add checks for correct use of use_inline_resources
Browse files Browse the repository at this point in the history
- recommend use_inline_resource in all 'LWRPBase' providers
- catch the use of use_inline_resources when users are also declaring
  `def action_whatever` instead of `action :whatever` which defeats
  use_inline_resources
  • Loading branch information
lamont-granquist committed Nov 6, 2015
1 parent 952d14f commit 4728af6
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Feature: Check for library providers that do not declare use_inline_resources

In order to ensure that notifications happen correctly
As a cookbook provider author
I want to always use_inline_resources

Scenario: Library provider with use_inline_resources
Given a cookbook that contains a library provider with use_inline_resources
When I check the cookbook
Then the library provider without use_inline_resources warning 057 should not be displayed against the libraries file

Scenario: Library provider without use_inline_resources
Given a cookbook that contains a library provider without use_inline_resources
When I check the cookbook
Then the library provider without use_inline_resources warning 057 should be displayed against the libraries file on line 11

Scenario: Library file without use_inline_resources
Given a cookbook that contains a library resource
When I check the cookbook
Then the library provider without use_inline_resources warning 057 should not be displayed against the libraries file
25 changes: 25 additions & 0 deletions features/058_check_for_library_provider_bad_action_methods.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Check for library providers that declare use_inline_resources and declare action_<name> methods

In order to ensure that notifications happen correctly
As a cookbook provider author
I want to always use_inline_resources

Scenario: Library provider with use_inline_resources and bad action_create
Given a cookbook that contains a library provider with use_inline_resources and uses def action_create
When I check the cookbook
Then the library provider without use_inline_resources and bad action_create warning 058 should be displayed against the libraries file on line 11

Scenario: Library provider without use_inline_resources and (okay) action_create
Given a cookbook that contains a library provider without use_inline_resources and uses def action_create
When I check the cookbook
Then the library provider without use_inline_resources and bad action_create warning 058 should not be displayed against the libraries file

Scenario: Library provider without use_inline_resources
Given a cookbook that contains a library provider without use_inline_resources
When I check the cookbook
Then the library provider without use_inline_resources and bad action_create warning 058 should not be displayed against the libraries file

Scenario: Library provider with use_inline_resources
Given a cookbook that contains a library provider with use_inline_resources
When I check the cookbook
Then the library provider without use_inline_resources and bad action_create warning 058 should not be displayed against the libraries file
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: Check for LWRP providers that do not declare use_inline_resources

In order to ensure that notifications happen correctly
As a cookbook provider author
I want to always use_inline_resources

Scenario: LWRP provider with use_inline_resources
Given a cookbook that contains a LWRP provider with use_inline_resources
When I check the cookbook
Then the LWRP provider without use_inline_resources warning 059 should not be displayed against the provider file

Scenario: LWRP provider without use_inline_resources
Given a cookbook that contains a LWRP provider without use_inline_resources
When I check the cookbook
Then the LWRP provider without use_inline_resources warning 059 should be displayed against the provider file
25 changes: 25 additions & 0 deletions features/060_check_for_lwrp_provider_bad_action_methods.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Check for LWRP providers that declare use_inline_resources and declare action_<name> methods

In order to ensure that notifications happen correctly
As a cookbook provider author
I want to always use_inline_resources

Scenario: LWRP provider with use_inline_resources and bad action_create
Given a cookbook that contains a LWRP provider with use_inline_resources and uses def action_create
When I check the cookbook
Then the LWRP provider without use_inline_resources and bad action_create warning 060 should be displayed against the provider file on line 3

Scenario: LWRP provider without use_inline_resources
Given a cookbook that contains a LWRP provider without use_inline_resources
When I check the cookbook
Then the LWRP provider without use_inline_resources and bad action_create warning 060 should not be displayed against the provider file

Scenario: LWRP provider without use_inline_resources and (okay) action_create
Given a cookbook that contains a LWRP provider without use_inline_resources and uses def action_create
When I check the cookbook
Then the LWRP provider without use_inline_resources and bad action_create warning 060 should not be displayed against the provider file

Scenario: LWRP provider with use_inline_resources
Given a cookbook that contains a LWRP provider with use_inline_resources
When I check the cookbook
Then the LWRP provider without use_inline_resources and bad action_create warning 060 should not be displayed against the provider file
82 changes: 81 additions & 1 deletion features/step_definitions/cookbook_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,7 @@ def search(bag_name, query=nil, sort=nil, start=0, rows=1000, &block)
expect_warning code, {:expect_warning => should_not.nil?}
end

Then /^the (?:[a-zA-Z \-_]+) warning ([0-9]+) should (not )?be displayed(?: against the (attributes|libraries|definition|metadata|provider|resource|README.md|README.rdoc) file)?( below)?$/ do |code, no_display, file, warning_only|
Then /^the (?:[a-zA-Z \-_]+) warning ([0-9]+) should (not )?be displayed(?: against the (attributes|libraries|definition|metadata|provider|resource|README.md|README.rdoc) file)?( below)?(?: on line ([0-9]+))?$/ do |code, no_display, file, warning_only, line|
options = {}
options[:expect_warning] = no_display != 'not '
unless file.nil?
Expand All @@ -2095,6 +2095,7 @@ def search(bag_name, query=nil, sort=nil, start=0, rows=1000, &block)
end
options[:line] = 3 if code == '018' and options[:expect_warning]
options[:line] = 2 if ['021', '022'].include?(code)
options[:line] = line unless line.nil?
options[:warning_only] = ! warning_only.nil?
expect_warning("FC#{code}", options)
end
Expand Down Expand Up @@ -2331,3 +2332,82 @@ def search(bag_name, query=nil, sort=nil, start=0, rows=1000, &block)
Then /^the metadata using recommends warning 053 should be (shown|not shown) against the metadata file$/ do |show_warning|
expect_warning('FC053', :file => "metadata.rb", :line => 2, :expect_warning => show_warning == 'shown')
end

Given /^a cookbook that contains a LWRP provider (with|without) use_inline_resources( and uses def action_create)?$/ do |with_use_inline_resources, uses_def|
write_resource("site", %q{
actions :create
attribute :name, :kind_of => String, :name_attribute => true
})
provider_file = ""
if with_use_inline_resources == 'with'
provider_file += %q{
use_inline_resources
}
end
if uses_def
provider_file += %q{
def action_create
}
else
provider_file += %q{
action :create do
}
end
provider_file += %q{
file "/tmp/foo.txt"
end
}
write_provider("site", provider_file)
end

Given /^a cookbook that contains a library provider (with|without) use_inline_resources( and uses def action_create)?$/ do |with_use_inline_resources, uses_def|
library_file = %q{
class MyResources
class Site < Chef::Resource::LWRPBase
provides :site
resource_name :site
actions :create
attribute :name, :kind_of => String, :name_attribute => true
end
end
class MyProviders
class Site < Chef::Provider::LWRPBase
provides :site
}
if with_use_inline_resources == 'with'
library_file += %q{
use_inline_resources
}
end
if uses_def
library_file += %q{
def action_create
}
else
library_file += %q{
action :create do
}
end
library_file += %q{
file "/tmp/foo.txt"
end
end
end
}
write_library('lib', library_file)
end

Given /^a cookbook that contains a library resource$/ do
library_file = %q{
class MyResources
class Site < Chef::Resource::LWRPBase
provides :site
resource_name :site
actions :create
attribute :name, :kind_of => String, :name_attribute => true
end
end
}
write_library('lib', library_file)
end
4 changes: 4 additions & 0 deletions features/support/command_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def assertions
# FC054 was yanked and is considered reserved, do not reuse it
'FC055' => 'Ensure maintainer is set in metadata',
'FC056' => 'Ensure maintainer_email is set in metadata',
'FC057' => 'Library provider does not declare use_inline_resources',
'FC058' => 'Library provider declares use_inline_resources and declares #action_<name> methods',
'FC059' => 'LWRP provider does not declare use_inline_resources',
'FC060' => 'LWRP provider declares use_inline_resources and declares #action_<name> methods',
'FCTEST001' => 'Test Rule'
}

Expand Down
41 changes: 41 additions & 0 deletions lib/foodcritic/rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -785,3 +785,44 @@ def invalid_name(ast)
[file_match(filename)] unless field(ast, 'maintainer_email').any?
end
end

rule 'FC057', 'Library provider does not declare use_inline_resources' do
tags %w(correctness)
library do |ast, filename|
ast.xpath('//const_path_ref/const[@value="LWRPBase"]/..//const[@value="Provider"]/../../..').select do |x|
x.xpath('//*[self::vcall or self::var_ref]/ident[@value="use_inline_resources"]').empty?
end
end
end

rule 'FC058', 'Library provider declares use_inline_resources and declares #action_<name> methods' do
tags %w(correctness)
library do |ast, filename|
ast.xpath('//const_path_ref/const[@value="LWRPBase"]/..//const[@value="Provider"]/../../..').select do |x|
x.xpath('//*[self::vcall or self::var_ref]/ident[@value="use_inline_resources"]') &&
x.xpath(%Q(//def[ident[contains(@value, 'action_')]]))
end
end
end

rule 'FC059', 'LWRP provider does not declare use_inline_resources' do
tags %w(correctness)
provider do |ast, filename|
use_inline_resources = !ast.xpath('//*[self::vcall or self::var_ref]/ident
[@value="use_inline_resources"]').empty?
unless use_inline_resources
[file_match(filename)]
end
end
end

rule 'FC060', 'LWRP provider declares use_inline_resources and declares #action_<name> methods' do
tags %w(correctness)
provider do |ast, filename|
use_inline_resources = !ast.xpath('//*[self::vcall or self::var_ref]/ident
[@value="use_inline_resources"]').empty?
if use_inline_resources
ast.xpath(%Q(//def[ident[contains(@value, 'action_')]]))
end
end
end

0 comments on commit 4728af6

Please sign in to comment.