-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <tsmith@chef.io>
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |