Class: RuboCop::Cop::Chef::Modernize::UseChefLanguageEnvHelpers

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetChefVersion
Defined in:
lib/rubocop/cop/chef/modernize/use_chef_language_env_helpers.rb

Overview

Chef Infra Client 15.5 and later include a large number of new helpers in the Chef Infra Language to simplify checking the system configuration in recipes and resources. These should be used when possible over more complex attributes or ENV var comparisons.

Examples:


### incorrect
ENV['CI']
ENV['TEST_KITCHEN']

### correct
ci?
kitchen?

Constant Summary collapse

RESTRICT_ON_SEND =
[:[]].freeze

Instance Method Summary collapse

Methods included from TargetChefVersion

minimum_target_chef_version, required_minimum_chef_version, support_target_chef_version?

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/cop/chef/modernize/use_chef_language_env_helpers.rb', line 48

def on_send(node)
  env?(node) do |env_value|
    # we don't handle .nil? checks yet so just skip them
    next if node.parent.send_type? && node.parent.method?(:nil?)

    case env_value
    when 'CI'
      add_offense(node, message: 'Chef Infra Client 15.5 and later include a helper `ci?` that should be used to see if the `CI` env var is set.', severity: :refactor) do |corrector|
        corrector.replace(node, 'ci?')
      end
    when 'TEST_KITCHEN'
      add_offense(node, message: 'Chef Infra Client 15.5 and later include a helper `kitchen?` that should be used to see if the `TEST_KITCHEN` env var is set.', severity: :refactor) do |corrector|
        corrector.replace(node, 'kitchen?')
      end
    end
  end
end