module ChefSpec::API::StubsFor
def self.setup_stubs_for(object, type)
-
(void)-
Parameters:
-
type(Symbol) -- Type of object to register stubs on -
object(Chef::Resource, Chef::Provider) -- Resource or provider to inject
Other tags:
- Api: - private
def self.setup_stubs_for(object, type) # This space left intentionally blank, real implementation is below. end
def receive_shell_out(*cmd, stdout: '', stderr: '', exitstatus: 0, **opts)
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, **opts) 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 receive(shell_out_method).with(*cmd).and_return(fake_cmd) end
def stubs_for_current_resource(target=nil, &block)
-
(void)-
Parameters:
-
block(Proc) -- A block taking the resource object as a parameter. -
target(String, nil) -- Resource name to inject, or nil for all resources.
Other tags:
- See: #stubs_for_resource -
def stubs_for_current_resource(target=nil, &block) _chefspec_stubs_for_registry[:current_resource][target] << block end
def stubs_for_provider(target=nil, &block)
-
(void)-
Parameters:
-
block(Proc) -- A block taking the resource object as a parameter. -
target(String, nil) -- Resource name to inject, or nil for all providers.
Other tags:
- See: #stubs_for_resource -
def stubs_for_provider(target=nil, &block) _chefspec_stubs_for_registry[:provider][target] << block end
def stubs_for_resource(target=nil, current_resource: true, &block)
-
(void)-
Parameters:
-
block(Proc) -- A block taking the resource object as a parameter. -
current_resource(Boolean) -- If true, also register stubs for current_resource objects on the same target. -
target(String, nil) -- Resource name to inject, or nil for all resources.
Other tags:
- Example: Setting method stub on a single resource -
def stubs_for_resource(target=nil, current_resource: true, &block) _chefspec_stubs_for_registry[:resource][target] << block stubs_for_current_resource(target, &block) if current_resource end