From ce07d06217dc08b56b958bc2abfb75fe99cb6c8f Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Mon, 9 Sep 2019 13:41:36 -0700 Subject: [PATCH] Add ChefStyle/UsePlatformHelpers cop Move people towards our platform? and platform_family? helpers to simplify the platform checks in their recipes / resources. Signed-off-by: Tim Smith --- config/cookstyle.yml | 8 +++ .../cop/chef/style/use_platform_helpers.rb | 59 +++++++++++++++++++ .../chef/style/use_platform_helpers_spec.rb | 47 +++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 lib/rubocop/cop/chef/style/use_platform_helpers.rb create mode 100644 spec/rubocop/cop/chef/style/use_platform_helpers_spec.rb diff --git a/config/cookstyle.yml b/config/cookstyle.yml index d989c39b2..101911c7b 100644 --- a/config/cookstyle.yml +++ b/config/cookstyle.yml @@ -63,6 +63,14 @@ ChefStyle/FileMode: Exclude: - '**/metadata.rb' +ChefStyle/UsePlatformHelpers: + Description: Use platform? and platform_family? helpers for checking node platform in resources and recipes + Enabled: true + VersionAdded: '5.6.0' + Exclude: + - '**/metadata.rb' + - '**/libraries/*' + ############################### # ChefCorrectness: Avoiding potential problems ############################### diff --git a/lib/rubocop/cop/chef/style/use_platform_helpers.rb b/lib/rubocop/cop/chef/style/use_platform_helpers.rb new file mode 100644 index 000000000..4e83d993b --- /dev/null +++ b/lib/rubocop/cop/chef/style/use_platform_helpers.rb @@ -0,0 +1,59 @@ +# +# Copyright:: 2019, Chef Software, Inc. +# Author:: Tim Smith () +# +# 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 ChefStyle + # Use the platform?() and platform_family?() helpers instead of node['platform] == 'foo' + # and node['platform_family'] == 'bar'. These helpers are easier to read and can accept multiple + # platform arguments, which greatly simplifies complex platform logic. + # + # @example + # + # # bad + # node['platform'] == 'ubuntu' + # + # # good + # platform?('ubuntu') + # + class UsePlatformHelpers < Cop + MSG = "Use platform? and platform_family? helpers for checking node platform".freeze + + def_node_matcher :platform_check?, <<-PATTERN + ( send (send (send nil? :node) :[] $(str {"platform" "platform_family"}) ) :== $str ) + PATTERN + + def on_send(node) + platform_check?(node) do |type, plat| + # set these so we can use them in the auto_correct method + @type = type + @plat = plat + + add_offense(node, location: :expression, message: MSG, severity: :refactor) + end + end + + def autocorrect(node) + lambda do |corrector| + corrector.replace(node.loc.expression, "#{@type.value}?('#{@plat.value}')") + end + end + end + end + end + end +end \ No newline at end of file diff --git a/spec/rubocop/cop/chef/style/use_platform_helpers_spec.rb b/spec/rubocop/cop/chef/style/use_platform_helpers_spec.rb new file mode 100644 index 000000000..3328381f9 --- /dev/null +++ b/spec/rubocop/cop/chef/style/use_platform_helpers_spec.rb @@ -0,0 +1,47 @@ +# +# Copyright:: 2019, Chef Software, Inc. +# +# 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::ChefStyle::UsePlatformHelpers do + subject(:cop) { described_class.new } + + it "registers an offense when checking platform using node['platform']" do + expect_offense(<<~RUBY) + if node['platform'] == 'redhat' + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use platform? and platform_family? helpers for checking node platform + end + RUBY + + expect_correction(<<~RUBY) + if platform?('redhat') + end + RUBY + end + + it "registers an offense when checking platform family using node['platform_family']" do + expect_offense(<<~RUBY) + if node['platform_family'] == 'rhel' + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use platform? and platform_family? helpers for checking node platform + end + RUBY + + expect_correction(<<~RUBY) + if platform_family?('rhel') + end + RUBY + end +end