Class: RuboCop::Cop::Chef::ChefCorrectness::InvalidPlatformMetadata

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/chef/correctness/invalid_platform_metadata.rb

Overview

metadata.rb supports methods should contain valid platforms.

Examples:


# bad
supports 'darwin'
supports 'mswin'

# good
supports 'mac_os_x'
supports 'windows'

Constant Summary collapse

COMMON_TYPOS =
{
  "aws": nil,
  "archlinux": 'arch',
  "amazonlinux": 'amazon',
  "darwin": 'mac_os_x',
  "debuan": nil,
  "mingw32": 'windows',
  "mswin": 'windows',
  "macos": 'mac_os_x',
  "macosx": 'mac_os_x',
  "mac_os_x_server": 'mac_os_x',
  "mint": 'linuxmint',
  "linux": nil,
  "oel": 'oracle',
  "oraclelinux": 'oracle',
  "rhel": 'redhat',
  "schientific": 'scientific',
  "scientificlinux": 'scientific',
  "sles": 'suse',
  "solaris": 'solaris2',
  "ubundu": 'ubuntu',
  "ubunth": 'ubuntu',
  "ubunutu": 'ubuntu',
  "windwos": 'windows',
  "xcp": nil,
}.freeze
MSG =
'metadata.rb "supports" platform is invalid'.freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/rubocop/cop/chef/correctness/invalid_platform_metadata.rb', line 74

def autocorrect(node)
  correct_string = autocorrect_license_string(node.str_content)
  if correct_string
    lambda do |corrector|
      corrector.replace(node.loc.expression, "'#{correct_string}'")
    end
  end
end

#autocorrect_license_string(bad_string) ⇒ Object

private



85
86
87
# File 'lib/rubocop/cop/chef/correctness/invalid_platform_metadata.rb', line 85

def autocorrect_license_string(bad_string)
  COMMON_TYPOS[bad_string.delete(',').downcase.to_sym]
end

#on_send(node) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/rubocop/cop/chef/correctness/invalid_platform_metadata.rb', line 66

def on_send(node)
  supports?(node) do |plat|
    if COMMON_TYPOS[plat.str_content.to_sym]
      add_offense(plat, location: :expression, message: MSG, severity: :refactor)
    end
  end
end