cookstyle 6.12.6 → 6.15.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/config/cookstyle.yml +122 -41
  4. data/config/disable_all.yml +28 -4
  5. data/config/upstream.yml +96 -16
  6. data/cookstyle.gemspec +1 -0
  7. data/lib/cookstyle.rb +7 -7
  8. data/lib/cookstyle/version.rb +2 -2
  9. data/lib/rubocop/cop/chef/correctness/invalid_default_action.rb +48 -0
  10. data/lib/rubocop/cop/chef/correctness/invalid_platform_family_helper.rb +26 -3
  11. data/lib/rubocop/cop/chef/correctness/macos_userdefaults_invalid_type.rb +68 -0
  12. data/lib/rubocop/cop/chef/correctness/notifies_action_not_symbol.rb +4 -4
  13. data/lib/rubocop/cop/chef/correctness/supports_must_be_float.rb +52 -0
  14. data/lib/rubocop/cop/chef/deprecation/cheffile.rb +2 -2
  15. data/lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb +6 -5
  16. data/lib/rubocop/cop/chef/deprecation/eol_audit_mode.rb +2 -2
  17. data/lib/rubocop/cop/chef/deprecation/macos_userdefaults_global_property.rb +61 -0
  18. data/lib/rubocop/cop/chef/deprecation/poise_archive.rb +4 -2
  19. data/lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb +3 -3
  20. data/lib/rubocop/cop/chef/deprecation/ruby_27_keyword_argument_warnings.rb +6 -10
  21. data/lib/rubocop/cop/chef/effortless/node_environment.rb +1 -1
  22. data/lib/rubocop/cop/chef/effortless/node_roles.rb +3 -3
  23. data/lib/rubocop/cop/chef/modernize/action_method_in_resource.rb +57 -0
  24. data/lib/rubocop/cop/chef/modernize/allowed_actions_initializer.rb +2 -2
  25. data/lib/rubocop/cop/chef/modernize/cron_d_file_or_template.rb +88 -0
  26. data/lib/rubocop/cop/chef/modernize/default_action_initializer.rb +3 -3
  27. data/lib/rubocop/cop/chef/modernize/definitions.rb +1 -1
  28. data/lib/rubocop/cop/chef/modernize/provides_initializer.rb +3 -3
  29. data/lib/rubocop/cop/chef/modernize/resource_name_initializer.rb +2 -2
  30. data/lib/rubocop/cop/chef/modernize/use_multipackage_installs.rb +3 -3
  31. data/lib/rubocop/cop/chef/redundant/double_compile_time.rb +65 -0
  32. data/lib/rubocop/cop/chef/redundant/ohai_attribute_to_string.rb +3 -3
  33. data/lib/rubocop/cop/chef/sharing/empty_metadata_field.rb +1 -1
  34. data/lib/rubocop/cop/chef/sharing/include_property_descriptions.rb +5 -5
  35. data/lib/rubocop/cop/chef/sharing/include_resource_descriptions.rb +3 -3
  36. data/lib/rubocop/cop/chef/sharing/include_resource_examples.rb +2 -2
  37. data/lib/rubocop/cop/chef/style/file_mode.rb +38 -19
  38. data/lib/rubocop/cop/chef/style/unnecessary_platform_case_statement.rb +1 -1
  39. data/lib/rubocop/monkey_patches/{cop.rb → base.rb} +1 -1
  40. metadata +13 -6
@@ -32,8 +32,8 @@ module RuboCop
32
32
  # shell_out!('hostnamectl status', returns: [0, 1])
33
33
  # shell_out('hostnamectl status', returns: [0, 1])
34
34
  #
35
- class Ruby27KeywordArgumentWarnings < Cop
36
- include RuboCop::Chef::CookbookHelpers
35
+ class Ruby27KeywordArgumentWarnings < Base
36
+ extend RuboCop::Cop::AutoCorrector
37
37
 
38
38
  MSG = 'Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings.'
39
39
 
@@ -43,14 +43,10 @@ module RuboCop
43
43
 
44
44
  def on_send(node)
45
45
  positional_shellout?(node) do |h|
46
- add_offense(h, location: :expression, message: MSG, severity: :refactor) if h.braces?
47
- end
48
- end
49
-
50
- def autocorrect(node)
51
- lambda do |corrector|
52
- # @todo when we drop ruby 2.4 support we can convert to to just delete_prefix delete_suffix
53
- corrector.replace(node.loc.expression, node.loc.expression.source.gsub(/^{/, '').gsub(/}$/, ''))
46
+ next unless h.braces?
47
+ add_offense(h.loc.expression, message: MSG, severity: :refactor) do |corrector|
48
+ corrector.replace(h.loc.expression, h.loc.expression.source[1..-2])
49
+ end
54
50
  end
55
51
  end
56
52
  end
@@ -27,7 +27,7 @@ module RuboCop
27
27
  # node.environment == "production"
28
28
  # node.chef_environment == "production"
29
29
  #
30
- class CookbookUsesEnvironmments < Cop
30
+ class CookbookUsesEnvironments < Cop
31
31
  MSG = 'Cookbook uses environments, which cannot be used in Policyfiles or Effortless Infra'
32
32
 
33
33
  def on_send(node)
@@ -19,13 +19,13 @@ module RuboCop
19
19
  module Cop
20
20
  module Chef
21
21
  module ChefEffortless
22
- # Neither Policyfiles or Effortless Infra which is based on Policyfiles supports Chef Roles
22
+ # Neither Policyfiles or Effortless Infra which is based on Policyfiles supports Chef Infra Roles
23
23
  #
24
24
  # @example
25
25
  #
26
26
  # # bad
27
- # node.role?('webserver')
28
- # node.roles.include?('webserver')
27
+ # node.role?('web_server')
28
+ # node.roles.include?('web_server')
29
29
  #
30
30
  class CookbookUsesRoles < Cop
31
31
  MSG = 'Cookbook uses roles, which cannot be used in Policyfiles or Effortless Infra'
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: 2020, Chef Software, Inc.
4
+ # Author:: Tim Smith (<tsmith@chef.io>)
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ module ChefModernize
22
+ # Use the custom resource language's `action :my_action` blocks instead of creating actions with methods.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # def action_create
28
+ # # :create action code here
29
+ # end
30
+ #
31
+ # # good
32
+ # action :create do
33
+ # # :create action code here
34
+ # end
35
+ #
36
+ class ActionMethodInResource < Base
37
+ extend AutoCorrector
38
+
39
+ MSG = "Use the custom resource language's `action :my_action` blocks instead of creating actions with methods."
40
+
41
+ def_node_search :includes_poise?, '(send nil? :include (const nil? :Poise))'
42
+
43
+ def on_def(node)
44
+ return unless node.method_name.to_s.start_with?('action_') # when we stop support for Ruby < 2.7 the .to_s can go away here
45
+ return if node.arguments? # if they passed in arguments they may actually need this
46
+ return if node.parent && includes_poise?(node.parent)
47
+
48
+ add_offense(node.loc.expression, message: MSG, severity: :refactor) do |corrector|
49
+ # @todo when we drop ruby 2.4 support we can convert this to use delete_suffix
50
+ corrector.replace(node.loc.expression, node.source.gsub("def #{node.method_name}", "action :#{node.method_name.to_s.gsub(/^action_/, '')} do"))
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -56,13 +56,13 @@ module RuboCop
56
56
 
57
57
  def_node_search :action_methods?, '(send nil? {:actions :allowed_actions} ... )'
58
58
 
59
- def_node_search :intialize_method, '(def :initialize ... )'
59
+ def_node_search :initialize_method, '(def :initialize ... )'
60
60
 
61
61
  def autocorrect(node)
62
62
  lambda do |corrector|
63
63
  # insert the new allowed_actions call above the initialize method, but not if one already exists (this is sadly common)
64
64
  unless action_methods?(processed_source.ast)
65
- initialize_node = intialize_method(processed_source.ast).first
65
+ initialize_node = initialize_method(processed_source.ast).first
66
66
  corrector.insert_before(initialize_node.source_range, "allowed_actions #{node.descendants.first.source}\n\n")
67
67
  end
68
68
 
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: 2020, Chef Software, Inc.
4
+ # Author:: Tim Smith (<tsmith@chef.io>)
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ module ChefModernize
22
+ # Use the cron_d resource that ships with Chef Infra Client 14.4+ instead of manually creating the file with template, file, or cookbook_file resources.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # template '/etc/cron.d/backup' do
28
+ # source 'cron_backup_job.erb'
29
+ # owner 'root'
30
+ # group 'root'
31
+ # mode '644'
32
+ # end
33
+ #
34
+ # cookbook_file '/etc/cron.d/backup' do
35
+ # owner 'root'
36
+ # group 'root'
37
+ # mode '644'
38
+ # end
39
+ #
40
+ # file '/etc/cron.d/backup' do
41
+ # content '*/30 * * * * backup /usr/local/bin/backup_script.sh'
42
+ # owner 'root'
43
+ # group 'root'
44
+ # mode '644'
45
+ # end
46
+ #
47
+ # file '/etc/cron.d/blogs' do
48
+ # action :delete
49
+ # end
50
+ #
51
+ # # good
52
+ # cron_d 'backup' do
53
+ # minute '1'
54
+ # hour '1'
55
+ # mailto 'sysadmins@example.com'
56
+ # command '/usr/local/bin/backup_script.sh'
57
+ # end
58
+ #
59
+ # cron_d 'blogs' do
60
+ # action :delete
61
+ # end
62
+ #
63
+ class CronDFileOrTemplate < Cop
64
+ extend TargetChefVersion
65
+
66
+ minimum_target_chef_version '14.4'
67
+
68
+ MSG = 'Use the cron_d resource that ships with Chef Infra Client 14.4+ instead of manually creating the file with template, file, or cookbook_file resources'
69
+
70
+ def_node_matcher :file_or_template?, <<-PATTERN
71
+ (block
72
+ (send nil? {:template :file :cookbook_file}
73
+ (str $_))
74
+ ...
75
+ )
76
+ PATTERN
77
+
78
+ def on_block(node)
79
+ file_or_template?(node) do |file_name|
80
+ return unless file_name.start_with?('/etc/cron.d/')
81
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -47,13 +47,13 @@ module RuboCop
47
47
  (ivasgn {:@action :@default_action} $(...))
48
48
  PATTERN
49
49
 
50
- def_node_search :intialize_method, '(def :initialize ... )'
50
+ def_node_search :initialize_method, '(def :initialize ... )'
51
51
 
52
52
  def_node_search :default_action_method?, '(send nil? :default_action ... )'
53
53
 
54
54
  def on_ivasgn(node)
55
55
  action_variable_assignment?(node) do
56
- add_offense(node, location: :expression, message: MSG, severity: :refactor) if intialize_method(node.parent.parent)
56
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if initialize_method(node.parent.parent)
57
57
  end
58
58
  end
59
59
 
@@ -61,7 +61,7 @@ module RuboCop
61
61
  lambda do |corrector|
62
62
  # insert the new default_action call above the initialize method, but not if one already exists (this is sadly common)
63
63
  unless default_action_method?(processed_source.ast)
64
- initialize_node = intialize_method(processed_source.ast).first
64
+ initialize_node = initialize_method(processed_source.ast).first
65
65
  corrector.insert_before(initialize_node.source_range, "default_action #{node.descendants.first.source}\n\n")
66
66
  end
67
67
 
@@ -19,7 +19,7 @@ module RuboCop
19
19
  module Cop
20
20
  module Chef
21
21
  module ChefModernize
22
- # In 2016 with Chef Infra Client 12.5 Custom Resources were introduced as a way of writing reusable resource code that could be shipped in cookbooks. Custom Resources offer many advantages of legacy Definitions including unit testing with ChefSpec, input validation, actions, commmon properties like not_if/only_if, and resource reporting.
22
+ # In 2016 with Chef Infra Client 12.5 Custom Resources were introduced as a way of writing reusable resource code that could be shipped in cookbooks. Custom Resources offer many advantages of legacy Definitions including unit testing with ChefSpec, input validation, actions, common properties like not_if/only_if, and resource reporting.
23
23
  #
24
24
  class Definitions < Cop
25
25
  include RuboCop::Chef::CookbookHelpers
@@ -43,19 +43,19 @@ module RuboCop
43
43
 
44
44
  def on_ivasgn(node)
45
45
  provides_assignment?(node) do
46
- add_offense(node, location: :expression, message: MSG, severity: :refactor) if intialize_method(node.parent.parent).any?
46
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if initialize_method(node.parent.parent).any?
47
47
  end
48
48
  end
49
49
 
50
50
  def_node_search :provides_method?, '(send nil? :provides ... )'
51
51
 
52
- def_node_search :intialize_method, '(def :initialize ... )'
52
+ def_node_search :initialize_method, '(def :initialize ... )'
53
53
 
54
54
  def autocorrect(node)
55
55
  lambda do |corrector|
56
56
  # insert the new provides call above the initialize method, but not if one already exists (this is sadly common)
57
57
  unless provides_method?(processed_source.ast)
58
- initialize_node = intialize_method(processed_source.ast).first
58
+ initialize_node = initialize_method(processed_source.ast).first
59
59
  corrector.insert_before(initialize_node.source_range, "provides #{node.descendants.first.source}\n\n")
60
60
  end
61
61
 
@@ -48,12 +48,12 @@ module RuboCop
48
48
  end
49
49
  end
50
50
 
51
- def_node_search :intialize_method, '(def :initialize ... )'
51
+ def_node_search :initialize_method, '(def :initialize ... )'
52
52
 
53
53
  def autocorrect(node)
54
54
  lambda do |corrector|
55
55
  # insert the new resource_name call above the initialize method
56
- initialize_node = intialize_method(processed_source.ast).first
56
+ initialize_node = initialize_method(processed_source.ast).first
57
57
  corrector.insert_before(initialize_node.source_range, "resource_name #{node.descendants.first.source}\n\n")
58
58
 
59
59
  # remove the variable from the initialize method
@@ -19,7 +19,7 @@ module RuboCop
19
19
  module Cop
20
20
  module Chef
21
21
  module ChefModernize
22
- # 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
+ # Pass an array of packages to package resources instead of iterating over an array of packages when using multi-package capable package subsystem such as apt, yum, chocolatey, dnf, or zypper. Multi-package installs are faster and simplify logs.
23
23
  #
24
24
  # @example
25
25
  #
@@ -34,7 +34,7 @@ module RuboCop
34
34
  # package %w(bmon htop vim curl)
35
35
  #
36
36
  class UseMultipackageInstalls < Cop
37
- 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.'
37
+ MSG = 'Pass an array of packages to package resources instead of iterating over an array of packages when using multi-package capable package subsystem such as apt, yum, chocolatey, dnf, or zypper. Multi-package installs are faster and simplify logs.'
38
38
  MULTIPACKAGE_PLATS = %w(debian redhat suse amazon fedora scientific oracle rhel ubuntu centos redhat).freeze
39
39
 
40
40
  def_node_matcher :platform_or_platform_family?, <<-PATTERN
@@ -73,7 +73,7 @@ module RuboCop
73
73
  (lvar _))})
74
74
  PATTERN
75
75
 
76
- # see if all platforms in the when condition are multipackage compliant
76
+ # see if all platforms in the when condition are multi-package compliant
77
77
  def multipackage_platforms?(condition_obj)
78
78
  condition_obj.all? do |p|
79
79
  MULTIPACKAGE_PLATS.include?(p.value)
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: 2020, Chef Software, Inc.
4
+ # Author:: Tim Smith (<tsmith@chef.io>)
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ module ChefRedundantCode
22
+ # If a resource includes the `compile_time` property there's no need to also use `.run_action(:some_action)` on the resource block
23
+ #
24
+ # # bad
25
+ # chef_gem 'deep_merge' do
26
+ # action :nothing
27
+ # compile_time true
28
+ # end.run_action(:install)
29
+ #
30
+ # # good
31
+ # chef_gem 'deep_merge' do
32
+ # action :install
33
+ # compile_time true
34
+ # end
35
+ #
36
+ class DoubleCompileTime < Base
37
+ extend RuboCop::Cop::AutoCorrector
38
+
39
+ MSG = "If a resource includes the `compile_time` property there's no need to also use `.run_action(:some_action)` on the resource block."
40
+
41
+ def_node_matcher :compile_time_and_run_action?, <<-PATTERN
42
+ (send
43
+ $(block
44
+ (send nil? ... )
45
+ (args)
46
+ (begin <
47
+ (send nil? :action (sym $_) )
48
+ (send nil? :compile_time (true) )
49
+ ...
50
+ >)
51
+ ) :run_action (sym $_) )
52
+ PATTERN
53
+
54
+ def on_send(node)
55
+ compile_time_and_run_action?(node) do |resource, action, run_action|
56
+ add_offense(node.loc.selector, message: MSG, severity: :refactor) do |corrector|
57
+ corrector.replace(node.loc.expression, resource.source.gsub(action.to_s, run_action.to_s))
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -45,18 +45,18 @@ module RuboCop
45
45
  class OhaiAttributeToString < Cop
46
46
  MSG = "This Ohai node attribute is already a string and doesn't need to be converted"
47
47
 
48
- def_node_matcher :platform_to_s?, <<-PATTERN
48
+ def_node_matcher :attribute_to_s?, <<-PATTERN
49
49
  (send (send (send nil? :node) :[] $(str {"platform" "platform_family" "platform_version" "fqdn" "hostname" "os" "name"}) ) :to_s )
50
50
  PATTERN
51
51
 
52
52
  def on_send(node)
53
- platform_to_s?(node) do
53
+ attribute_to_s?(node) do
54
54
  add_offense(node, location: :expression, message: MSG, severity: :refactor)
55
55
  end
56
56
  end
57
57
 
58
58
  def autocorrect(node)
59
- platform_to_s?(node) do |method|
59
+ attribute_to_s?(node) do |method|
60
60
  lambda do |corrector|
61
61
  corrector.replace(node.loc.expression, "node['#{method.value}']")
62
62
  end
@@ -31,7 +31,7 @@ module RuboCop
31
31
  # license 'Apache-2.0'
32
32
  #
33
33
  class EmptyMetadataField < Cop
34
- MSG = 'Cookbook metadata.rb contains an field with an empty string.'
34
+ MSG = 'Cookbook metadata.rb contains a field with an empty string.'
35
35
 
36
36
  def_node_matcher :field?, '(send nil? _ $str ...)'
37
37
 
@@ -19,7 +19,7 @@ module RuboCop
19
19
  module Cop
20
20
  module Chef
21
21
  module ChefSharing
22
- # Resource properties should include description fields to allow automated documention. Requires Chef Infra Client 13.9 or later.
22
+ # Resource properties should include description fields to allow automated documentation. Requires Chef Infra Client 13.9 or later.
23
23
  #
24
24
  # @example
25
25
  #
@@ -29,22 +29,22 @@ module RuboCop
29
29
  # # good
30
30
  # property :foo, String, description: "Set the important thing to..."
31
31
  #
32
- class IncludePropertyDescriptions < Cop
32
+ class IncludePropertyDescriptions < Base
33
33
  extend TargetChefVersion
34
34
 
35
35
  minimum_target_chef_version '13.9'
36
36
 
37
- MSG = 'Resource properties should include description fields to allow automated documention. Requires Chef Infra Client 13.9 or later.'
37
+ MSG = 'Resource properties should include description fields to allow automated documentation. Requires Chef Infra Client 13.9 or later.'
38
38
 
39
39
  # any method named property being called with a symbol argument and anything else
40
40
  def_node_matcher :property?, '(send nil? :property (sym _) ...)'
41
41
 
42
42
  # hash that contains description in any order (that's the <> bit)
43
- def_node_search :description_hash, '(hash <(pair (sym :description) ...) ...>)'
43
+ def_node_search :description_hash?, '(hash <(pair (sym :description) ...) ...>)'
44
44
 
45
45
  def on_send(node)
46
46
  property?(node) do
47
- add_offense(node, location: :expression, message: MSG, severity: :refactor) unless description_hash(processed_source.ast).any?
47
+ add_offense(node.loc.expression, message: MSG, severity: :refactor) unless description_hash?(node)
48
48
  end
49
49
  end
50
50
  end