Module: ChefSpec::API::StubsFor
- Extended by:
- ClassMethods, RSpec::SharedContext
- Defined in:
- lib/chefspec/api/stubs_for.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- HAS_SHELLOUT_COMPACTED =
Which version to use the shell_out_compacted hook on.
Gem::Requirement.create("> 14.2")
Class Method Summary collapse
-
.setup_stubs_for(object, type) ⇒ void
private
Hook used in the monkey patches to set up a place to inject stubs when needed for a resource or provider.
Instance Method Summary collapse
- #receive_shell_out(*cmd, stdout: "", stderr: "", exitstatus: 0, **opts) ⇒ Object
-
#stubs_for_current_value(target = nil, &block) ⇒ void
(also: #stubs_for_current_resource)
Register stubs for current_value objects.
-
#stubs_for_provider(target = nil, &block) ⇒ void
Register stubs for provider objects.
-
#stubs_for_resource(target = nil, current_value: true, current_resource: true, &block) ⇒ void
Register stubs for resource objects.
Methods included from ClassMethods
Class Method Details
.setup_stubs_for(object, type) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Hook used in the monkey patches to set up a place to inject stubs when needed for a resource or provider.
20 21 22 |
# 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 |
Instance Method Details
#receive_shell_out(*cmd, stdout: "", stderr: "", exitstatus: 0, **opts) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# 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_value(target = nil, &block) ⇒ void Also known as: stubs_for_current_resource
This method returns an undefined value.
Register stubs for current_value objects.
74 75 76 |
# 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 |
#stubs_for_provider(target = nil, &block) ⇒ void
This method returns an undefined value.
Register stubs for provider objects.
85 86 87 |
# 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) ⇒ void
This method returns an undefined value.
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.
62 63 64 65 66 |
# 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 |