Skip to content

Commit

Permalink
Add foodcritic. Improve output.
Browse files Browse the repository at this point in the history
Add foodcritic check for Cookbooks. Give user more feedback which
check-stage fails.
  • Loading branch information
jbraeuer committed Jan 13, 2014
1 parent 7211d5e commit 38fabca
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ tmp
.yardoc
_yardoc
doc/
/Gemfile.lock
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gem 'foodcritic'
27 changes: 18 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#/usr/bin/env rake
# encoding: utf-8

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
Encoding.default_external = Encoding::UTF_8 unless RUBY_VERSION.start_with? "1.8"
Encoding.default_internal = Encoding::UTF_8 unless RUBY_VERSION.start_with? "1.8"

desc 'check literal recipe includes'
task :validate_literal_includes do
puts "Check literal recipe includes"
found_issue = false
Dir['**/*.rb'].each do |file|
begin
Expand Down Expand Up @@ -40,6 +40,7 @@ KNOWN_COOKBOOK_ATTRIBUTES = {

desc 'check declared attribute dependencies'
task :validate_attribute_dependencies do
puts "Validate attribute dependencies"
Dir['**/*.rb'].each do |file|
next unless file.match(/\/attributes\//)
used_cookbook_attributes = []
Expand All @@ -62,24 +63,21 @@ task :validate_attribute_dependencies do

used_cookbook_attributes.uniq!
loaded_cookbook_attributes.uniq!

used_cookbook_attributes_without_include = used_cookbook_attributes - loaded_cookbook_attributes

used_cookbook_attributes_without_include.delete_if{|cookbook_attribute| KNOWN_COOKBOOK_ATTRIBUTES[cookbook_attribute] == :any || loaded_cookbook_attributes.include?(KNOWN_COOKBOOK_ATTRIBUTES[cookbook_attribute]) }

if used_cookbook_attributes_without_include.size > 0
puts "#{file} used attributes from #{used_cookbook_attributes.inspect} but does only loads from #{loaded_cookbook_attributes.inspect}"
found_trouble = true
end

if found_trouble
exit 1
end
exit 1 if found_trouble
end
end

desc 'check syntax of ruby files'
task :validate_syntax do
puts "Check syntax of Ruby files"
found_trouble = false
Dir['**/*.rb'].each do |file|
output = `ruby -c #{file}`
Expand All @@ -91,5 +89,16 @@ task :validate_syntax do
exit 1 if found_trouble
end

desc 'check cookbooks with Foodcritic'
task :validate_best_practises do
if RUBY_VERSION.start_with? "1.8"
warn "Foodcritic requires Ruby 1.9+. You run 1.8. Skipping..."
else
puts "Check Cookbooks with Foodcritic"
system "foodcritic ."
exit 1 unless $?.success?
end
end

desc 'run all checks'
task :default => [:validate_literal_includes, :validate_syntax, :validate_attribute_dependencies]
task :default => [:validate_syntax, :validate_literal_includes, :validate_best_practises, :validate_attribute_dependencies]

0 comments on commit 38fabca

Please sign in to comment.