Class: RuboCop::Cop::Chef::Deprecations::ResourceUsesOnlyResourceName

Inherits:
Base
  • Object
show all
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.

Examples:


# bad
# mycookbook/resources/myresource.rb
resource_name :mycookbook_myresource

# good
# mycookbook/resources/myresource.rb
provides :mycookbook_myresource

# or, to also support Chef Infra Client < 16
resource_name :mycookbook_myresource
provides :mycookbook_myresource

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

Methods included from RuboCop::Chef::CookbookHelpers

#each_action_symbol, #match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string

Methods inherited from Base

#target_chef_version

Instance Method Details

#cookbook_nameString

determine the cookbook name either by parsing metadata.rb or by parsing metadata.json

Returns:

  • (String)

    the cookbook name



55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb', line 55

def cookbook_name
  cb_path = File.expand_path(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



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb', line 76

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

Returns:

  • (TrueClass, FalseClass)


69
70
71
72
73
74
# File 'lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb', line 69

def valid_provides?(resource_name)
  provides_ast = provides(processed_source.ast)
  return false unless provides_ast

  provides_ast.include?(resource_name)
end