From 80c2ac6f7b03bd9276c7c5270158859b572a71b3 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Tue, 5 Nov 2019 09:46:48 -0800 Subject: [PATCH] Add ChefDeprecations/PartialSearchHelperUsage Signed-off-by: Tim Smith --- config/cookstyle.yml | 7 +++ .../partial_search_helper_usage.rb | 59 +++++++++++++++++++ .../partial_search_helper_usage_spec.rb | 53 +++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 lib/rubocop/cop/chef/deprecation/partial_search_helper_usage.rb create mode 100644 spec/rubocop/cop/chef/deprecation/partial_search_helper_usage_spec.rb diff --git a/config/cookstyle.yml b/config/cookstyle.yml index 010a7549a..0d4d3eba5 100644 --- a/config/cookstyle.yml +++ b/config/cookstyle.yml @@ -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 ############################### diff --git a/lib/rubocop/cop/chef/deprecation/partial_search_helper_usage.rb b/lib/rubocop/cop/chef/deprecation/partial_search_helper_usage.rb new file mode 100644 index 000000000..22a5a2cc1 --- /dev/null +++ b/lib/rubocop/cop/chef/deprecation/partial_search_helper_usage.rb @@ -0,0 +1,59 @@ +# +# Copyright:: 2019, Chef Software, Inc. +# Author:: Tim Smith () +# +# 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 diff --git a/spec/rubocop/cop/chef/deprecation/partial_search_helper_usage_spec.rb b/spec/rubocop/cop/chef/deprecation/partial_search_helper_usage_spec.rb new file mode 100644 index 000000000..d469f5d9c --- /dev/null +++ b/spec/rubocop/cop/chef/deprecation/partial_search_helper_usage_spec.rb @@ -0,0 +1,53 @@ +# +# Copyright:: 2019, Chef Software, Inc. +# Author:: Tim Smith () +# +# 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