Class: RuboCop::Cop::Chef::Deprecations::ResourceWithoutUnifiedTrue

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetChefVersion
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/chef/deprecation/resource_without_unified_mode_true.rb

Overview

Chef Infra Client 15.3 and later include a new Unified Mode that simplifies the execution of resources by replace the traditional compile and converge phases with a single phase. Unified mode simplifies writing advanced resources and avoids confusing errors that often occur when mixing ruby and Chef Infra resources. Chef Infra Client 17.0 and later will begin warning that ‘unified_mode true` should be set in all resources to validate that they will continue to function in Chef Infra Client 18.0 (April 2022) when Unified Mode becomes the default.

Examples:


### incorrect
 resource_name :foo
 provides :foo

 action :create do
   # some action code
 end

### correct
 resource_name :foo
 provides :foo
 unified_mode true

 action :create do
   # some action code
 end

Constant Summary collapse

MSG =
'Set `unified_mode true` in Chef Infra Client 15.3+ custom resources to ensure they work correctly in Chef Infra Client 18 (April 2022) when Unified Mode becomes the default.'

Instance Method Summary collapse

Methods included from TargetChefVersion

minimum_target_chef_version, required_minimum_chef_version, support_target_chef_version?

Methods inherited from Base

#target_chef_version

Instance Method Details

#insert_below_provides(corrector) ⇒ Object



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

def insert_below_provides(corrector)
  provides_ast = provides(processed_source.ast).first
  if provides_ast
    corrector.insert_after(provides_ast, "\nunified_mode true")
    true
  end
end

#insert_below_resource_name(corrector) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/rubocop/cop/chef/deprecation/resource_without_unified_mode_true.rb', line 76

def insert_below_resource_name(corrector)
  resource_name_ast = resource_name(processed_source.ast).first
  if resource_name_ast
    corrector.insert_after(resource_name_ast, "\nunified_mode true")
    true
  end
end

#on_new_investigationObject



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

def on_new_investigation
  # gracefully fail if the resource is empty
  return if processed_source.ast.nil?

  # Using range similar to RuboCop::Cop::Naming::Filename (file_name.rb)
  return if unified_mode?(processed_source.ast)
  range = source_range(processed_source.buffer, 1, 0)
  add_offense(range, severity: :refactor) do |corrector|
    insert_below_provides(corrector) || insert_below_resource_name(corrector)
  end
end