Class: RuboCop::Cop::Chef::Correctness::PowershellScriptDeleteFile

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

Overview

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

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

Examples:


### 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

Constant Summary collapse

MSG =
'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'

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

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



42
43
44
45
46
47
48
49
# 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, severity: :refactor)
  end
end