cookstyle 6.2.9 → 6.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -1
  3. data/config/cookstyle.yml +306 -15
  4. data/config/disable_all.yml +21 -3
  5. data/config/upstream.yml +141 -35
  6. data/lib/cookstyle.rb +1 -1
  7. data/lib/cookstyle/version.rb +2 -2
  8. data/lib/rubocop/chef/platform_helpers.rb +2 -1
  9. data/lib/rubocop/cop/chef/correctness/invalid_platform_family_values_in_case.rb +77 -0
  10. data/lib/rubocop/cop/chef/correctness/invalid_platform_values_in_case.rb +77 -0
  11. data/lib/rubocop/cop/chef/correctness/lazy_eval_node_attribute_defaults.rb +56 -0
  12. data/lib/rubocop/cop/chef/correctness/node_normal.rb +1 -1
  13. data/lib/rubocop/cop/chef/correctness/node_normal_unless.rb +1 -1
  14. data/lib/rubocop/cop/chef/correctness/openssl_password_helpers.rb +45 -0
  15. data/lib/rubocop/cop/chef/deprecation/depends_compat_resource.rb +1 -1
  16. data/lib/rubocop/cop/chef/deprecation/depends_partial_search.rb +1 -1
  17. data/lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb +10 -4
  18. data/lib/rubocop/cop/chef/deprecation/deprecated_shellout_methods.rb +65 -0
  19. data/lib/rubocop/cop/chef/deprecation/easy_install.rb +2 -2
  20. data/lib/rubocop/cop/chef/deprecation/erl_call.rb +1 -1
  21. data/lib/rubocop/cop/chef/deprecation/locale_lc_all_property.rb +2 -2
  22. data/lib/rubocop/cop/chef/deprecation/node_methods_not_attributes.rb +1 -1
  23. data/lib/rubocop/cop/chef/deprecation/node_set.rb +2 -3
  24. data/lib/rubocop/cop/chef/deprecation/node_set_unless.rb +2 -3
  25. data/lib/rubocop/cop/chef/deprecation/powershell_cookbook_helpers.rb +3 -3
  26. data/lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb +77 -0
  27. data/lib/rubocop/cop/chef/deprecation/ruby_27_keyword_argument_warnings.rb +59 -0
  28. data/lib/rubocop/cop/chef/deprecation/user_supports_property.rb +6 -1
  29. data/lib/rubocop/cop/chef/deprecation/xml_ruby_recipe.rb +3 -3
  30. data/lib/rubocop/cop/chef/modernize/includes_mixin_shellout.rb +24 -3
  31. data/lib/rubocop/cop/chef/modernize/respond_to_compile_time.rb +93 -0
  32. data/lib/rubocop/cop/chef/modernize/respond_to_provides.rb +15 -5
  33. data/lib/rubocop/cop/chef/modernize/shell_out_helper.rb +64 -0
  34. data/lib/rubocop/cop/chef/modernize/use_multipackage_installs.rb +8 -4
  35. data/lib/rubocop/cop/chef/style/overly_complex_supports_depends_metadata.rb +1 -1
  36. data/lib/rubocop/cop/target_chef_version.rb +4 -0
  37. data/lib/rubocop/monkey_patches/team.rb +24 -0
  38. metadata +14 -5
  39. data/lib/rubocop/monkey_patches/commissioner.rb +0 -26
@@ -29,7 +29,7 @@ module RuboCop
29
29
  # end
30
30
  #
31
31
  class ErlCallResource < Cop
32
- MSG = "Don't use the deprecated erl_call resource removed in Chef 13".freeze
32
+ MSG = "Don't use the deprecated erl_call resource removed in Chef Infra Client 13".freeze
33
33
 
34
34
  def on_send(node)
35
35
  add_offense(node, location: :expression, message: MSG, severity: :warning) if node.method_name == :erl_call
@@ -18,7 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefDeprecations
21
- # The local resource's lc_all property has been deprecated and will be removed in Chef Infra Client 16
21
+ # The local resource's lc_all property has been deprecated and will be removed in Chef Infra Client 17
22
22
  #
23
23
  # @example
24
24
  #
@@ -31,7 +31,7 @@ module RuboCop
31
31
  class LocaleDeprecatedLcAllProperty < Cop
32
32
  include RuboCop::Chef::CookbookHelpers
33
33
 
34
- MSG = "The local resource's lc_all property has been deprecated and will be removed in Chef Infra Client 16".freeze
34
+ MSG = "The local resource's lc_all property has been deprecated and will be removed in Chef Infra Client 17".freeze
35
35
 
36
36
  def on_block(node)
37
37
  match_property_in_resource?(:locale, 'lc_all', node) do |property|
@@ -18,7 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefDeprecations
21
- # Incorrectly using node methods for Ohai data when you really want node attributes
21
+ # Use node attributes to access data provided by Ohai instead of using node methods to access that data.
22
22
  #
23
23
  # @example
24
24
  #
@@ -17,10 +17,9 @@ module RuboCop
17
17
  module Cop
18
18
  module Chef
19
19
  module ChefDeprecations
20
- # The node.set method has been removed in Chef-13 and must be replaced by node.normal.
20
+ # The `node.set` method has been removed in Chef Infra Client 13 and usage must be replaced with `node.normal`.
21
21
  #
22
- # Note that node.normal keeps the semantics identical, but the use of node.normal is
23
- # also discouraged.
22
+ # This cop will autocorrect code to use node.normal, which is functionally identical to node.set, but we also discourage the use of that method as normal level attributes persist on the node even if the code setting the attribute is later removed.
24
23
  #
25
24
  # @example
26
25
  #
@@ -17,10 +17,9 @@ module RuboCop
17
17
  module Cop
18
18
  module Chef
19
19
  module ChefDeprecations
20
- # The node.set_unless method has been removed in Chef-13 and must be replaced by node.normal_unless.
20
+ # The node.set_unless method has been removed in Chef Infra Client 13 and usage must be replaced with node.normal_unless.
21
21
  #
22
- # Note that node.normal_unless keeps the semantics identical, but the use of node.normal is
23
- # also discouraged.
22
+ # This cop will autocorrect code to use node.normal_unless, which is functionally identical to node.set_unless, but we also discourage the use of that method as normal level attributes persist on the node even if the code setting the attribute is later removed.
24
23
  #
25
24
  # @example
26
25
  #
@@ -18,7 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefDeprecations
21
- # Use node['powershell']['version'] or the new powershell_version helper available in Chef Infra Client 16+ instead of the deprecated PowerShell cookbook helpers
21
+ # Use `node['powershell']['version']` or the new `powershell_version` helper available in Chef Infra Client 15.8+ instead of the deprecated PowerShell cookbook helpers
22
22
  #
23
23
  # @example
24
24
  #
@@ -28,11 +28,11 @@ module RuboCop
28
28
  # # good
29
29
  # node['powershell']['version'].to_f == 4.0
30
30
  #
31
- # # good (Chef Infra Client 16+)
31
+ # # better (Chef Infra Client 15.8+)
32
32
  # powershell_version == 4.0
33
33
  #
34
34
  class PowershellCookbookHelpers < Cop
35
- MSG = "Use node['powershell']['version'] or the new powershell_version helper available in Chef Infra Client 16+ instead of the deprecated PowerShell cookbook helpers.".freeze
35
+ MSG = "Use node['powershell']['version'] or the new powershell_version helper available in Chef Infra Client 15.8+ instead of the deprecated PowerShell cookbook helpers.".freeze
36
36
 
37
37
  def_node_matcher :ps_cb_helper?, <<-PATTERN
38
38
  (send
@@ -0,0 +1,77 @@
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 ChefDeprecations
21
+ # Starting with Chef Infra Client 16, using `resource_name` without also using `provides` will result in resource failures. Use `provides` to change the name of the resource instead and omit `resource_name` entirely if it matches the name Chef Infra Client automatically assigns based on COOKBOOKNAME_FILENAME.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # mycookbook/resources/myresource.rb:
27
+ # resource_name :mycookbook_myresource
28
+ #
29
+ class ResourceUsesOnlyResourceName < Cop
30
+ include RuboCop::Chef::CookbookHelpers
31
+ include RangeHelp
32
+
33
+ MSG = 'Starting with Chef Infra Client 16, using `resource_name` without also using `provides` will result in resource failures. Use `provides` to change the name of the resource instead and omit `resource_name` entirely if it matches the name Chef Infra Client automatically assigns based on COOKBOOKNAME_FILENAME.'.freeze
34
+
35
+ def_node_matcher :resource_name?, <<-PATTERN
36
+ (send nil? :resource_name (sym $_ ))
37
+ PATTERN
38
+
39
+ def_node_search :cb_name_match, <<~PATTERN
40
+ (send nil? :name (str $_))
41
+ PATTERN
42
+
43
+ def_node_search :provides_methods?, '(send nil? {:provides :chef_version_for_provides} ... )'
44
+
45
+ def cookbook_name
46
+ cb_path = File.expand_path(File.join(processed_source.file_path, '../..'))
47
+
48
+ if File.exist?(File.join(cb_path, 'metadata.rb'))
49
+ cb_metadata_ast = ProcessedSource.from_file(File.join(cb_path, 'metadata.rb'), @config.target_ruby_version).ast
50
+ cb_name_match(cb_metadata_ast).first
51
+ elsif File.exist?(File.join(cb_path, 'metadata.json')) # this exists only for supermarket files that lack metadata.rb
52
+ JSON.parse(File.read(File.join(cb_path, 'metadata.json')))['name']
53
+ end
54
+ end
55
+
56
+ def on_send(node)
57
+ resource_name?(node) do |_name|
58
+ add_offense(node, location: :expression, message: MSG, severity: :warning) unless provides_methods?(processed_source.ast)
59
+ end
60
+ end
61
+
62
+ def autocorrect(node)
63
+ lambda do |corrector|
64
+ resource_name?(node) do |name|
65
+ if name.to_s == "#{cookbook_name}_#{File.basename(processed_source.path, '.rb')}"
66
+ corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left))
67
+ else
68
+ corrector.replace(node.loc.expression, node.source.gsub('resource_name', 'provides'))
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,59 @@
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
+
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ module ChefDeprecations
22
+ # Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # shell_out!('hostnamectl status', { returns: [0, 1] })
28
+ # shell_out('hostnamectl status', { returns: [0, 1] })
29
+ #
30
+ # # good
31
+ # shell_out!('hostnamectl status', returns: [0, 1])
32
+ # shell_out('hostnamectl status', returns: [0, 1])
33
+ #
34
+ class Ruby27KeywordArgumentWarnings < Cop
35
+ include RuboCop::Chef::CookbookHelpers
36
+
37
+ MSG = 'Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings.'.freeze
38
+
39
+ def_node_matcher :positional_shellout?, <<-PATTERN
40
+ (send nil? {:shell_out :shell_out!} ... $(hash ... ))
41
+ PATTERN
42
+
43
+ def on_send(node)
44
+ positional_shellout?(node) do |h|
45
+ add_offense(h, location: :expression, message: MSG, severity: :refactor) if h.braces?
46
+ end
47
+ end
48
+
49
+ def autocorrect(node)
50
+ lambda do |corrector|
51
+ # @todo when we drop ruby 2.4 support we can convert to to just delete_prefix delete_suffix
52
+ corrector.replace(node.loc.expression, node.loc.expression.source.gsub(/^{/, '').gsub(/}$/, ''))
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -54,8 +54,13 @@ module RuboCop
54
54
  def autocorrect(node)
55
55
  lambda do |corrector|
56
56
  new_text = []
57
+
57
58
  node.arguments.first.each_pair do |k, v|
58
- new_text << "#{k.value} #{v.source}"
59
+ # account for a strange edge case where the person incorrectly makes "manage_home a method
60
+ # the code would be broken, but without this handling cookstyle would explode
61
+ key_value = (k.send_type? && k.method_name == :manage_home) ? 'manage_home' : k.value
62
+
63
+ new_text << "#{key_value} #{v.source}"
59
64
  end
60
65
 
61
66
  corrector.replace(node.loc.expression, new_text.join("\n "))
@@ -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,8 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefDeprecations
21
- # Do not include the deprecated xml::ruby recipe to install the nokogiri gem.
22
- # Chef Infra Client 12 and later ships with nokogiri included.
21
+ # Do not include the deprecated xml::ruby recipe to install the nokogiri gem. Chef Infra Client 12 and later ships with nokogiri included.
23
22
  #
24
23
  # @example
25
24
  #
@@ -35,6 +34,7 @@ module RuboCop
35
34
 
36
35
  def on_send(node)
37
36
  xml_ruby_recipe?(node) do
37
+ node = node.parent if node.parent&.conditional? && node.parent&.single_line_condition? # make sure we catch any inline conditionals
38
38
  add_offense(node, location: :expression, message: MSG, severity: :warning)
39
39
  end
40
40
  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");
@@ -42,13 +42,34 @@ module RuboCop
42
42
  (send nil? :require ( str {"chef/mixin/shell_out" "chef/mixin/powershell_out"} ))
43
43
  PATTERN
44
44
 
45
+ def_node_search :hwrp_classes?, <<-PATTERN
46
+ (class
47
+ (const ... )
48
+ {(const
49
+ (const
50
+ (const nil? :Chef) :Provider) :LWRPBase)
51
+ (const
52
+ (const nil? :Chef) :Provider)
53
+ }
54
+ ...)
55
+ PATTERN
56
+
57
+ def check_for_offenses(node)
58
+ containing_dir = File.basename(File.dirname(processed_source.path))
59
+
60
+ # only add offenses when we're in a custom resource or HWRP, but not a plain old library
61
+ if containing_dir == 'resources' || hwrp_classes?(processed_source.ast)
62
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
63
+ end
64
+ end
65
+
45
66
  def on_send(node)
46
67
  require_shellout?(node) do
47
- add_offense(node, location: :expression, message: MSG, severity: :refactor)
68
+ check_for_offenses(node)
48
69
  end
49
70
 
50
71
  include_shellout?(node) do
51
- add_offense(node, location: :expression, message: MSG, severity: :refactor)
72
+ check_for_offenses(node)
52
73
  end
53
74
  end
54
75
 
@@ -0,0 +1,93 @@
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
+ # There is no need to check if the chef_gem resource supports compile_time as Chef Infra Client 12.1 and later support the compile_time property.
22
+ #
23
+ # # bad
24
+ # chef_gem 'ultradns-sdk' do
25
+ # compile_time true if Chef::Resource::ChefGem.method_defined?(:compile_time)
26
+ # action :nothing
27
+ # end
28
+ #
29
+ # chef_gem 'ultradns-sdk' do
30
+ # compile_time true if Chef::Resource::ChefGem.instance_methods(false).include?(:compile_time)
31
+ # action :nothing
32
+ # end
33
+ #
34
+ # chef_gem 'ultradns-sdk' do
35
+ # compile_time true if respond_to?(:compile_time)
36
+ # action :nothing
37
+ # end
38
+ #
39
+ # # good
40
+ # chef_gem 'ultradns-sdk' do
41
+ # compile_time true
42
+ # action :nothing
43
+ # end
44
+ #
45
+ class RespondToCompileTime < Cop
46
+ extend TargetChefVersion
47
+
48
+ minimum_target_chef_version '12.1'
49
+
50
+ MSG = 'There is no need to check if the chef_gem resource supports compile_time as Chef Infra Client 12.1 and later support the compile_time property.'.freeze
51
+
52
+ def_node_matcher :compile_time_method_defined?, <<-PATTERN
53
+ (if
54
+ {
55
+ (send
56
+ (const
57
+ (const
58
+ (const nil? :Chef) :Resource) :ChefGem) :method_defined?
59
+ (sym :compile_time))
60
+
61
+ (send
62
+ (send
63
+ (const
64
+ (const
65
+ (const nil? :Chef) :Resource) :ChefGem) :instance_methods
66
+ (false)) :include?
67
+ (sym :compile_time))
68
+
69
+ (send nil? :respond_to?
70
+ (sym :compile_time))
71
+ }
72
+ (send nil? :compile_time
73
+ $(_)) nil?)
74
+ PATTERN
75
+
76
+ def on_if(node)
77
+ compile_time_method_defined?(node) do
78
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
79
+ end
80
+ end
81
+
82
+ def autocorrect(node)
83
+ lambda do |corrector|
84
+ compile_time_method_defined?(node) do |val|
85
+ corrector.replace(node.loc.expression, "compile_time #{val.source}")
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ 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,20 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefModernize
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.
21
+ # In Chef Infra Client 12+ is is no longer necessary to gate the use of the provides methods in resources with `if respond_to?(:provides)` or `if defined? provides`.
23
22
  #
24
23
  # @example
25
24
  #
26
25
  # # bad
27
26
  # provides :foo if respond_to?(:provides)
28
27
  #
28
+ # provides :foo if defined? provides
29
+ #
29
30
  # # good
30
31
  # provides :foo
31
32
  #
32
33
  class RespondToProvides < Cop
33
- MSG = 'respond_to?(:provides) in resources is no longer necessary in Chef Infra Client 12+'.freeze
34
+ MSG = 'Using `respond_to?(:provides)` or `if defined? provides` in resources is no longer necessary in Chef Infra Client 12+.'.freeze
34
35
 
35
36
  def on_if(node)
36
37
  if_respond_to_provides?(node) do
@@ -39,7 +40,16 @@ module RuboCop
39
40
  end
40
41
 
41
42
  def_node_matcher :if_respond_to_provides?, <<~PATTERN
42
- (if (send nil? :respond_to? ( :sym :provides ) ) ... )
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 _)) ... )
43
53
  PATTERN
44
54
 
45
55
  def autocorrect(node)