cookstyle 5.4.13 → 5.5.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/config/cookstyle.yml +231 -129
  3. data/lib/cookstyle.rb +1 -0
  4. data/lib/cookstyle/version.rb +1 -1
  5. data/lib/rubocop/chef/cookbook_helpers.rb +67 -0
  6. data/lib/rubocop/cop/chef/correctness/block_guard_clause_string_only.rb +34 -31
  7. data/lib/rubocop/cop/chef/correctness/cb_depends_on_self.rb +16 -14
  8. data/lib/rubocop/cop/chef/correctness/default_maintainer_metadata.rb +22 -20
  9. data/lib/rubocop/cop/chef/correctness/insecure_cookbook_url.rb +32 -30
  10. data/lib/rubocop/cop/chef/correctness/invalid_license_string.rb +454 -453
  11. data/lib/rubocop/cop/chef/correctness/invalid_platform_metadata.rb +57 -57
  12. data/lib/rubocop/cop/chef/correctness/metadata_missing_name.rb +18 -16
  13. data/lib/rubocop/cop/chef/correctness/name_property_and_required.rb +61 -59
  14. data/lib/rubocop/cop/chef/correctness/node_normal.rb +29 -27
  15. data/lib/rubocop/cop/chef/correctness/node_normal_unless.rb +29 -27
  16. data/lib/rubocop/cop/chef/correctness/node_save.rb +46 -0
  17. data/lib/rubocop/cop/chef/correctness/ohai_default_recipe.rb +47 -0
  18. data/lib/rubocop/cop/chef/correctness/property_with_default_and_required.rb +35 -33
  19. data/lib/rubocop/cop/chef/correctness/property_with_name_attribute.rb +29 -27
  20. data/lib/rubocop/cop/chef/correctness/resource_sets_internal_properties.rb +52 -0
  21. data/lib/rubocop/cop/chef/correctness/resource_sets_name_property.rb +51 -0
  22. data/lib/rubocop/cop/chef/correctness/resource_with_none_action.rb +52 -0
  23. data/lib/rubocop/cop/chef/correctness/service_resource.rb +26 -24
  24. data/lib/rubocop/cop/chef/correctness/tmp_path.rb +30 -28
  25. data/lib/rubocop/cop/chef/deprecation/attribute_metadata.rb +22 -21
  26. data/lib/rubocop/cop/chef/deprecation/chef_rest.rb +56 -0
  27. data/lib/rubocop/cop/chef/deprecation/chocolatey_package_uninstall_action.rb +58 -0
  28. data/lib/rubocop/cop/chef/deprecation/conflicts_metadata.rb +18 -16
  29. data/lib/rubocop/cop/chef/deprecation/depends_compat_resource.rb +21 -19
  30. data/lib/rubocop/cop/chef/deprecation/depends_partial_search.rb +17 -15
  31. data/lib/rubocop/cop/chef/deprecation/depends_poise.rb +17 -15
  32. data/lib/rubocop/cop/chef/deprecation/deprecated_mixins.rb +39 -37
  33. data/lib/rubocop/cop/chef/deprecation/easy_install.rb +15 -13
  34. data/lib/rubocop/cop/chef/deprecation/epic_fail.rb +24 -22
  35. data/lib/rubocop/cop/chef/deprecation/erl_call.rb +15 -13
  36. data/lib/rubocop/cop/chef/deprecation/launchd_deprecated_hash_property.rb +55 -0
  37. data/lib/rubocop/cop/chef/deprecation/legacy_yum_cookbook_recipes.rb +24 -22
  38. data/lib/rubocop/cop/chef/deprecation/locale_lc_all_property.rb +45 -0
  39. data/lib/rubocop/cop/chef/deprecation/long_description_metadata.rb +21 -19
  40. data/lib/rubocop/cop/chef/deprecation/node_methods_not_attributes.rb +55 -53
  41. data/lib/rubocop/cop/chef/deprecation/node_set.rb +27 -25
  42. data/lib/rubocop/cop/chef/deprecation/node_set_unless.rb +27 -25
  43. data/lib/rubocop/cop/chef/deprecation/provides_metadata.rb +18 -16
  44. data/lib/rubocop/cop/chef/deprecation/replaces_metadata.rb +18 -16
  45. data/lib/rubocop/cop/chef/deprecation/require_recipe.rb +23 -21
  46. data/lib/rubocop/cop/chef/deprecation/suggests_metadata.rb +18 -16
  47. data/lib/rubocop/cop/chef/deprecation/use_inline_resources.rb +56 -0
  48. data/lib/rubocop/cop/chef/deprecation/user_supports_property.rb +53 -0
  49. data/lib/rubocop/cop/chef/deprecation/xml_ruby_recipe.rb +51 -0
  50. data/lib/rubocop/cop/chef/deprecation/yum_dnf_compat_recipe.rb +51 -0
  51. data/lib/rubocop/cop/chef/effortless/data_bags.rb +13 -11
  52. data/lib/rubocop/cop/chef/effortless/search_used.rb +13 -11
  53. data/lib/rubocop/cop/chef/modernize/apt_default_recipe.rb +22 -20
  54. data/lib/rubocop/cop/chef/modernize/berksfile_source.rb +31 -29
  55. data/lib/rubocop/cop/chef/modernize/build_essential.rb +25 -23
  56. data/lib/rubocop/cop/chef/modernize/chef_14_resources.rb +27 -25
  57. data/lib/rubocop/cop/chef/modernize/defines_chefspec_matchers.rb +25 -23
  58. data/lib/rubocop/cop/chef/modernize/execute_apt_update.rb +19 -17
  59. data/lib/rubocop/cop/chef/modernize/includes_mixin_shellout.rb +27 -25
  60. data/lib/rubocop/cop/chef/modernize/libarchive_file.rb +41 -0
  61. data/lib/rubocop/cop/chef/modernize/minitest_handler_usage.rb +21 -19
  62. data/lib/rubocop/cop/chef/modernize/powershell_expand_archive.rb +48 -0
  63. data/lib/rubocop/cop/chef/modernize/powershell_install_package.rb +51 -0
  64. data/lib/rubocop/cop/chef/modernize/powershell_install_windowsfeature.rb +54 -0
  65. data/lib/rubocop/cop/chef/modernize/resource_set_or_return.rb +25 -23
  66. data/lib/rubocop/cop/chef/modernize/resource_with_allowed_actions.rb +77 -0
  67. data/lib/rubocop/cop/chef/modernize/resource_with_attributes.rb +37 -35
  68. data/lib/rubocop/cop/chef/modernize/respond_to_metadata.rb +54 -0
  69. data/lib/rubocop/cop/chef/modernize/respond_to_provides.rb +25 -23
  70. data/lib/rubocop/cop/chef/modernize/respond_to_resource_name.rb +26 -24
  71. data/lib/rubocop/cop/chef/{effortless/node_save.rb → modernize/seven_zip_archive.rb} +16 -16
  72. data/lib/rubocop/cop/chef/modernize/shellouts_to_chocolatey.rb +59 -0
  73. data/lib/rubocop/cop/chef/modernize/whyrun_supported_true.rb +20 -18
  74. data/lib/rubocop/cop/chef/modernize/windows_default_recipe.rb +23 -21
  75. data/lib/rubocop/cop/chef/modernize/windows_version_helper.rb +20 -18
  76. data/lib/rubocop/cop/chef/modernize/windows_zipfile.rb +15 -13
  77. data/lib/rubocop/cop/chef/style/attribute_keys.rb +59 -57
  78. data/lib/rubocop/cop/chef/style/comment_sentence_spacing.rb +14 -12
  79. data/lib/rubocop/cop/chef/style/comments_copyright_format.rb +65 -63
  80. data/lib/rubocop/cop/chef/style/comments_format.rb +48 -45
  81. data/lib/rubocop/cop/chef/style/file_mode.rb +32 -30
  82. metadata +24 -9
  83. data/lib/rubocop/cop/chef/deprecation/respond_to_metadata.rb +0 -53
  84. data/lib/rubocop/cop/chef/modernize/ohai_default_recipe.rb +0 -45
  85. data/lib/rubocop/cop/chef/modernize/resource_with_allowed_action.rb +0 -75
  86. data/lib/rubocop/cop/chef/modernize/use_inline_resources.rb +0 -54
  87. data/lib/rubocop/cop/chef/modernize/xml_ruby_recipe.rb +0 -49
  88. data/lib/rubocop/cop/chef/modernize/yum_dnf_compat_recipe.rb +0 -49
@@ -18,73 +18,73 @@
18
18
  module RuboCop
19
19
  module Cop
20
20
  module Chef
21
- # metadata.rb supports methods should contain valid platforms
22
- #
23
- # @example
24
- #
25
- # # bad
26
- # supports 'darwin'
27
- # supports 'mswin'
28
- #
29
- #
30
- # # good
31
- # supports 'mac_os_x'
32
- # supports 'windows'
33
- #
21
+ module ChefCorrectness
22
+ # metadata.rb supports methods should contain valid platforms.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # supports 'darwin'
28
+ # supports 'mswin'
29
+ #
30
+ # # good
31
+ # supports 'mac_os_x'
32
+ # supports 'windows'
33
+ #
34
+ class InvalidPlatformMetadata < Cop
35
+ COMMON_TYPOS = {
36
+ "aws": nil,
37
+ "archlinux": 'arch',
38
+ "amazonlinux": 'amazon',
39
+ "darwin": 'mac_os_x',
40
+ "debuan": nil,
41
+ "mingw32": 'windows',
42
+ "mswin": 'windows',
43
+ "macos": 'mac_os_x',
44
+ "macosx": 'mac_os_x',
45
+ "mac_os_x_server": 'mac_os_x',
46
+ "mint": 'linuxmint',
47
+ "linux": nil,
48
+ "oel": 'oracle',
49
+ "oraclelinux": 'oracle',
50
+ "rhel": 'redhat',
51
+ "schientific": 'scientific',
52
+ "scientificlinux": 'scientific',
53
+ "sles": 'suse',
54
+ "solaris": 'solaris2',
55
+ "ubundu": 'ubuntu',
56
+ "ubunth": 'ubuntu',
57
+ "ubunutu": 'ubuntu',
58
+ "windwos": 'windows',
59
+ "xcp": nil,
60
+ }.freeze
34
61
 
35
- class InvalidPlatformMetadata < Cop
36
- COMMON_TYPOS = {
37
- "aws": nil,
38
- "archlinux": 'arch',
39
- "amazonlinux": 'amazon',
40
- "darwin": 'mac_os_x',
41
- "debuan": nil,
42
- "mingw32": 'windows',
43
- "mswin": 'windows',
44
- "macos": 'mac_os_x',
45
- "macosx": 'mac_os_x',
46
- "mac_os_x_server": 'mac_os_x',
47
- "mint": 'linuxmint',
48
- "linux": nil,
49
- "oel": 'oracle',
50
- "oraclelinux": 'oracle',
51
- "rhel": 'redhat',
52
- "schientific": 'scientific',
53
- "scientificlinux": 'scientific',
54
- "sles": 'suse',
55
- "solaris": 'solaris2',
56
- "ubundu": 'ubuntu',
57
- "ubunth": 'ubuntu',
58
- "ubunutu": 'ubuntu',
59
- "windwos": 'windows',
60
- "xcp": nil,
61
- }.freeze
62
+ MSG = 'metadata.rb "supports" platform is invalid'.freeze
62
63
 
63
- MSG = 'metadata.rb "supports" platform is invalid'.freeze
64
+ def_node_matcher :supports?, '(send nil? :supports $str ...)'
64
65
 
65
- def_node_matcher :supports?, '(send nil? :supports $str ...)'
66
-
67
- def on_send(node)
68
- supports?(node) do |plat|
69
- if COMMON_TYPOS[plat.str_content.to_sym]
70
- add_offense(plat, location: :expression, message: MSG, severity: :refactor)
66
+ def on_send(node)
67
+ supports?(node) do |plat|
68
+ if COMMON_TYPOS[plat.str_content.to_sym]
69
+ add_offense(plat, location: :expression, message: MSG, severity: :refactor)
70
+ end
71
71
  end
72
72
  end
73
- end
74
73
 
75
- def autocorrect(node)
76
- correct_string = autocorrect_license_string(node.str_content)
77
- if correct_string
78
- lambda do |corrector|
79
- corrector.replace(node.loc.expression, "'#{correct_string}'")
74
+ def autocorrect(node)
75
+ correct_string = autocorrect_license_string(node.str_content)
76
+ if correct_string
77
+ lambda do |corrector|
78
+ corrector.replace(node.loc.expression, "'#{correct_string}'")
79
+ end
80
80
  end
81
81
  end
82
- end
83
82
 
84
- # private
83
+ # private
85
84
 
86
- def autocorrect_license_string(bad_string)
87
- COMMON_TYPOS[bad_string.delete(',').downcase.to_sym]
85
+ def autocorrect_license_string(bad_string)
86
+ COMMON_TYPOS[bad_string.delete(',').downcase.to_sym]
87
+ end
88
88
  end
89
89
  end
90
90
  end
@@ -25,29 +25,31 @@ module RuboCop
25
25
  # # good
26
26
  # name 'foo'
27
27
  #
28
- class MetadataMissingName < Cop
29
- include RangeHelp
28
+ module ChefCorrectness
29
+ class MetadataMissingName < Cop
30
+ include RangeHelp
30
31
 
31
- MSG = 'metadata.rb needs to include the name method'.freeze
32
+ MSG = 'metadata.rb needs to include the name method'.freeze
32
33
 
33
- def investigate(processed_source)
34
- return if processed_source.blank?
34
+ def investigate(processed_source)
35
+ return if processed_source.blank?
35
36
 
36
- # Using range similar to RuboCop::Cop::Naming::Filename (file_name.rb)
37
- range = source_range(processed_source.buffer, 1, 0)
37
+ # Using range similar to RuboCop::Cop::Naming::Filename (file_name.rb)
38
+ range = source_range(processed_source.buffer, 1, 0)
38
39
 
39
- add_offense(nil, location: range, message: MSG, severity: :refactor) unless cb_name(processed_source.ast).any?
40
- end
40
+ add_offense(nil, location: range, message: MSG, severity: :refactor) unless cb_name(processed_source.ast).any?
41
+ end
41
42
 
42
- def_node_search :cb_name, '(send nil? :name str ...)'
43
+ def_node_search :cb_name, '(send nil? :name str ...)'
43
44
 
44
- def autocorrect(_node)
45
- lambda do |_corrector|
46
- path = processed_source.path
47
- cb_name = File.basename(File.dirname(path))
45
+ def autocorrect(_node)
46
+ lambda do |_corrector|
47
+ path = processed_source.path
48
+ cb_name = File.basename(File.dirname(path))
48
49
 
49
- metadata = IO.read(path)
50
- IO.write(path, "name '#{cb_name}'\n" + metadata)
50
+ metadata = IO.read(path)
51
+ IO.write(path, "name '#{cb_name}'\n" + metadata)
52
+ end
51
53
  end
52
54
  end
53
55
  end
@@ -17,75 +17,77 @@
17
17
  module RuboCop
18
18
  module Cop
19
19
  module Chef
20
- # When using properties in a custom resource you shouldn't set a property to
21
- # be both required and a name_property. Name properties are a way to optionally
22
- # override the name given to the resource block in cookbook code. In your resource
23
- # code you use the name_property and if the user doesn't pass in anything to that
24
- # property its value will be populated with resource block's name. This
25
- # allows users to provide more friendly resource names for logging that give
26
- # additional context on the change being made.
27
- #
28
- # How about a nice example! Here we have a resource called ntp_config that has a
29
- # name_property of config_file. All throughout the code of this resource we'd
30
- # use new_resource.config_file when referencing the path to the config.
31
- #
32
- # We can use a friendly name for the block and specific a value to config_file
33
- # ntp_config 'Configure the main config file' do
34
- # config_file '/etc/ntp/ntp.conf'
35
- # action :create
36
- # end
37
- #
38
- # We can also just set the config path as the resource block and Chef will
39
- # make sure to pass this in as new_resource.config_file as well.
40
- # ntp_config '/etc/ntp/ntp.conf' do
41
- # action :create
42
- # end
43
- #
44
- # The core tenant of the name property feature is that these properties are optional
45
- # and making them required effectively turns off the functionality provided by name
46
- # properties. If the goal is to always require the user to pass the config_file property
47
- # then it should just be made a required property and not a name_property.
48
- #
49
- #
50
- # @example
51
- #
52
- #
53
- # # bad
54
- # property :config_file, String, required: true, name_property: true
55
- #
56
- #
57
- # # good
58
- # property :config_file, String, required: true
59
- class NamePropertyIsRequired < Cop
60
- MSG = 'Resource properties marked as name properties should not also be required properties'.freeze
20
+ module ChefCorrectness
21
+ # When using properties in a custom resource you shouldn't set a property to
22
+ # be both required and a name_property. Name properties are a way to optionally
23
+ # override the name given to the resource block in cookbook code. In your resource
24
+ # code you use the name_property and if the user doesn't pass in anything to that
25
+ # property its value will be populated with resource block's name. This
26
+ # allows users to provide more friendly resource names for logging that give
27
+ # additional context on the change being made.
28
+ #
29
+ # How about a nice example! Here we have a resource called ntp_config that has a
30
+ # name_property of config_file. All throughout the code of this resource we'd
31
+ # use new_resource.config_file when referencing the path to the config.
32
+ #
33
+ # We can use a friendly name for the block and specific a value to config_file
34
+ # ntp_config 'Configure the main config file' do
35
+ # config_file '/etc/ntp/ntp.conf'
36
+ # action :create
37
+ # end
38
+ #
39
+ # We can also just set the config path as the resource block and Chef will
40
+ # make sure to pass this in as new_resource.config_file as well.
41
+ # ntp_config '/etc/ntp/ntp.conf' do
42
+ # action :create
43
+ # end
44
+ #
45
+ # The core tenant of the name property feature is that these properties are optional
46
+ # and making them required effectively turns off the functionality provided by name
47
+ # properties. If the goal is to always require the user to pass the config_file property
48
+ # then it should just be made a required property and not a name_property.
49
+ #
50
+ #
51
+ # @example
52
+ #
53
+ #
54
+ # # bad
55
+ # property :config_file, String, required: true, name_property: true
56
+ #
57
+ # # good
58
+ # property :config_file, String, required: true
59
+ #
60
+ class NamePropertyIsRequired < Cop
61
+ MSG = 'Resource properties marked as name properties should not also be required properties'.freeze
61
62
 
62
- def on_send(node)
63
- if required_property?(node) && property_is_name_property?(node)
64
- add_offense(node, location: :expression, message: MSG, severity: :refactor)
63
+ def on_send(node)
64
+ if required_property?(node) && property_is_name_property?(node)
65
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
66
+ end
65
67
  end
66
- end
67
68
 
68
- private
69
+ private
69
70
 
70
- def property_is_name_property?(node)
71
- if node.method_name == :property
72
- node.arguments.each do |arg|
73
- if arg.type == :hash
74
- return true if arg.source.match?(/name_property:\s*true/)
71
+ def property_is_name_property?(node)
72
+ if node.method_name == :property
73
+ node.arguments.each do |arg|
74
+ if arg.type == :hash
75
+ return true if arg.source.match?(/name_property:\s*true/)
76
+ end
75
77
  end
78
+ false # no required: true found
76
79
  end
77
- false # no required: true found
78
80
  end
79
- end
80
81
 
81
- def required_property?(node)
82
- if node.method_name == :property
83
- node.arguments.each do |arg|
84
- if arg.type == :hash
85
- return true if arg.source.match?(/required:\s*true/)
82
+ def required_property?(node)
83
+ if node.method_name == :property
84
+ node.arguments.each do |arg|
85
+ if arg.type == :hash
86
+ return true if arg.source.match?(/required:\s*true/)
87
+ end
86
88
  end
89
+ false # no default: found
87
90
  end
88
- false # no default: found
89
91
  end
90
92
  end
91
93
  end
@@ -16,35 +16,37 @@
16
16
  module RuboCop
17
17
  module Cop
18
18
  module Chef
19
- # Normal attributes are discouraged since their semantics differ importantly from the
20
- # default and override levels. Their values persist in the node object even after
21
- # all code referencing them has been deleted, unlike default and override.
22
- #
23
- # Code should be updated to use default or override levels, but this will change
24
- # attribute merging behavior so needs to be validated manually and force_default or
25
- # force_override levels may need to be used in recipe code.
26
- #
27
- # @example
28
- #
29
- # # bad
30
- # node.normal['foo'] = true
31
- #
32
- # # good
33
- # node.default['foo'] = true
34
- # node.override['foo'] = true
35
- # node.force_default['foo'] = true
36
- # node.force_override['foo'] = true
37
- #
38
- class NodeNormal < Cop
39
- MSG = 'Do not use node.normal. Replace with default/override/force_default/force_override attribute levels.'.freeze
19
+ module ChefCorrectness
20
+ # Normal attributes are discouraged since their semantics differ importantly from the
21
+ # default and override levels. Their values persist in the node object even after
22
+ # all code referencing them has been deleted, unlike default and override.
23
+ #
24
+ # Code should be updated to use default or override levels, but this will change
25
+ # attribute merging behavior so needs to be validated manually and force_default or
26
+ # force_override levels may need to be used in recipe code.
27
+ #
28
+ # @example
29
+ #
30
+ # # bad
31
+ # node.normal['foo'] = true
32
+ #
33
+ # # good
34
+ # node.default['foo'] = true
35
+ # node.override['foo'] = true
36
+ # node.force_default['foo'] = true
37
+ # node.force_override['foo'] = true
38
+ #
39
+ class NodeNormal < Cop
40
+ MSG = 'Do not use node.normal. Replace with default/override/force_default/force_override attribute levels.'.freeze
40
41
 
41
- def_node_matcher :node_normal?, <<-PATTERN
42
- (send (send _ :node) :normal)
43
- PATTERN
42
+ def_node_matcher :node_normal?, <<-PATTERN
43
+ (send (send _ :node) :normal)
44
+ PATTERN
44
45
 
45
- def on_send(node)
46
- node_normal?(node) do
47
- add_offense(node, location: :expression, message: MSG, severity: :refactor)
46
+ def on_send(node)
47
+ node_normal?(node) do
48
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
49
+ end
48
50
  end
49
51
  end
50
52
  end
@@ -16,35 +16,37 @@
16
16
  module RuboCop
17
17
  module Cop
18
18
  module Chef
19
- # Normal attributes are discouraged since their semantics differ importantly from the
20
- # default and override levels. Their values persist in the node object even after
21
- # all code referencing them has been deleted, unlike default and override.
22
- #
23
- # Code should be updated to use default or override levels, but this will change
24
- # attribute merging behavior so needs to be validated manually and force_default or
25
- # force_override levels may need to be used in recipe code.
26
- #
27
- # @example
28
- #
29
- # # bad
30
- # node.normal_unless['foo'] = true
31
- #
32
- # # good
33
- # node.default_unless['foo'] = true
34
- # node.override_unless['foo'] = true
35
- # node.force_default_unless['foo'] = true
36
- # node.force_override_unless['foo'] = true
37
- #
38
- class NodeNormalUnless < Cop
39
- MSG = 'Do not use node.normal_unless. Replace with default/override/force_default/force_override attribute levels.'.freeze
19
+ module ChefCorrectness
20
+ # Normal attributes are discouraged since their semantics differ importantly from the
21
+ # default and override levels. Their values persist in the node object even after
22
+ # all code referencing them has been deleted, unlike default and override.
23
+ #
24
+ # Code should be updated to use default or override levels, but this will change
25
+ # attribute merging behavior so needs to be validated manually and force_default or
26
+ # force_override levels may need to be used in recipe code.
27
+ #
28
+ # @example
29
+ #
30
+ # # bad
31
+ # node.normal_unless['foo'] = true
32
+ #
33
+ # # good
34
+ # node.default_unless['foo'] = true
35
+ # node.override_unless['foo'] = true
36
+ # node.force_default_unless['foo'] = true
37
+ # node.force_override_unless['foo'] = true
38
+ #
39
+ class NodeNormalUnless < Cop
40
+ MSG = 'Do not use node.normal_unless. Replace with default/override/force_default/force_override attribute levels.'.freeze
40
41
 
41
- def_node_matcher :node_normal_unless?, <<-PATTERN
42
- (send (send _ :node) :normal_unless)
43
- PATTERN
42
+ def_node_matcher :node_normal_unless?, <<-PATTERN
43
+ (send (send _ :node) :normal_unless)
44
+ PATTERN
44
45
 
45
- def on_send(node)
46
- node_normal_unless?(node) do
47
- add_offense(node, location: :expression, message: MSG, severity: :refactor)
46
+ def on_send(node)
47
+ node_normal_unless?(node) do
48
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
49
+ end
48
50
  end
49
51
  end
50
52
  end
@@ -0,0 +1,46 @@
1
+ #
2
+ # Copyright:: Copyright 2019, 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 ChefCorrectness
21
+ # Don't use node.save to save partial node data to the Chef Infra Server mid-run unless it's
22
+ # absolutely necessary. Node.save can result in failed Chef Infra runs appearing in search and
23
+ # increases load on the Chef Infra Server."
24
+ #
25
+ # @example
26
+ #
27
+ # # bad
28
+ # node.save
29
+ #
30
+ class CookbookUsesNodeSave < Cop
31
+ MSG = "Don't use node.save to save partial node data to the Chef Infra Server mid-run unless it's absolutely necessary. Node.save can result in failed Chef Infra runs appearing in search and increases load on the Chef Infra Server.".freeze
32
+
33
+ def_node_matcher :node_save?, <<-PATTERN
34
+ (send (send nil? :node) :save)
35
+ PATTERN
36
+
37
+ def on_send(node)
38
+ node_save?(node) do
39
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end