Module: ChefSpec::Extensions::Chef::Resource

Defined in:
lib/chefspec/extensions/chef/resource.rb

Overview

Three concerns:

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object

auto-registration


117
118
119
120
121
# File 'lib/chefspec/extensions/chef/resource.rb', line 117

def self.prepended(base)
  class << base
    prepend ClassMethods
  end
end

Instance Method Details

#dupObject


30
31
32
33
34
35
36
37
38
39
# File 'lib/chefspec/extensions/chef/resource.rb', line 30

def dup
  return super unless $CHEFSPEC_MODE
  # Also here be dragons.
  stack = caller
  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

#initialize(*args, &block) ⇒ Object

Hooks for the stubs_for system


17
18
19
20
21
22
23
24
25
26
27
28
# 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

#perform_action(action, options = {}) ⇒ Object

tracking


89
90
91
92
93
# File 'lib/chefspec/extensions/chef/resource.rb', line 89

def perform_action(action, options = {})
  @performed_actions ||= {}
  @performed_actions[action.to_sym] ||= {}
  @performed_actions[action.to_sym].merge!(options)
end

#performed_action(action) ⇒ Object


95
96
97
98
# File 'lib/chefspec/extensions/chef/resource.rb', line 95

def performed_action(action)
  @performed_actions ||= {}
  @performed_actions[action.to_sym]
end

#performed_action?(action) ⇒ Boolean

Returns:

  • (Boolean)

100
101
102
103
104
105
106
# File 'lib/chefspec/extensions/chef/resource.rb', line 100

def performed_action?(action)
  if action == :nothing
    performed_actions.empty?
  else
    !!performed_action(action)
  end
end

#performed_actionsObject


108
109
110
111
# File 'lib/chefspec/extensions/chef/resource.rb', line 108

def performed_actions
  @performed_actions ||= {}
  @performed_actions.keys
end

#run_action(action, notification_type = nil, notifying_resource = nil) ⇒ Object

mix of no-op and tracking concerns


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object


79
80
81
82
# File 'lib/chefspec/extensions/chef/resource.rb', line 79

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) ⇒ Object


69
70
71
72
# File 'lib/chefspec/extensions/chef/resource.rb', line 69

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) ⇒ Object


74
75
76
77
# File 'lib/chefspec/extensions/chef/resource.rb', line 74

def shell_out_compacted!(*args)
  return super unless $CHEFSPEC_MODE
  shell_out_compacted(*args).tap {|c| c.error! }
end