Class: RuboCop::Cop::Chef::Modernize::ExecuteSleep

Inherits:
RuboCop::Cop
  • Object
show all
Extended by:
TargetChefVersion
Includes:
RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/modernize/execute_sleep.rb

Overview

Chef Infra Client 15.5 and later include a chef_sleep resource that should be used to sleep between executing resources if necessary instead of using the bash or execute resources to run the sleep command.

Examples:


### incorrect
execute "sleep 60" do
  command "sleep 60"
  action :run
end

bash 'sleep' do
  user 'root'
  cwd '/tmp'
  code 'sleep 60'
end

### correct
chef_sleep '60'

Constant Summary collapse

MSG =
'Chef Infra Client 15.5 and later include a chef_sleep resource that should be used to sleep between executing resources if necessary instead of using the bash or execute resources to run the sleep command.'
RESTRICT_ON_SEND =
[:execute].freeze

Instance Method Summary collapse

Methods included from TargetChefVersion

minimum_target_chef_version, required_minimum_chef_version, support_target_chef_version?

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

#on_block(node) ⇒ Object

block execute resources



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/chef/modernize/execute_sleep.rb', line 59

def on_block(node)
  match_property_in_resource?(:execute, 'command', node) do |code_property|
    property_data = method_arg_ast_to_string(code_property)
    next unless property_data && property_data.match?(/^sleep/i)
    add_offense(node, severity: :refactor)
  end

  match_property_in_resource?(:bash, 'code', node) do |code_property|
    property_data = method_arg_ast_to_string(code_property)
    next unless property_data && property_data.match?(/^sleep/i)
    add_offense(node, severity: :refactor)
  end
end

#on_send(node) ⇒ Object

non block execute resources



51
52
53
54
55
56
# File 'lib/rubocop/cop/chef/modernize/execute_sleep.rb', line 51

def on_send(node)
  # use a regex on source instead of .value in case there's string interpolation which adds a complex dstr type
  # with a nested string and a begin. Source allows us to avoid a lot of defensive programming here
  return unless node&.arguments.first&.source&.match?(/^("|')sleep/)
  add_offense(node, severity: :refactor)
end