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 ChefDeprecations/PartialSearchHelperUsage #363

Merged
merged 1 commit into from
Nov 5, 2019
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
7 changes: 7 additions & 0 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,13 @@ ChefDeprecations/PoiseArchiveUsage:
Enabled: true
VersionAdded: '5.11.0'

ChefDeprecations/PartialSearchHelperUsage:
Description: Legacy partial_search usage should be updated to use :filter_keys in the search helper instead.
Enabled: true
VersionAdded: '5.11.0'
Exclude:
- '**/metadata.rb'

###############################
# ChefModernize: Cleaning up legacy code and using new built-in resources
###############################
Expand Down
59 changes: 59 additions & 0 deletions lib/rubocop/cop/chef/deprecation/partial_search_helper_usage.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 ChefDeprecations
# Legacy partial_search usage should be updated to use :filter_keys in the search helper instead
#
# @example
#
# # bad
# partial_search(:node, 'role:web',
# keys: { 'name' => [ 'name' ],
# 'ip' => [ 'ipaddress' ],
# 'kernel_version' => %w(kernel version),
# }
# ).each do |result|
# puts result['name']
# puts result['ip']
# puts result['kernel_version']
# end
#
# # good
# search(:node, 'role:web',
# filter_result: { 'name' => [ 'name' ],
# 'ip' => [ 'ipaddress' ],
# 'kernel_version' => %w(kernel version),
# }
# ).each do |result|
# puts result['name']
# puts result['ip']
# puts result['kernel_version']
# end
#
class PartialSearchHelperUsage < Cop
MSG = 'Legacy partial_search usage should be updated to use :filter_keys in the search helper instead'.freeze

def on_send(node)
add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.method_name == :partial_search
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# 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.
#

require 'spec_helper'

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

it 'registers an offense when using the partial_search helper' do
expect_offense(<<~RUBY)
partial_search(:node, 'role:web',
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Legacy partial_search usage should be updated to use :filter_keys in the search helper instead
keys: { 'name' => [ 'name' ],
'ip' => [ 'ipaddress' ],
'kernel_version' => %w(kernel version),
}
).each do |result|
puts result['name']
puts result['ip']
puts result['kernel_version']
end
RUBY
end

it "doesn't register an offense when using the search helper" do
expect_no_offenses(<<~RUBY)
search(:node, 'role:web',
filter_result: { 'name' => [ 'name' ],
'ip' => [ 'ipaddress' ],
'kernel_version' => %w(kernel version),
}
).each do |result|
puts result['name']
puts result['ip']
puts result['kernel_version']
end
RUBY
end
end