module ChefSpec::Extensions::Chef::Resource

Three concerns:

- no-op'ing so that the action does not run the provider
- tracking the actions that were performed
- auto registering helper methods

Public Class Methods

new(*args, &block) click to toggle source

Hooks for the stubs_for system

Calls superclass method
# File lib/chefspec/extensions/chef/resource.rb, line 17
def initialize(*args, &block)
  super(*args, &block)
  if $CHEFSPEC_MODE
    # Here be dragons.
    # If we're directly inside a `load_current_resource`, this is probably
    # something like `new_resource.class.new` so we want to call this a current_resource,
    # Otherwise it's probably a normal resource instantiation.
    mode = :resource
    mode = :current_value if caller.any? { |x| x.include?("`load_current_resource'") || x.include?("`load_after_resource'") }
    ChefSpec::API::StubsFor.setup_stubs_for(self, mode)
  end
end
prepended(base) click to toggle source

auto-registration

# File lib/chefspec/extensions/chef/resource.rb, line 121
def self.prepended(base)
  class << base
    prepend ClassMethods
  end
end

Public Instance Methods

dup() click to toggle source
Calls superclass method
# File lib/chefspec/extensions/chef/resource.rb, line 30
def dup
  return super unless $CHEFSPEC_MODE

  # Also here be dragons.
  super.tap do |dup_resource|
    # We're directly inside a load_current_resource, which is probably via
    # the load_current_value DSL system, so call this a current resource.
    ChefSpec::API::StubsFor.setup_stubs_for(dup_resource, :current_value) if caller.any? { |x| x.include?("`load_current_resource'") || x.include?("`load_after_resource'") }
  end
end
perform_action(action, options = {}) click to toggle source

tracking

# File lib/chefspec/extensions/chef/resource.rb, line 93
def perform_action(action, options = {})
  @performed_actions ||= {}
  @performed_actions[action.to_sym] ||= {}
  @performed_actions[action.to_sym].merge!(options)
end
performed_action(action) click to toggle source
# File lib/chefspec/extensions/chef/resource.rb, line 99
def performed_action(action)
  @performed_actions ||= {}
  @performed_actions[action.to_sym]
end
performed_action?(action) click to toggle source
# File lib/chefspec/extensions/chef/resource.rb, line 104
def performed_action?(action)
  if action == :nothing
    performed_actions.empty?
  else
    !!performed_action(action)
  end
end
performed_actions() click to toggle source
# File lib/chefspec/extensions/chef/resource.rb, line 112
def performed_actions
  @performed_actions ||= {}
  @performed_actions.keys
end
run_action(action, notification_type = nil, notifying_resource = nil) click to toggle source

mix of no-op and tracking concerns

Calls superclass method
# File lib/chefspec/extensions/chef/resource.rb, line 42
def run_action(action, notification_type = nil, notifying_resource = nil)
  return super unless $CHEFSPEC_MODE

  resolve_notification_references
  validate_action(action)

  Chef::Log.info("Processing #{self} action #{action} (#{defined_at})")

  ChefSpec::Coverage.add(self)

  unless should_skip?(action)
    if node.runner.step_into?(self)
      instance_eval { @not_if = []; @only_if = [] }
      super
    end

    if node.runner.compiling?
      perform_action(action, compile_time: true)
    else
      perform_action(action, converge_time: true)
    end
  end
end
shell_out(*args) click to toggle source
Calls superclass method
# File lib/chefspec/extensions/chef/resource.rb, line 82
def shell_out(*args)
  return super unless $CHEFSPEC_MODE

  raise ChefSpec::Error::ShellOutNotStubbed.new(args: args, type: "resource", resource: self)
end
shell_out_compacted(*args) click to toggle source
Calls superclass method
# File lib/chefspec/extensions/chef/resource.rb, line 70
def shell_out_compacted(*args)
  return super unless $CHEFSPEC_MODE

  raise ChefSpec::Error::ShellOutNotStubbed.new(args: args, type: "resource", resource: self)
end
shell_out_compacted!(*args) click to toggle source
Calls superclass method
# File lib/chefspec/extensions/chef/resource.rb, line 76
def shell_out_compacted!(*args)
  return super unless $CHEFSPEC_MODE

  shell_out_compacted(*args).tap(&:error!)
end