class RuboCop::Cop::Chef::Modernize::UseChefLanguageEnvHelpers

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.

@example

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

#### correct
ci?
kitchen?

Constants

RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# 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