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

Commit

Permalink
Merge branch 'master' into ffi_yajl
Browse files Browse the repository at this point in the history
  • Loading branch information
damacus authored Jul 9, 2017
2 parents 4859358 + f6be63d commit 5d799f9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
5 changes: 2 additions & 3 deletions features/033_check_for_missing_template.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ Feature: Check for missing template
When I check the cookbook specifying <version> as the Chef version
Then the missing template warning 033 <warning>
Examples:
| version | warning |
| 11.18.12 | should be displayed |
| 12.0.0 | should not be displayed |
| version | warning |
| 12.9.38 | should not be displayed |

Scenario: Template within deploy resource
Given a cookbook recipe with a deploy resource that contains a template resource
Expand Down
2 changes: 1 addition & 1 deletion features/support/command_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def assertions
"FC030" => "Cookbook contains debugger breakpoints",
"FC031" => "Cookbook without metadata.rb file",
"FC032" => "Invalid notification timing",
"FC033" => "Missing template",
"FC033" => "Missing template file",
"FC034" => "Unused template variables",
"FC037" => "Invalid notification action",
"FC038" => "Invalid resource action",
Expand Down
16 changes: 12 additions & 4 deletions lib/foodcritic/rules/fc033.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
rule "FC033", "Missing template" do
rule "FC033", "Missing template file" do
tags %w{correctness templates}
recipe do |ast, filename|
# find all template resources that don't fetch a template
# from either another cookbook or a local path
find_resources(ast, type: :template).reject do |resource|
resource_attributes(resource)["local"] ||
resource_attributes(resource)["cookbook"]
end.map do |resource|
# fetch the specified file to the template
file = template_file(resource_attributes(resource,
return_expressions: true))
{ resource: resource, file: file }
end.reject do |resource|
# skip the check if the file path is derived since
# we can't determine if that's here or not without converging the node
resource[:file].respond_to?(:xpath)
end.select do |resource|
template_paths(filename).none? do |path|
relative_path = []
Pathname.new(path).ascend do |template_path|
relative_path << template_path.basename
break if gem_version(chef_version) >= gem_version("12.0.0") &&
template_path.dirname.basename.to_s == "templates"
break if template_path.dirname.dirname.basename.to_s == "templates"
# stop building relative path if we've hit template or 1 dir above
# NOTE: This is a totally flawed attempt to strip things like
# templates/ubuntu/something.erb down to something.erb, which breaks
# legit nested dirs in the templates dir like templates/something/something.erb
break if template_path.dirname.basename.to_s == "templates" ||
template_path.dirname.dirname.basename.to_s == "templates"
end
File.join(relative_path.reverse) == resource[:file]
end
Expand Down

0 comments on commit 5d799f9

Please sign in to comment.