Class: RuboCop::Cop::Chef::Deprecations::DeprecatedChefSpecPlatform

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

Overview

Use currently supported platforms in ChefSpec listed at github.com/chefspec/fauxhai/blob/main/PLATFORMS.md. Fauxhai / ChefSpec will perform fuzzy matching on platform version values so it’s always best to be less specific ie. 10 instead of 10.3

Examples:


let(:chef_run) { ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '14.04') }

Constant Summary collapse

MSG =
"Use currently supported platforms in ChefSpec listed at https://github.com/chefspec/fauxhai/blob/main/PLATFORMS.md. Fauxhai / ChefSpec will perform fuzzy matching on platform version so it's always best to be less specific ie. 10 instead of 10.3"
DEPRECATED_MAPPING =
{
  'amazon' => {
    '2017.12' => '2',
    '> 2010' => true,
  },
  'aix' => {
    '~> 6' => true,
  },
  'smartos' => {
    '5.10' => true,
  },
  'ubuntu' => {
    '< 16.04' => true,
    '> 16.04, < 18.04' => true,
  },
  'fedora' => {
    '< 32' => '32',
  },
  'freebsd' => {
    '= 12.0' => '12',
    '< 12' => true,
  },
  'mac_os_x' => {
    '< 10.14' => '10.15',
    ' = 11.0' => '11',
  },
  'suse' => {
    '~> 12.0, < 12.4' => '12',
    '< 12' => true,
  },
  'opensuse' => {
    '< 14' => true,
    '~> 42.0' => true,
    '~> 15.0, < 15.2' => '15',
  },
  'debian' => {
    '< 9' => true,
    '> 9.0, < 9.12' => '9',
  },
  'centos' => {
    '< 6.0' => true,
    '~> 6.0, < 6.10' => '6',
    '~> 7.0, < 7.8 ' => '7',
  },
  'redhat' => {
    '< 6.0' => true,
    '~> 6.0, < 6.10' => '6',
    '~> 7.0, < 7.8' => '7',
  },
  'oracle' => {
    '< 6.0' => true,
    '~> 6.0, < 6.10' => '6',
    '~> 7.0, < 7.6 ' => '7',
  },
}.freeze

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

#legacy_chefspec_platform(platform, version) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb', line 94

def legacy_chefspec_platform(platform, version)
  return false unless DEPRECATED_MAPPING.key?(platform)

  DEPRECATED_MAPPING[platform].each_pair do |match_string, replacement|
    return true if Gem::Dependency.new('', match_string.split(',')).match?('', version) &&
                   replacement != version # we want to catch '7.0' and suggest '7', but not alert on '7'
  end

  false
end

#on_send(node) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb', line 115

def on_send(node)
  chefspec_definition?(node) do |plat, ver|
    next unless legacy_chefspec_platform(plat.value, ver.value)
    add_offense(node, severity: :warning) do |corrector|
      if replacement = replacement_string(plat.value, ver.value) # rubocop: disable Lint/AssignmentInCondition
        corrector.replace(ver, "'#{replacement}'")
      end
    end
  end
end

#replacement_string(platform, version) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb', line 105

def replacement_string(platform, version)
  DEPRECATED_MAPPING[platform].each_pair do |match_string, replacement|
    return replacement if Gem::Dependency.new('', match_string.split(',')).match?('', version) &&
                          replacement != version && # we want to catch '7.0' and suggest '7', but not alert on '7'
                          replacement != true # true means it's busted, but requires human intervention to fix
  end

  nil # we don't have a replacement os return nil
end