class RuboCop::Cop::Chef::Correctness::PowershellScriptDeleteFile

Use the `file` or `directory` resources built into Chef Infra Client with the :delete action to remove files/directories instead of using Remove-Item in a powershell_script resource

@example

 #### incorrect
 powershell_script 'Cleanup old files' do
   code 'Remove-Item C:\Windows\foo\bar.txt'
   only_if { ::File.exist?('C:\\Windows\\foo\\bar.txt') }
 end

#### correct
file 'C:\Windows\foo\bar.txt' do
  action :delete
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/chef/correctness/powershell_delete_file.rb, line 42
def on_block(node)
  match_property_in_resource?(:powershell_script, 'code', node) do |code_property|
    property_data = method_arg_ast_to_string(code_property)
    return unless property_data && property_data.match?(/^remove-item/i) &&
                  !property_data.include?('*')
    add_offense(node, message: MSG, severity: :refactor)
  end
end