Class: RuboCop::Cop::Chef::Modernize::ConditionalUsingTest

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/chef/modernize/conditional_using_test.rb

Overview

Use ::File.exist?(‘/foo/bar’) instead of the slower ‘test -f /foo/bar’ which requires shelling out

Examples:


### incorrect
only_if 'test -f /bin/foo'

### correct
only_if { ::File.exist?('bin/foo') }

Constant Summary collapse

MSG =
"Use ::File.exist?('/foo/bar') instead of the slower 'test -f /foo/bar' which requires shelling out"
RESTRICT_ON_SEND =
[:not_if, :only_if].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/chef/modernize/conditional_using_test.rb', line 42

def on_send(node)
  resource_conditional?(node) do |conditional|
    return unless conditional.value.match?(/^test -[ef] \S*$/)
    add_offense(node, severity: :refactor) do |corrector|
      new_string = "{ ::File.exist?('#{conditional.value.match(/^test -[ef] (\S*)$/)[1]}') }"
      corrector.replace(conditional, new_string)
    end
  end
end