diff --git a/Rakefile b/Rakefile index 36bc9b90c..98c3193be 100644 --- a/Rakefile +++ b/Rakefile @@ -20,14 +20,13 @@ task :vendor do end require 'cookstyle' -require 'rubocop/rake_task' -RuboCop::RakeTask.new(:style) do |task| - task.options << '--display-cop-names' +task :style do + sh('bundle exec cookstyle') end require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) do |spec| - spec.pattern = FileList['spec/**/*_spec.rb'] + spec.pattern = FileList['spec/cop/**/*.rb'] end desc 'Run RSpec with code coverage' @@ -36,9 +35,25 @@ task :coverage do Rake::Task['spec'].execute end -desc 'Run RuboCop over this gem' -task :internal_investigation do - sh('bundle exec rubocop --require rubocop-rspec') +desc 'Ensure that all cops are defined in the cookstyle.yml config' +task :validate_config do + require 'cookstyle' + require 'yaml' + status = 0 + config = YAML.load_file('config/cookstyle.yml') + + puts 'Checking that all cops are defined in config/cookstyle.yml:' + + RuboCop::Cop::Chef.constants.each do |cop| + unless config["Chef/#{cop}"] + puts "Error: Chef/#{cop} not found in config/cookstyle.yml" + status = 1 + end + end + + puts 'All Cops found in the config. Good work.' if status == 0 + + exit status end begin @@ -54,4 +69,4 @@ task :console do ARGV.clear IRB.start end -task default: [:style, :spec] +task default: [:style, :spec, :validate_config] diff --git a/config/cookstyle.yml b/config/cookstyle.yml index 810b1728e..7009cea79 100644 --- a/config/cookstyle.yml +++ b/config/cookstyle.yml @@ -128,6 +128,13 @@ Chef/InvalidPlatformMetadata: Include: - '**/metadata.rb' +Chef/CookbookUsesNodeSave: + Description: Don't use node.save to save partial node data to the Chef Infra Server mid-run unless it's absolutely necessary. Node.save can result in failed Chef Infra runs appearing in search and increases load on the Chef Infra Server. + Enabled: true + VersionAdded: '5.5.0' + Exclude: + - '**/metadata.rb' + ############################### # Resolving Deprecations ############################### diff --git a/lib/rubocop/cop/chef/effortless/node_save.rb b/lib/rubocop/cop/chef/correctness/node_save.rb similarity index 68% rename from lib/rubocop/cop/chef/effortless/node_save.rb rename to lib/rubocop/cop/chef/correctness/node_save.rb index 1576b8c10..d0a2927d7 100644 --- a/lib/rubocop/cop/chef/effortless/node_save.rb +++ b/lib/rubocop/cop/chef/correctness/node_save.rb @@ -17,14 +17,16 @@ module RuboCop module Cop module Chef - # Do not use node.save with effortless as there is no server to save node state back to + # Don't use node.save to save partial node data to the Chef Infra Server mid-run unless it's + # absolutely necessary. Node.save can result in failed Chef Infra runs appearing in search and + # increases load on the Chef Infra Server." # # @example # # # bad # node.save class CookbookUsesNodeSave < Cop - MSG = 'Do not use node.save with Effortless as there is no server to save node state back to'.freeze + MSG = "Don't use node.save to save partial node data to the Chef Infra Server mid-run unless it's absolutely necessary. Node.save can result in failed Chef Infra runs appearing in search and increases load on the Chef Infra Server.".freeze def_node_matcher :node_save?, <<-PATTERN (send (send nil? :node) :save)