class RuboCop::Cop::Chef::Correctness::BlockGuardWithOnlyString

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.

@example

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

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# 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