Class: RuboCop::Cop::Chef::Correctness::MalformedPlatformValueForPlatformHelper

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/chef/correctness/malformed_value_for_platform.rb

Overview

When using the value_for_platform helper you must include a hash of possible platforms where each platform contains a hash of versions and potential values. If you don’t wish to match on a particular version you can instead use the key ‘default’.

Examples:


### incorrect
value_for_platform(
  %w(redhat oracle) => 'baz'
)

### correct
value_for_platform(
  %w(redhat oracle) => {
    '5' => 'foo',
    '6' => 'bar',
    'default'd => 'baz',
  }
)

value_for_platform(
  %w(redhat oracle) => {
    'default' => 'foo',
  },
  'default' => 'bar'
)

Constant Summary collapse

RESTRICT_ON_SEND =
[:value_for_platform].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/chef/correctness/malformed_value_for_platform.rb', line 50

def on_send(node)
  if node.arguments.count > 1
    msg = 'Malformed value_for_platform helper argument. The value_for_platform helper takes a single hash of platforms as an argument.'
    add_offense(node, message: msg, severity: :refactor)
  elsif node.arguments.first.hash_type? # if it's a variable we can't check what's in that variable so skip
    msg = "Malformed value_for_platform helper argument. The value for each platform in your hash must be a hash of either platform version strings or a value with a key of 'default'"
    node.arguments.first.each_pair do |plats, plat_vals|
      # instead of a platform the hash key can be default with a value of anything. Depending on the hash format this is a string or symbol
      next if plat_vals.hash_type? || plats == s(:str, 'default') || plats == s(:sym, :default)
      add_offense(plat_vals, message: msg, severity: :refactor)
    end
  end
end