Class: RuboCop::Cop::Chef::ChefCorrectness::BlockGuardWithOnlyString

Inherits:
RuboCop::Cop
  • Object
show all
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 be executed as Ruby code which will always returns true instead of a shell command that will actually run.

Examples:


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

# good
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 be executed as Ruby code which will always returns true instead of a shell command that will actually run.'.freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



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

def autocorrect(node)
  lambda do |corrector|
    new_val = "#{node.method_name} #{node.body.source}"
    corrector.replace(node.loc.expression, new_val)
  end
end

#on_block(node) ⇒ Object



46
47
48
49
50
# File 'lib/rubocop/cop/chef/correctness/block_guard_clause_string_only.rb', line 46

def on_block(node)
  block_guard_with_only_string?(node) do
    add_offense(node, location: :expression, message: MSG, severity: :refactor)
  end
end