Class: RuboCop::Cop::Chef::Style::UnnecessaryOSCheck

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/chef/style/unnecessary_os_check.rb

Overview

Use the platform_family?() helpers instead of node == ‘foo’ for platform_families that match one-to-one with OS values. These helpers are easier to read and can accept multiple platform arguments, which greatly simplifies complex platform logic. All values of ‘os` from Ohai match one-to-one with `platform_family` values except for `linux`, which has no single equivalent `platform_family`.

Examples:


### incorrect
node['os'] == 'darwin'
node['os'] == 'windows'
node['os'].eql?('aix')
%w(netbsd openbsd freebsd).include?(node['os'])

### correct
platform_family?('mac_os_x')
platform_family?('windows')
platform_family?('aix')
platform_family?('netbsd', 'openbsd', 'freebsd)

Constant Summary collapse

MSG =
"Use the platform_family?() helpers instead of node['os] == 'foo' for platform_families that match 1:1 with OS values."
RESTRICT_ON_SEND =
[:==, :!=, :eql?, :include?].freeze
UNNECESSARY_OS_VALUES =

sorted list of all the os values that match 1:1 with a platform_family

%w(aix darwin dragonflybsd freebsd netbsd openbsd solaris2 windows).freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#array_from_ast(ast) ⇒ Object

given an ast array spit out a ruby array



94
95
96
97
98
# File 'lib/rubocop/cop/chef/style/unnecessary_os_check.rb', line 94

def array_from_ast(ast)
  vals = []
  ast.each_child_node { |x| vals << x.value }
  vals.sort
end

#on_send(node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rubocop/cop/chef/style/unnecessary_os_check.rb', line 59

def on_send(node)
  os_equals?(node) do |operator, val|
    return unless UNNECESSARY_OS_VALUES.include?(val.value)
    add_offense(node, severity: :refactor) do |corrector|
      corrected_string = (operator == :!= ? '!' : '') + "platform_family?('#{sanitized_platform(val.value)}')"
      corrector.replace(node, corrected_string)
    end
  end

  os_eql?(node) do |val|
    return unless UNNECESSARY_OS_VALUES.include?(val.value)
    add_offense(node, severity: :refactor) do |corrector|
      corrected_string = "platform_family?('#{sanitized_platform(val.value)}')"
      corrector.replace(node, corrected_string)
    end
  end

  os_include?(node) do |val|
    array_of_plats = array_from_ast(val)
    # see if all the values in the .include? usage are in our list of 1:1 platform family to os values
    return unless (UNNECESSARY_OS_VALUES & array_of_plats) == array_of_plats
    add_offense(node, severity: :refactor) do |corrector|
      platforms = val.values.map { |x| x.str_type? ? "'#{sanitized_platform(x.value)}'" : x.source }
      corrected_string = "platform_family?(#{platforms.join(', ')})"
      corrector.replace(node, corrected_string)
    end
  end
end

#sanitized_platform(plat) ⇒ Object

return the passed value unless the value is darwin and then return mac_os_x



89
90
91
# File 'lib/rubocop/cop/chef/style/unnecessary_os_check.rb', line 89

def sanitized_platform(plat)
  plat == 'darwin' ? 'mac_os_x' : plat
end