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



121
122
123
124
125
# File 'lib/chefspec/extensions/chef/resource.rb', line 121

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.
  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



93
94
95
96
97
# 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) ⇒ Object



99
100
101
102
# File 'lib/chefspec/extensions/chef/resource.rb', line 99

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

#performed_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
# 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_actionsObject



112
113
114
115
# 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) ⇒ 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
64
# 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



82
83
84
85
86
# 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) ⇒ Object



70
71
72
73
74
# 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) ⇒ Object



76
77
78
79
80
# 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