class RuboCop::Cop::Chef::RedundantCode::UseCreateIfMissing

Use the :create_if_missing action instead of not_if with a ::File.exist(FOO) check.

@example

#### incorrect
cookbook_file '/logs/foo/error.log' do
  source 'error.log'
  owner 'root'
  group 'root'
  mode '0644'
  not_if { ::File.exists?('/logs/foo/error.log') }
end

#### correct
cookbook_file '/logs/foo/error.log' do
  source 'error.log'
  owner 'root'
  group 'root'
  mode '0644'
  action :create_if_missing
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/chef/redundant/use_create_if_missing.rb, line 60
def on_block(node)
  not_if_file_exist?(node) do |props|
    file_like_resource?(node.parent.parent) do |resource_blk_name|
      # the not_if file name is the same as the resource name and there's no action defined (it's the default)
      return unless props == resource_blk_name && create_action?(node.parent.parent).nil?

      add_offense(node, message: MSG, severity: :refactor) do |corrector|
        corrector.replace(node, 'action :create_if_missing')
      end
    end
  end
end