Skip to content

Commit

Permalink
Merge pull request #889 from chef/env_helpers
Browse files Browse the repository at this point in the history
Add new Chef/Modernize/UseChefLanguageEnvHelpers cop
  • Loading branch information
tas50 authored Aug 25, 2021
2 parents c608ead + 7335343 commit 5af872a
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/SUPPORT_QUESTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ about: If you have a question 💬, please check out our Slack!

We use GitHub issues to track bugs and feature requests. If you need help please post to our Mailing List or join the Chef Community Slack.

* Chef Community Slack at <http://community-slack.chef.io/.>
* Chef Community Slack at <https://community-slack.chef.io/.>
* Chef Mailing List <https://discourse.chef.io/>

Support issues opened here will be closed and redirected to Slack or Discourse.
10 changes: 10 additions & 0 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,16 @@ Chef/Modernize/DependsOnOpensslCookbook:
Include:
- '**/metadata.rb'

Chef/Modernize/UseChefLanguageEnvHelpers:
Description: 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 attribute or ENV var comparisons.
StyleGuide: 'chef_modernize_usecheflanguageenvhelpers'
Enabled: true
VersionAdded: '7.21.0'
Include:
- '**/resources/*.rb'
- '**/providers/*.rb'
- '**/recipes/*.rb'

###############################
# Chef/RedundantCode: Cleanup unnecessary code in your cookbooks regardless of Chef Infra Client release
###############################
Expand Down
66 changes: 66 additions & 0 deletions lib/rubocop/cop/chef/modernize/use_chef_language_env_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true
#
# Copyright:: 2021, Chef Software, Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
module RuboCop
module Cop
module Chef
module Modernize
# 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 attribute or ENV var comparisons.
#
# @example
#
# #### incorrect
# ENV['CI']
# ENV['TEST_KITCHEN']
#
# #### correct
# ci?
# kitchen?
#
class UseChefLanguageEnvHelpers < Base
extend AutoCorrector

RESTRICT_ON_SEND = [:[]].freeze

def_node_matcher :env?, <<-PATTERN
(send
(const nil? :ENV) :[]
(str ${"TEST_KITCHEN" "CI"}))
PATTERN

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_name == :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
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true
#
# Copyright:: 2020, Chef Software, Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

describe RuboCop::Cop::Chef::Modernize::UseChefLanguageEnvHelpers, :config do
subject(:cop) { described_class.new(config) }

it "registers an offense when using ENV['CI'] in a cookbook" do
expect_offense(<<~RUBY)
if ENV['CI']
^^^^^^^^^ Chef Infra Client 15.5 and later include a helper `ci?` that should be used to see if the `CI` env var is set.
foo
end
RUBY

expect_correction(<<~RUBY)
if ci?
foo
end
RUBY
end

it "registers an offense when using ENV['TEST_KITCHEN'] in a cookbook" do
expect_offense(<<~RUBY)
if ENV['TEST_KITCHEN']
^^^^^^^^^^^^^^^^^^^ 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.
foo
end
RUBY

expect_correction(<<~RUBY)
if kitchen?
foo
end
RUBY
end

it 'does not alert when checking if the env var is nil' do
expect_no_offenses(<<~RUBY)
unless ENV['TEST_KITCHEN'].nil?
foo
end
RUBY
end

it 'does not alert when checking any old env var' do
expect_no_offenses(<<~RUBY)
if ENV['NOT_TEST_KITCHEN']
foo
end
RUBY
end
end

0 comments on commit 5af872a

Please sign in to comment.