Class: RuboCop::Cop::Chef::Correctness::MacosUserdefaultsInvalidType

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/correctness/macos_userdefaults_invalid_type.rb

Overview

The macos_userdefaults resource prior to Chef Infra Client 16.3 would silently continue if invalid types were passed resulting in unexpected behavior. Valid values are: “array”, “bool”, “dict”, “float”, “int”, and “string”.

Examples:


### incorrect
macos_userdefaults 'set a value' do
  global true
  key 'key'
  type 'boolean'
end

### correct
macos_userdefaults 'set a value' do
  global true
  key 'key'
  type 'bool'
end

Constant Summary collapse

VALID_VALUES =
%w(array bool dict float int string).freeze
INVALID_VALUE_MAP =
{
  'boolean' => 'bool',
  'str' => 'string',
  'integer' => 'int',
}.freeze
MSG =
'The macos_userdefaults resource prior to Chef Infra Client 16.3 would silently continue if invalid types were passed resulting in unexpected behavior. Valid values are: "array", "bool", "dict", "float", "int", and "string".'

Instance Method Summary collapse

Methods included from RuboCop::Chef::CookbookHelpers

#match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



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

def on_block(node)
  match_property_in_resource?(:macos_userdefaults, 'type', node) do |type|
    type_val = method_arg_ast_to_string(type)
    return if VALID_VALUES.include?(type_val)
    add_offense(type, severity: :refactor) do |corrector|
      next unless INVALID_VALUE_MAP[type_val]
      corrector.replace(type, "type '#{INVALID_VALUE_MAP[type_val]}'")
    end
  end
end