Class: RuboCop::Cop::Chef::ChefCorrectness::NotifiesActionNotSymbol

Inherits:
RuboCop::Cop
  • Object
show all
Includes:
RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/correctness/notifies_action_not_symbol.rb

Overview

When notifying an action within a resource the action should always be a symbol. In Chef Infra Client releases before 14.0 this may result in double notification.

Examples:


# bad
execute 'some commmand' do
  notifies 'restart', 'service[httpd]', 'delayed'
end

# good
execute 'some commmand' do
  notifies :restart, 'service[httpd]', 'delayed'
end

Constant Summary collapse

MSG =
'Resource notifcation actions should be symbols not strings.'.freeze

Instance Method Summary collapse

Methods included from RuboCop::Chef::CookbookHelpers

#match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string

Instance Method Details

#autocorrect(node) ⇒ Object



47
48
49
50
51
# File 'lib/rubocop/cop/chef/correctness/notifies_action_not_symbol.rb', line 47

def autocorrect(node)
  lambda do |corrector|
    corrector.replace(node.first_argument.loc.expression, ":#{node.node_parts[2].value}")
  end
end

#on_block(node) ⇒ Object



41
42
43
44
45
# File 'lib/rubocop/cop/chef/correctness/notifies_action_not_symbol.rb', line 41

def on_block(node)
  match_property_in_resource?(nil, 'notifies', node) do |notifies_property|
    add_offense(notifies_property, location: :expression, message: MSG, severity: :refactor) if notifies_property.node_parts[2].str_type?
  end
end