From db9b1239f1455a65c56487ce89fd63381f6eb1d9 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Thu, 21 Jan 2021 15:39:47 -0800 Subject: [PATCH 1/2] Add a cop for detecting legacy powershell methods We shouldn't use these anywhere now that we have the improved methods Signed-off-by: Tim Smith --- config/chefstyle.yml | 7 ++- .../ruby/legacy_powershell_out_methods.rb | 36 +++++++++++++++ .../legacy_powershell_out_methods_spec.rb | 45 +++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 lib/rubocop/cop/chef/ruby/legacy_powershell_out_methods.rb create mode 100644 spec/rubocop/cop/chef/ruby/legacy_powershell_out_methods_spec.rb diff --git a/config/chefstyle.yml b/config/chefstyle.yml index f161f4f..41d689a 100644 --- a/config/chefstyle.yml +++ b/config/chefstyle.yml @@ -713,4 +713,9 @@ Chef/Ruby/GemspecRequireRubygems: Chef/Ruby/RequireNetHttps: Description: net/https is deprecated and just includes net/http and openssl. We should include those directly instead Enabled: true - VersionAdded: '1.3.0' \ No newline at end of file + VersionAdded: '1.3.0' + +Chef/Ruby/LegacyPowershellOutMethods: + Description: Use powershell_exec!/powershell_exec instead of the slower legacy powershell_out!/powershell_out methods. + Enabled: true + VersionAdded: '1.6.0' \ No newline at end of file diff --git a/lib/rubocop/cop/chef/ruby/legacy_powershell_out_methods.rb b/lib/rubocop/cop/chef/ruby/legacy_powershell_out_methods.rb new file mode 100644 index 0000000..a99d7cc --- /dev/null +++ b/lib/rubocop/cop/chef/ruby/legacy_powershell_out_methods.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true +# +# Copyright:: 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 Ruby + # Use powershell_exec!/powershell_exec instead of powershell_out!/powershell_out. The new + # methods don't spawn 2 shells per shellout and instead use .NET bindings to call PS directly. + class LegacyPowershellOutMethods < Base + MSG = "Use powershell_exec!/powershell_exec instead of the slower legacy powershell_out!/powershell_out methods." + RESTRICT_ON_SEND = %i{powershell_out! powershell_out}.freeze + + def on_send(node) + add_offense(node, message: MSG, severity: :refactor) + end + end + end + end + end +end \ No newline at end of file diff --git a/spec/rubocop/cop/chef/ruby/legacy_powershell_out_methods_spec.rb b/spec/rubocop/cop/chef/ruby/legacy_powershell_out_methods_spec.rb new file mode 100644 index 0000000..09c72df --- /dev/null +++ b/spec/rubocop/cop/chef/ruby/legacy_powershell_out_methods_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true +# +# Copyright:: 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::Ruby::LegacyPowershellOutMethods, :config do + subject(:cop) { described_class.new(config) } + + it "registers an offense when using powershell_out!" do + expect_offense(<<~RUBY) + powershell_out!('foo') + ^^^^^^^^^^^^^^^^^^^^^^ Use powershell_exec!/powershell_exec instead of the slower legacy powershell_out!/powershell_out methods. + RUBY + end + + it "registers an offense when using powershell_out" do + expect_offense(<<~RUBY) + powershell_out('foo') + ^^^^^^^^^^^^^^^^^^^^^ Use powershell_exec!/powershell_exec instead of the slower legacy powershell_out!/powershell_out methods. + RUBY + end + + it "doesn't register an when using powershell_exec" do + expect_no_offenses("powershell_exec('foo')") + end + + it "doesn't register an when using powershell_exec!" do + expect_no_offenses("powershell_exec!('foo')") + end +end From b6eb708372836dac6eaf310a43906cb7a2f6f366 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Thu, 21 Jan 2021 16:09:06 -0800 Subject: [PATCH 2/2] Add the newline for phil Signed-off-by: Tim Smith --- config/chefstyle.yml | 2 +- lib/rubocop/cop/chef/ruby/legacy_powershell_out_methods.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/chefstyle.yml b/config/chefstyle.yml index 41d689a..2d917f7 100644 --- a/config/chefstyle.yml +++ b/config/chefstyle.yml @@ -718,4 +718,4 @@ Chef/Ruby/RequireNetHttps: Chef/Ruby/LegacyPowershellOutMethods: Description: Use powershell_exec!/powershell_exec instead of the slower legacy powershell_out!/powershell_out methods. Enabled: true - VersionAdded: '1.6.0' \ No newline at end of file + VersionAdded: '1.6.0' diff --git a/lib/rubocop/cop/chef/ruby/legacy_powershell_out_methods.rb b/lib/rubocop/cop/chef/ruby/legacy_powershell_out_methods.rb index a99d7cc..603e388 100644 --- a/lib/rubocop/cop/chef/ruby/legacy_powershell_out_methods.rb +++ b/lib/rubocop/cop/chef/ruby/legacy_powershell_out_methods.rb @@ -33,4 +33,4 @@ def on_send(node) end end end -end \ No newline at end of file +end