From fa747d1fb82d7208466a76d270e2281e7027a3c4 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Wed, 26 Feb 2020 09:44:08 -0800 Subject: [PATCH] Add ChefDeprecations/WindowsFeatureServermanagercmd Detect the old install_method value that is no longer accepted. Signed-off-by: Tim Smith --- config/cookstyle.yml | 9 +++ .../windows_feature_servermanagercmd.rb | 57 +++++++++++++++++++ .../windows_feature_servermanagercmd_spec.rb | 39 +++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 lib/rubocop/cop/chef/deprecation/windows_feature_servermanagercmd.rb create mode 100644 spec/rubocop/cop/chef/deprecation/windows_feature_servermanagercmd_spec.rb diff --git a/config/cookstyle.yml b/config/cookstyle.yml index 6f2dc3e75..17decf147 100644 --- a/config/cookstyle.yml +++ b/config/cookstyle.yml @@ -717,6 +717,15 @@ ChefDeprecations/DeprecatedChefSpecPlatform: Include: - '**/spec/**/*.rb' +ChefDeprecations/WindowsFeatureServermanagercmd: + Description: The `windows_feature` resource no longer supports setting the `install_method` to `:servermanagercmd`. `:windows_feature_dism` or `:windows_feature_powershell` should be used instead. + Enabled: true + VersionAdded: '5.22.0' + Exclude: + - '**/metadata.rb' + - '**/attributes/*.rb' + - '**/Berksfile' + ############################### # ChefModernize: Cleaning up legacy code and using new built-in resources ############################### diff --git a/lib/rubocop/cop/chef/deprecation/windows_feature_servermanagercmd.rb b/lib/rubocop/cop/chef/deprecation/windows_feature_servermanagercmd.rb new file mode 100644 index 000000000..aa10f9057 --- /dev/null +++ b/lib/rubocop/cop/chef/deprecation/windows_feature_servermanagercmd.rb @@ -0,0 +1,57 @@ +# +# Copyright:: 2020, 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 + # The `windows_feature` resource no longer supports setting the `install_method` to `:servermanagercmd`. `:windows_feature_dism` or `:windows_feature_powershell` should be used instead. + # + # @example + # + # # bad + # windows_feature 'DHCP' do + # install_method :servermanagercmd + # end + # + # # good + # windows_feature 'DHCP' do + # install_method :windows_feature_dism + # end + # + # windows_feature 'DHCP' do + # install_method :windows_feature_powershell + # end + # + # windows_feature_dism 'DHCP' + # + # windows_feature_powershell 'DHCP' + # + class WindowsFeatureServermanagercmd < Cop + include RuboCop::Chef::CookbookHelpers + + MSG = 'The `windows_feature` resource no longer supports setting the `install_method` to `:servermanagercmd`. `:windows_feature_dism` or `:windows_feature_powershell` should be used instead.'.freeze + + def on_block(node) + match_property_in_resource?(:windows_feature, :install_method, node) do |prop_node| + add_offense(prop_node, location: :expression, message: MSG, severity: :refactor) if prop_node.source.match?(/:servermanagercmd/) + end + end + end + end + end + end +end diff --git a/spec/rubocop/cop/chef/deprecation/windows_feature_servermanagercmd_spec.rb b/spec/rubocop/cop/chef/deprecation/windows_feature_servermanagercmd_spec.rb new file mode 100644 index 000000000..bb0ca47d3 --- /dev/null +++ b/spec/rubocop/cop/chef/deprecation/windows_feature_servermanagercmd_spec.rb @@ -0,0 +1,39 @@ +# +# Copyright:: 2020, 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::WindowsFeatureServermanagercmd, :config do + subject(:cop) { described_class.new(config) } + + it 'registers an offense when windows_feature sets install_method to :servermanagercmd' do + expect_offense(<<~RUBY) + windows_feature 'DHCP' do + install_method :servermanagercmd + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The `windows_feature` resource no longer supports setting the `install_method` to `:servermanagercmd`. `:windows_feature_dism` or `:windows_feature_powershell` should be used instead. + end + RUBY + end + + it "doesn't register an offense when windows_feature sets install_method to :windows_feature_dism" do + expect_no_offenses(<<~RUBY) + windows_feature 'DHCP' do + install_method :windows_feature_dism + end + RUBY + end +end