module ChefSpec::API::StubsFor

Constants

HAS_SHELLOUT_COMPACTED

Which version to use the shell_out_compacted hook on.

Public Class Methods

setup_stubs_for(object, type) click to toggle source

Hook used in the monkey patches to set up a place to inject stubs when needed for a resource or provider.

@api private @param object [Chef::Resource, Chef::Provider] Resource or provider to inject @param type [Symbol] Type of object to register stubs on @return [void]

# File lib/chefspec/api/stubs_for.rb, line 20
def self.setup_stubs_for(object, type)
  # This space left intentionally blank, real implementation is below.
end

Public Instance Methods

receive_shell_out(*cmd, stdout: "", stderr: "", exitstatus: 0, **opts) click to toggle source
# File lib/chefspec/api/stubs_for.rb, line 89
def receive_shell_out(*cmd, stdout: "", stderr: "", exitstatus: 0, **opts)
  # Ruby does not allow constructing an actual exitstatus object from Ruby code. Really.
  fake_exitstatus = double(exitstatus: exitstatus)
  fake_cmd = Mixlib::ShellOut.new(*cmd)
  fake_cmd.define_singleton_method(:run_command) {} # Do nothing, just in case.
  # Inject our canned data.
  fake_cmd.instance_exec do
    @stdout = stdout
    @stderr = stderr
    @status = fake_exitstatus
  end
  # On newer Chef, we can intercept using the new, better shell_out_compact hook point.
  shell_out_method ||= if HAS_SHELLOUT_COMPACTED.satisfied_by?(Gem::Version.create(Chef::VERSION))
                         :shell_out_compacted
                       else
                         :shell_out
                       end
  with_args = cmd + (opts.empty? ? [any_args] : [hash_including(opts)])
  receive(shell_out_method).with(*with_args).and_return(fake_cmd)
end
stubs_for_current_resource(target = nil, &block)
stubs_for_current_value(target = nil, &block) click to toggle source

Register stubs for current_value objects.

@see stubs_for_resource @param target [String, nil] Resource name to inject, or nil for all resources. @param block [Proc] A block taking the resource object as a parameter. @return [void]

# File lib/chefspec/api/stubs_for.rb, line 74
def stubs_for_current_value(target = nil, &block)
  _chefspec_stubs_for_registry[:current_value][target] << block
end
Also aliased as: stubs_for_current_resource
stubs_for_provider(target = nil, &block) click to toggle source

Register stubs for provider objects.

@see stubs_for_resource @param target [String, nil] Resource name to inject, or nil for all providers. @param block [Proc] A block taking the resource object as a parameter. @return [void]

# File lib/chefspec/api/stubs_for.rb, line 85
def stubs_for_provider(target = nil, &block)
  _chefspec_stubs_for_registry[:provider][target] << block
end
stubs_for_resource(target = nil, current_value: true, current_resource: true, &block) click to toggle source

Register stubs for resource objects.

The ‘target` parameter can select either a resource string like `’package’‘, a resource name like `’package’‘, or `nil` for all resources.

@example Setting method stub on a single resource

it "does a thing" do
  stubs_for_resource("my_resource[foo]") do |res|
    expect(res).to receive_shell_out.with("my_command").and_return(stdout: "asdf")
  end
  expect(subject.some_method).to eq "asdf"
end

@param target [String, nil] Resource name to inject, or nil for all resources. @param current_value [Boolean] If true, also register stubs for current_value objects on the same target. @param block [Proc] A block taking the resource object as a parameter. @return [void]

# File lib/chefspec/api/stubs_for.rb, line 62
def stubs_for_resource(target = nil, current_value: true, current_resource: true, &block)
  current_value = false unless current_resource
  _chefspec_stubs_for_registry[:resource][target] << block
  stubs_for_current_value(target, &block) if current_value
end