Class: RuboCop::Cop::Chef::ChefModernize::DefinesChefSpecMatchers

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/chef/modernize/defines_chefspec_matchers.rb

Overview

ChefSpec 7.1 and later auto generate ChefSpec matchers. Matchers in cookbooks can now be removed.

Examples:


# bad
if defined?(ChefSpec)
  def create_yum_repository(resource_name)
    ChefSpec::Matchers::ResourceMatcher.new(:yum_repository, :create, resource_name)
  end
end

Constant Summary collapse

MSG =
'ChefSpec matchers are now auto generated by ChefSpec 7.1+ and do not need to be defined in a cookbook'.freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



45
46
47
48
49
# File 'lib/rubocop/cop/chef/modernize/defines_chefspec_matchers.rb', line 45

def autocorrect(node)
  lambda do |corrector|
    corrector.remove(node.loc.expression)
  end
end

#on_if(node) ⇒ Object



39
40
41
42
43
# File 'lib/rubocop/cop/chef/modernize/defines_chefspec_matchers.rb', line 39

def on_if(node)
  chefspec_matcher?(node) do
    add_offense(node, location: :expression, message: MSG, severity: :refactor)
  end
end