Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new ChefModernize/RespondToCompileTime cop #607

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,14 @@ ChefModernize/NodeRolesInclude:
- '**/metadata.rb'
- '**/Berksfile'

ChefModernize/RespondToCompileTime:
Description: There is no need to check if the chef_gem resource supports compile_time as Chef Infra Client 12.1 and later support the compile_time property.
Enabled: true
VersionAdded: '6.3.0'
Exclude:
- '**/metadata.rb'
- '**/Berksfile'

###############################
# ChefRedundantCode: Cleanup unnecessary code in your cookbooks regardless of Chef Infra Client release
###############################
Expand Down
93 changes: 93 additions & 0 deletions lib/rubocop/cop/chef/modernize/respond_to_compile_time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#
# 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.
#
module RuboCop
module Cop
module Chef
module ChefModernize
# There is no need to check if the chef_gem resource supports compile_time as Chef Infra Client 12.1 and later support the compile_time property.
#
# # bad
# chef_gem 'ultradns-sdk' do
# compile_time true if Chef::Resource::ChefGem.method_defined?(:compile_time)
# action :nothing
# end
#
# chef_gem 'ultradns-sdk' do
# compile_time true if Chef::Resource::ChefGem.instance_methods(false).include?(:compile_time)
# action :nothing
# end
#
# chef_gem 'ultradns-sdk' do
# compile_time true if respond_to?(:compile_time)
# action :nothing
# end
#
# # good
# chef_gem 'ultradns-sdk' do
# compile_time true
# action :nothing
# end
#
class RespondToCompileTime < Cop
extend TargetChefVersion

minimum_target_chef_version '12.1'

MSG = 'There is no need to check if the chef_gem resource supports compile_time as Chef Infra Client 12.1 and later support the compile_time property.'.freeze

def_node_matcher :compile_time_method_defined?, <<-PATTERN
(if
{
(send
(const
(const
(const nil? :Chef) :Resource) :ChefGem) :method_defined?
(sym :compile_time))

(send
(send
(const
(const
(const nil? :Chef) :Resource) :ChefGem) :instance_methods
(false)) :include?
(sym :compile_time))

(send nil? :respond_to?
(sym :compile_time))
}
(send nil? :compile_time
$(_)) nil?)
PATTERN

def on_if(node)
compile_time_method_defined?(node) do
add_offense(node, location: :expression, message: MSG, severity: :refactor)
end
end

def autocorrect(node)
lambda do |corrector|
compile_time_method_defined?(node) do |val|
corrector.replace(node.loc.expression, "compile_time #{val.source}")
end
end
end
end
end
end
end
end
82 changes: 82 additions & 0 deletions spec/rubocop/cop/chef/modernize/respond_to_compile_time_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#
# 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::ChefModernize::RespondToCompileTime, :config do
subject(:cop) { described_class.new(config) }

it 'registers an error when using: compile_time true if Chef::Resource::ChefGem.method_defined?(:compile_time)' do
expect_offense(<<~RUBY)
chef_gem 'ultradns-sdk' do
compile_time true if Chef::Resource::ChefGem.method_defined?(:compile_time)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ There is no need to check if the chef_gem resource supports compile_time as Chef Infra Client 12.1 and later support the compile_time property.
action :nothing
end
RUBY

expect_correction(<<~RUBY)
chef_gem 'ultradns-sdk' do
compile_time true
action :nothing
end
RUBY
end

it 'registers an error when using: compile_time true if Chef::Resource::ChefGem.instance_methods(false).include?(:compile_time)' do
expect_offense(<<~RUBY)
chef_gem 'ultradns-sdk' do
compile_time true if Chef::Resource::ChefGem.instance_methods(false).include?(:compile_time)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ There is no need to check if the chef_gem resource supports compile_time as Chef Infra Client 12.1 and later support the compile_time property.
action :nothing
end
RUBY

expect_correction(<<~RUBY)
chef_gem 'ultradns-sdk' do
compile_time true
action :nothing
end
RUBY
end

it 'registers an error when using: compile_time true if respond_to?(:compile_time)' do
expect_offense(<<~RUBY)
chef_gem 'ultradns-sdk' do
compile_time true if respond_to?(:compile_time)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ There is no need to check if the chef_gem resource supports compile_time as Chef Infra Client 12.1 and later support the compile_time property.
action :nothing
end
RUBY

expect_correction(<<~RUBY)
chef_gem 'ultradns-sdk' do
compile_time true
action :nothing
end
RUBY
end

it "doesn't register an offense when using just compile_time true" do
expect_no_offenses(<<~RUBY)
chef_gem 'ultradns-sdk' do
compile_time true
action :nothing
end
RUBY
end
end