Class: RuboCop::Cop::Chef::Correctness::BlockGuardWithOnlyString

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

Overview

A resource guard (not_if/only_if) that is a string should not be wrapped in ‘{}`. Wrapping a guard string in {} causes it to be executed as Ruby code which will always return true instead of a shell command that will actually run.

Examples:


### incorrect
template '/etc/foo' do
  mode '0644'
  source 'foo.erb'
  only_if { 'test -f /etc/foo' }
end

### correct
template '/etc/foo' do
  mode '0644'
  source 'foo.erb'
  only_if 'test -f /etc/foo'
end

Constant Summary collapse

MSG =
'A resource guard (not_if/only_if) that is a string should not be wrapped in {}. Wrapping a guard string in {} causes it to be executed as Ruby code which will always return true instead of a shell command that will actually run.'

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/chef/correctness/block_guard_clause_string_only.rb', line 49

def on_block(node)
  block_guard_with_only_string?(node) do
    add_offense(node, severity: :refactor) do |corrector|
      new_val = "#{node.method_name} #{node.body.source}"
      corrector.replace(node, new_val)
    end
  end
end