cookstyle 5.23.0 → 6.0.19
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/cookstyle.yml +86 -4
- data/config/disable_all.yml +58 -52
- data/config/upstream.yml +377 -336
- data/lib/cookstyle.rb +0 -1
- data/lib/cookstyle/version.rb +2 -2
- data/lib/rubocop/chef/cookbook_helpers.rb +3 -3
- data/lib/rubocop/cop/chef/correctness/chef_application_fatal.rb +58 -0
- data/lib/rubocop/cop/chef/correctness/incorrect_library_injection.rb +11 -5
- data/lib/rubocop/cop/chef/correctness/powershell_delete_file.rb +53 -0
- data/lib/rubocop/cop/chef/deprecation/chef_handler_supports.rb +3 -3
- data/lib/rubocop/cop/chef/deprecation/chef_rest.rb +2 -2
- data/lib/rubocop/cop/chef/deprecation/chef_rewind.rb +4 -4
- data/lib/rubocop/cop/chef/deprecation/chef_windows_platform_helper.rb +56 -0
- data/lib/rubocop/cop/chef/deprecation/cheffile.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/chefspec_coverage_report.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/chefspec_legacy_runner.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/chocolatey_package_uninstall_action.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/depends_compat_resource.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/depends_partial_search.rb +1 -5
- data/lib/rubocop/cop/chef/deprecation/depends_poise.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/deprecated_mixins.rb +3 -3
- data/lib/rubocop/cop/chef/deprecation/deprecated_platform_methods.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/deprecated_windows_version_check.rb +40 -0
- data/lib/rubocop/cop/chef/deprecation/deprecated_yum_repository_properties.rb +1 -4
- data/lib/rubocop/cop/chef/deprecation/easy_install.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/eol_audit_mode.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/epic_fail.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/erl_call.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/inherits_compat_resource.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/launchd_deprecated_hash_property.rb +1 -4
- data/lib/rubocop/cop/chef/deprecation/legacy_notify_syntax.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/legacy_yum_cookbook_recipes.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/locale_lc_all_property.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/log_resource_notifications.rb +60 -0
- data/lib/rubocop/cop/chef/deprecation/name_property_and_default.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/node_deep_fetch.rb +2 -2
- data/lib/rubocop/cop/chef/deprecation/node_methods_not_attributes.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/node_set.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/node_set_unless.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/node_set_without_level.rb +2 -2
- data/lib/rubocop/cop/chef/deprecation/partial_search_class_usage.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/partial_search_helper_usage.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/poise_archive.rb +2 -2
- data/lib/rubocop/cop/chef/deprecation/require_recipe.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/resource_overrides_provides_method.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/resource_uses_dsl_name_method.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/resource_uses_provider_base_method.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/resource_uses_updated_method.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/resource_without_name_or_provides.rb +81 -0
- data/lib/rubocop/cop/chef/deprecation/ruby_block_create_action.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/run_command_helper.rb +3 -3
- data/lib/rubocop/cop/chef/deprecation/search_uses_positional_parameters.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/use_inline_resources.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/user_supports_property.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/verify_property_file_expansion.rb +1 -4
- data/lib/rubocop/cop/chef/deprecation/windows_feature_servermanagercmd.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/windows_task_change_action.rb +1 -4
- data/lib/rubocop/cop/chef/deprecation/xml_ruby_recipe.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/yum_dnf_compat_recipe.rb +1 -1
- data/lib/rubocop/cop/chef/modernize/databag_helpers.rb +58 -0
- data/lib/rubocop/cop/chef/modernize/powershell_guard_interpreter.rb +17 -8
- data/lib/rubocop/cop/chef/modernize/powershell_install_windowsfeature.rb +3 -3
- data/lib/rubocop/cop/chef/modernize/provides_initializer.rb +69 -0
- data/lib/rubocop/cop/chef/modernize/respond_to_provides.rb +5 -15
- data/lib/rubocop/cop/chef/modernize/use_multipackage_installs.rb +103 -0
- data/lib/rubocop/cop/chef/modernize/whyrun_supported_true.rb +2 -9
- data/lib/rubocop/cop/chef/modernize/windows_registry_uac.rb +2 -2
- data/lib/rubocop/cop/chef/redundant/apt_repository_distribution_default.rb +0 -1
- data/lib/rubocop/cop/chef/redundant/sensitive_property_in_resource.rb +3 -3
- data/lib/rubocop/monkey_patches/comment_config.rb +1 -1
- data/lib/rubocop/monkey_patches/config.rb +1 -1
- metadata +13 -5
- data/lib/rubocop/monkey_patches/json_formatter.rb +0 -20
@@ -45,7 +45,7 @@ module RuboCop
|
|
45
45
|
|
46
46
|
def on_send(node)
|
47
47
|
search_method?(node) do
|
48
|
-
add_offense(node, location: :expression, message: MSG, severity: :
|
48
|
+
add_offense(node, location: :expression, message: MSG, severity: :warning) if positional_arguments?(node)
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
if node.parent && node.parent.if_type? && %i(defined? respond_to?).include?(node.parent.children.first.method_name)
|
44
44
|
node = node.parent
|
45
45
|
end
|
46
|
-
add_offense(node, location: :expression, message: MSG, severity: :
|
46
|
+
add_offense(node, location: :expression, message: MSG, severity: :warning)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -47,7 +47,7 @@ module RuboCop
|
|
47
47
|
|
48
48
|
def on_block(node)
|
49
49
|
match_property_in_resource?(:user, 'supports', node) do |property|
|
50
|
-
add_offense(property, location: :expression, message: MSG, severity: :
|
50
|
+
add_offense(property, location: :expression, message: MSG, severity: :warning)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -34,15 +34,12 @@ module RuboCop
|
|
34
34
|
#
|
35
35
|
class VerifyPropertyUsesFileExpansion < Cop
|
36
36
|
include RuboCop::Chef::CookbookHelpers
|
37
|
-
extend TargetChefVersion
|
38
|
-
|
39
|
-
minimum_target_chef_version '12.5'
|
40
37
|
|
41
38
|
MSG = "Use the 'path' variable in the verify property and not the 'file' variable which was removed in Chef Infra Client 13.".freeze
|
42
39
|
|
43
40
|
def on_block(node)
|
44
41
|
match_property_in_resource?(nil, 'verify', node) do |verify|
|
45
|
-
add_offense(verify, location: :expression, message: MSG, severity: :
|
42
|
+
add_offense(verify, location: :expression, message: MSG, severity: :warning) if verify.source.match?(/%{file}/)
|
46
43
|
end
|
47
44
|
end
|
48
45
|
|
@@ -47,7 +47,7 @@ module RuboCop
|
|
47
47
|
|
48
48
|
def on_block(node)
|
49
49
|
match_property_in_resource?(:windows_feature, :install_method, node) do |prop_node|
|
50
|
-
add_offense(prop_node, location: :expression, message: MSG, severity: :
|
50
|
+
add_offense(prop_node, location: :expression, message: MSG, severity: :warning) if prop_node.source.match?(/:servermanagercmd/)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
@@ -41,9 +41,6 @@ module RuboCop
|
|
41
41
|
#
|
42
42
|
class WindowsTaskChangeAction < Cop
|
43
43
|
include RuboCop::Chef::CookbookHelpers
|
44
|
-
extend TargetChefVersion
|
45
|
-
|
46
|
-
minimum_target_chef_version '13.0'
|
47
44
|
|
48
45
|
MSG = 'The :change action in the windows_task resource was removed when windows_task was added to Chef Infra Client 13+. The default action of :create should can now be used to create an update tasks.'.freeze
|
49
46
|
|
@@ -77,7 +74,7 @@ module RuboCop
|
|
77
74
|
|
78
75
|
def check_action(ast_obj)
|
79
76
|
if ast_obj.respond_to?(:value) && ast_obj.value == :change
|
80
|
-
add_offense(ast_obj, location: :expression, message: MSG, severity: :
|
77
|
+
add_offense(ast_obj, location: :expression, message: MSG, severity: :warning)
|
81
78
|
end
|
82
79
|
end
|
83
80
|
end
|
@@ -38,7 +38,7 @@ module RuboCop
|
|
38
38
|
def on_send(node)
|
39
39
|
yum_dnf_compat_recipe_usage?(node) do
|
40
40
|
node = node.parent if node.parent&.conditional? && node.parent&.single_line?
|
41
|
-
add_offense(node, location: :expression, message: MSG, severity: :
|
41
|
+
add_offense(node, location: :expression, message: MSG, severity: :warning)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: 2020, Chef Software, Inc.
|
3
|
+
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
module RuboCop
|
18
|
+
module Cop
|
19
|
+
module Chef
|
20
|
+
module ChefModernize
|
21
|
+
# Use the `data_bag_item` helper instead of `Chef::DataBagItem.load` or `Chef::EncryptedDataBagItem.load`.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# plain_text_data = Chef::DataBagItem.load('foo', 'bar')
|
27
|
+
# encrypted_data = Chef::EncryptedDataBagItem.load('foo2', 'bar2')
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# plain_text_data = data_bag_item('foo', 'bar')
|
31
|
+
# encrypted_data = data_bag_item('foo2', 'bar2')
|
32
|
+
#
|
33
|
+
class DatabagHelpers < Cop
|
34
|
+
MSG = 'Use the `data_bag_item` helper instead of `Chef::DataBagItem.load` or `Chef::EncryptedDataBagItem.load`.'.freeze
|
35
|
+
|
36
|
+
def_node_matcher :data_bag_class_load?, <<-PATTERN
|
37
|
+
(send
|
38
|
+
(const
|
39
|
+
(const nil? :Chef) {:DataBagItem :EncryptedDataBagItem}) :load
|
40
|
+
...)
|
41
|
+
PATTERN
|
42
|
+
|
43
|
+
def on_send(node)
|
44
|
+
data_bag_class_load?(node) do
|
45
|
+
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def autocorrect(node)
|
50
|
+
lambda do |corrector|
|
51
|
+
corrector.replace(node.loc.expression, node.source.gsub(/Chef::(EncryptedDataBagItem|DataBagItem).load/, 'data_bag_item'))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# Copyright:: 2019, Chef Software, Inc.
|
2
|
+
# Copyright:: 2019-2020, Chef Software, Inc.
|
3
3
|
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
4
|
#
|
5
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -18,19 +18,28 @@ module RuboCop
|
|
18
18
|
module Cop
|
19
19
|
module Chef
|
20
20
|
module ChefModernize
|
21
|
-
# PowerShell is already set as the default guard interpreter for powershell_script resources in Chef Infra Client 13 and later and does not need to be specified.
|
21
|
+
# PowerShell is already set as the default guard interpreter for `powershell_script` and `batch` resources in Chef Infra Client 13 and later and does not need to be specified.
|
22
22
|
#
|
23
23
|
# @example
|
24
24
|
#
|
25
25
|
# # bad
|
26
|
-
# powershell_script '
|
27
|
-
# code "
|
26
|
+
# powershell_script 'Create Directory' do
|
27
|
+
# code "New-Item -ItemType Directory -Force -Path C:\mydir"
|
28
|
+
# guard_interpreter :powershell_script
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# batch 'Create Directory' do
|
32
|
+
# code "mkdir C:\mydir"
|
28
33
|
# guard_interpreter :powershell_script
|
29
34
|
# end
|
30
35
|
#
|
31
36
|
# # good
|
32
|
-
# powershell_script '
|
33
|
-
# code "
|
37
|
+
# powershell_script 'Create Directory' do
|
38
|
+
# code "New-Item -ItemType Directory -Force -Path C:\mydir"
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# batch 'Create Directory' do
|
42
|
+
# code "mkdir C:\mydir"
|
34
43
|
# end
|
35
44
|
#
|
36
45
|
class PowerShellGuardInterpreter < Cop
|
@@ -40,10 +49,10 @@ module RuboCop
|
|
40
49
|
|
41
50
|
minimum_target_chef_version '13.0'
|
42
51
|
|
43
|
-
MSG = 'PowerShell is already set as the default guard interpreter for powershell_script resources in Chef Infra Client 13 and later and does not need to be specified.'.freeze
|
52
|
+
MSG = 'PowerShell is already set as the default guard interpreter for `powershell_script` and `batch` resources in Chef Infra Client 13 and later and does not need to be specified.'.freeze
|
44
53
|
|
45
54
|
def on_block(node)
|
46
|
-
match_property_in_resource?(
|
55
|
+
match_property_in_resource?(%i(powershell_script batch), 'guard_interpreter', node) do |interpreter|
|
47
56
|
if interpreter.arguments.first.source == ':powershell_script'
|
48
57
|
add_offense(interpreter, location: :expression, message: MSG, severity: :refactor)
|
49
58
|
end
|
@@ -18,7 +18,7 @@ module RuboCop
|
|
18
18
|
module Cop
|
19
19
|
module Chef
|
20
20
|
module ChefModernize
|
21
|
-
# Use the windows_feature resource built into Chef Infra Client
|
21
|
+
# Use the windows_feature resource built into Chef Infra Client 15+ instead of the powershell_script resource
|
22
22
|
# to run Install-WindowsFeature or Add-WindowsFeature
|
23
23
|
#
|
24
24
|
# @example
|
@@ -38,9 +38,9 @@ module RuboCop
|
|
38
38
|
include RuboCop::Chef::CookbookHelpers
|
39
39
|
extend TargetChefVersion
|
40
40
|
|
41
|
-
minimum_target_chef_version '
|
41
|
+
minimum_target_chef_version '13.0'
|
42
42
|
|
43
|
-
MSG = 'Use the windows_feature resource built into Chef Infra Client
|
43
|
+
MSG = 'Use the windows_feature resource built into Chef Infra Client 13+ instead of using Install-WindowsFeature or Add-WindowsFeature in a powershell_script resource'.freeze
|
44
44
|
|
45
45
|
def on_block(node)
|
46
46
|
match_property_in_resource?(:powershell_script, 'code', node) do |code_property|
|
@@ -0,0 +1,69 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: 2020, Chef Software Inc.
|
3
|
+
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
module RuboCop
|
18
|
+
module Cop
|
19
|
+
module Chef
|
20
|
+
module ChefModernize
|
21
|
+
# Provides should be set using the `provides` resource DSL method instead of instead of setting @provides in the initialize method.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# def initialize(*args)
|
27
|
+
# super
|
28
|
+
# @provides = :foo
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# # good
|
32
|
+
# provides :foo
|
33
|
+
#
|
34
|
+
class ProvidesFromInitialize < Cop
|
35
|
+
include RangeHelp
|
36
|
+
|
37
|
+
MSG = 'Provides should be set using the `provides` resource DSL method instead of instead of setting @provides in the initialize method.'.freeze
|
38
|
+
|
39
|
+
def_node_matcher :provides_assignment?, <<-PATTERN
|
40
|
+
(ivasgn :@provides $(sym ...))
|
41
|
+
PATTERN
|
42
|
+
|
43
|
+
def on_ivasgn(node)
|
44
|
+
provides_assignment?(node) do
|
45
|
+
add_offense(node, location: :expression, message: MSG, severity: :refactor) if intialize_method(node.parent.parent).any?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def_node_search :provides_method?, '(send nil? :provides ... )'
|
50
|
+
|
51
|
+
def_node_search :intialize_method, '(def :initialize ... )'
|
52
|
+
|
53
|
+
def autocorrect(node)
|
54
|
+
lambda do |corrector|
|
55
|
+
# insert the new provides call above the initialize method, but not if one already exists (this is sadly common)
|
56
|
+
unless provides_method?(processed_source.ast)
|
57
|
+
initialize_node = intialize_method(processed_source.ast).first
|
58
|
+
corrector.insert_before(initialize_node.source_range, "provides #{node.descendants.first.source}\n\n")
|
59
|
+
end
|
60
|
+
|
61
|
+
# remove the variable from the initialize method
|
62
|
+
corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left))
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# Copyright:: 2019
|
2
|
+
# Copyright:: 2019, Chef Software, Inc.
|
3
3
|
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
4
|
#
|
5
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -18,20 +18,19 @@ module RuboCop
|
|
18
18
|
module Cop
|
19
19
|
module Chef
|
20
20
|
module ChefModernize
|
21
|
-
#
|
21
|
+
# It is not longer necessary respond_to?(:foo) in metadata. This was used to support new metadata
|
22
|
+
# methods in Chef 11 and early versions of Chef 12.
|
22
23
|
#
|
23
24
|
# @example
|
24
25
|
#
|
25
26
|
# # bad
|
26
27
|
# provides :foo if respond_to?(:provides)
|
27
28
|
#
|
28
|
-
# provides :foo if defined? provides
|
29
|
-
#
|
30
29
|
# # good
|
31
30
|
# provides :foo
|
32
31
|
#
|
33
32
|
class RespondToProvides < Cop
|
34
|
-
MSG = '
|
33
|
+
MSG = 'respond_to?(:provides) in resources is no longer necessary in Chef Infra Client 12+'.freeze
|
35
34
|
|
36
35
|
def on_if(node)
|
37
36
|
if_respond_to_provides?(node) do
|
@@ -40,16 +39,7 @@ module RuboCop
|
|
40
39
|
end
|
41
40
|
|
42
41
|
def_node_matcher :if_respond_to_provides?, <<~PATTERN
|
43
|
-
(if
|
44
|
-
{
|
45
|
-
(send nil? :respond_to?
|
46
|
-
(sym :provides))
|
47
|
-
|
48
|
-
(:defined?
|
49
|
-
(send nil? :provides))
|
50
|
-
}
|
51
|
-
(send nil? :provides
|
52
|
-
(sym _)) ... )
|
42
|
+
(if (send nil? :respond_to? ( :sym :provides ) ) ... )
|
53
43
|
PATTERN
|
54
44
|
|
55
45
|
def autocorrect(node)
|
@@ -0,0 +1,103 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: 2020, Chef Software, Inc.
|
3
|
+
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
module RuboCop
|
18
|
+
module Cop
|
19
|
+
module Chef
|
20
|
+
module ChefModernize
|
21
|
+
# Pass an array of packages to package resources instead of interating over an array of packages when using multi-package capable package subystem such as apt, yum, chocolatey, dnf, or zypper. Multipackage installs are faster and simplify logs.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# %w(bmon htop vim curl).each do |pkg|
|
27
|
+
# package pkg do
|
28
|
+
# action :install
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# # good
|
33
|
+
# package %w(bmon htop vim curl)
|
34
|
+
#
|
35
|
+
class UseMultipackageInstalls < Cop
|
36
|
+
MSG = 'Pass an array of packages to package resources instead of interating over an array of packages when using multi-package capable package subystem such as apt, yum, chocolatey, dnf, or zypper. Multipackage installs are faster and simplify logs.'.freeze
|
37
|
+
MULTIPACKAGE_PLATS = %w(debian redhat suse amazon fedora scientific oracle rhel ubuntu centos redhat).freeze
|
38
|
+
|
39
|
+
def_node_matcher :platform_or_platform_family?, <<-PATTERN
|
40
|
+
(send (send nil? :node) :[] (str {"platform" "platform_family"}) )
|
41
|
+
PATTERN
|
42
|
+
|
43
|
+
def_node_matcher :platform_helper?, <<-PATTERN
|
44
|
+
(if
|
45
|
+
(send nil? {:platform_family? :platform?} $... )
|
46
|
+
$(block
|
47
|
+
(send
|
48
|
+
$(array ... ) :each)
|
49
|
+
(args ... )
|
50
|
+
(block
|
51
|
+
(send nil? :package
|
52
|
+
(lvar ... ))
|
53
|
+
(args)
|
54
|
+
(send nil? :action
|
55
|
+
(sym :install)))) nil?)
|
56
|
+
PATTERN
|
57
|
+
|
58
|
+
def_node_matcher :package_array_install?, <<-PATTERN
|
59
|
+
(block
|
60
|
+
(send
|
61
|
+
$(array ... ) :each)
|
62
|
+
(args ... )
|
63
|
+
(block
|
64
|
+
(send nil? :package
|
65
|
+
(lvar ... ))
|
66
|
+
(args)
|
67
|
+
(send nil? :action
|
68
|
+
(sym :install))))
|
69
|
+
PATTERN
|
70
|
+
|
71
|
+
# see if all platforms in the when condition are multipackage compliant
|
72
|
+
def multipackage_platforms?(condition_obj)
|
73
|
+
condition_obj.all? do |p|
|
74
|
+
MULTIPACKAGE_PLATS.include?(p.value)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def on_when(node)
|
79
|
+
if platform_or_platform_family?(node.parent.condition) &&
|
80
|
+
package_array_install?(node.body) &&
|
81
|
+
multipackage_platforms?(node.conditions)
|
82
|
+
add_offense(node.body, location: :expression, message: MSG, severity: :refactor)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def on_if(node)
|
87
|
+
platform_helper?(node) do |plats, blk, _pkgs|
|
88
|
+
add_offense(blk, location: :expression, message: MSG, severity: :refactor) if multipackage_platforms?(plats)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def autocorrect(node)
|
93
|
+
package_array_install?(node) do |vals|
|
94
|
+
lambda do |corrector|
|
95
|
+
corrector.replace(node.loc.expression, "package #{vals.source}")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|