Class: RuboCop::Cop::Chef::Deprecations::ResourceUsesOnlyResourceName
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp, RuboCop::Chef::CookbookHelpers
- Defined in:
- lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb
Overview
Starting with Chef Infra Client 16, using ‘resource_name` without also using `provides` will result in resource failures. Make sure to use both `resource_name` and `provides` to change the name of the resource. You can also omit `resource_name` entirely if the value set matches the name Chef Infra Client automatically assigns based on COOKBOOKNAME_FILENAME.
Constant Summary collapse
- MSG =
'Starting with Chef Infra Client 16, using `resource_name` without also using `provides` will result in resource failures. Make sure to use both `resource_name` and `provides` to change the name of the resource. You can also omit `resource_name` entirely if the value set matches the name Chef Infra Client automatically assigns based on COOKBOOKNAME_FILENAME.'
- RESTRICT_ON_SEND =
[:resource_name].freeze
Instance Method Summary collapse
-
#cookbook_name ⇒ String
determine the cookbook name either by parsing metadata.rb or by parsing metadata.json.
- #on_send(node) ⇒ Object
-
#valid_provides?(resource_name) ⇒ TrueClass, FalseClass
given a resource name make sure there’s a provides that matches that name.
Methods included from RuboCop::Chef::CookbookHelpers
#match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string
Methods inherited from Base
Instance Method Details
#cookbook_name ⇒ String
determine the cookbook name either by parsing metadata.rb or by parsing metadata.json
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb', line 47 def cookbook_name cb_path = File.(File.join(processed_source.file_path, '../..')) if File.exist?(File.join(cb_path, 'metadata.rb')) = ProcessedSource.from_file(File.join(cb_path, 'metadata.rb'), @config.target_ruby_version).ast cb_name_match().first elsif File.exist?(File.join(cb_path, 'metadata.json')) # this exists only for supermarket files that lack metadata.rb JSON.parse(File.read(File.join(cb_path, 'metadata.json')))['name'] end end |
#on_send(node) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb', line 68 def on_send(node) resource_name?(node) do |name| return if valid_provides?(name) add_offense(node, severity: :warning) do |corrector| if name.to_s == "#{cookbook_name}_#{File.basename(processed_source.path, '.rb')}" corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left)) else corrector.insert_after(node.source_range, "\n#{node.source.gsub('resource_name', 'provides')}") end end end end |
#valid_provides?(resource_name) ⇒ TrueClass, FalseClass
given a resource name make sure there’s a provides that matches that name
61 62 63 64 65 66 |
# File 'lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb', line 61 def valid_provides?(resource_name) provides_ast = provides(processed_source.ast) return false unless provides_ast provides_ast.include?(resource_name) end |