Skip to content

Commit

Permalink
Add ChefDeprecations/PartialSearchHelperUsage
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 committed Nov 5, 2019
1 parent 72801f9 commit 80c2ac6
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
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

0 comments on commit 80c2ac6

Please sign in to comment.