Module: ChefSpec::RemoveExistingLWRP

Defined in:
lib/chefspec/chef_backwards_compat.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object


26
27
28
29
30
31
# File 'lib/chefspec/chef_backwards_compat.rb', line 26

def self.extended(klass)
  class << klass
    alias_method :build_from_file_without_removal, :build_from_file
    alias_method :build_from_file, :build_from_file_with_removal
  end
end

Instance Method Details

#build_from_file_with_removal(cookbook_name, filename, run_context) ⇒ Chef::Provider

Override Chef provider to remove any existing LWRPs to suppress constant re-definition warnings.

Parameters:

  • cookbook_name (String)

    the name of the cookbook

  • filename (String)

    file to load as a LWRP

  • run_context (Chef::RunContext)

    context of a Chef Run

Returns:


46
47
48
49
50
51
52
# File 'lib/chefspec/chef_backwards_compat.rb', line 46

def build_from_file_with_removal(cookbook_name, filename, run_context)
  provider_name = filename_to_qualified_string(cookbook_name, filename)
  class_name    = convert_to_class_name(provider_name)

  remove_existing_lwrp(class_name)
  build_from_file_without_removal(cookbook_name, filename, run_context)
end

#remove_existing_lwrp(class_name) ⇒ Object

Remove any existing Chef provider or resource with the specified name.

Parameters:

  • class_name (String)

    The class name. Must be a valid constant name.


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chefspec/chef_backwards_compat.rb', line 60

def remove_existing_lwrp(class_name)
  [self, superclass].each do |resource_holder|
    look_in_parents = false
    if resource_holder.const_defined?(class_name, look_in_parents)
      old_class = resource_holder.send(:remove_const, class_name)

      if resource_holder.respond_to?(:resource_classes)
        resource_holder.resource_classes.delete(old_class)
      end
    end
  end
end