Skip to content

Commit

Permalink
Add ChefStyle/UsePlatformHelpers cop
Browse files Browse the repository at this point in the history
Move people towards our platform? and platform_family? helpers to simplify the platform checks in their recipes / resources.

Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 committed Sep 9, 2019
1 parent c5c74cc commit ce07d06
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
###############################
Expand Down
59 changes: 59 additions & 0 deletions lib/rubocop/cop/chef/style/use_platform_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#
# Copyright:: 2019, 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 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
47 changes: 47 additions & 0 deletions spec/rubocop/cop/chef/style/use_platform_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ce07d06

Please sign in to comment.