Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions lib/chefspec/api/stubs_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ module StubsFor
# Pull in the needed machinery to use `before` here.
extend RSpec::SharedContext

# Which version to use the shell_out_compacted hook on.
HAS_SHELLOUT_COMPACTED = Gem::Requirement.create("> 14.2")

# Hook used in the monkey patches to set up a place to inject stubs when
# needed for a resource or provider.
#
Expand Down Expand Up @@ -97,12 +94,8 @@ def receive_shell_out(*cmd, stdout: "", stderr: "", exitstatus: 0, **opts)
@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
# Intercept using the shell_out_compacted hook point.
shell_out_method ||= :shell_out_compacted
with_args = cmd + (opts.empty? ? [any_args] : [hash_including(opts)])
receive(shell_out_method).with(*with_args).and_return(fake_cmd)
end
Expand Down
29 changes: 6 additions & 23 deletions lib/chefspec/extensions/chef/lwrp_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,10 @@
# collection. Chefspec needs to be taught how to deal with
# sub-resource collections.

if defined?(Chef::Provider::InlineResources)
Chef::Provider::InlineResources.prepend(Module.new do
def initialize(resource, run_context)
super
@run_context = run_context
@resource_collection = run_context.resource_collection
end
end)
Chef::Provider.prepend(Module.new do
def compile_and_converge_action(&block)
return super unless $CHEFSPEC_MODE

Chef::Provider::InlineResources::ClassMethods.prepend(Module.new do
def action(name, &block)
# Note: This does not check $CHEFSPEC_MODE.
define_method("action_#{name}", &block)
end
end)
else # >= 13.0
Chef::Provider.prepend(Module.new do
def compile_and_converge_action(&block)
return super unless $CHEFSPEC_MODE

instance_eval(&block)
end
end)
end
instance_eval(&block)
end
end)
22 changes: 7 additions & 15 deletions lib/chefspec/extensions/chef/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,15 @@ def initialize(*args, &block)
end

# Defang shell_out and friends so it can never run.
if ChefSpec::API::StubsFor::HAS_SHELLOUT_COMPACTED.satisfied_by?(Gem::Version.create(Chef::VERSION))
def shell_out_compacted(*args)
return super unless $CHEFSPEC_MODE
def shell_out_compacted(*args)
return super unless $CHEFSPEC_MODE

raise ChefSpec::Error::ShellOutNotStubbed.new(args: args, type: "provider", resource: new_resource)
end

def shell_out_compacted!(*args)
return super unless $CHEFSPEC_MODE
raise ChefSpec::Error::ShellOutNotStubbed.new(args: args, type: "provider", resource: new_resource)
end

shell_out_compacted(*args).tap(&:error!)
end
else
def shell_out(*args)
return super unless $CHEFSPEC_MODE
def shell_out_compacted!(*args)
return super unless $CHEFSPEC_MODE

raise ChefSpec::Error::ShellOutNotStubbed.new(args: args, type: "provider", resource: new_resource)
end
shell_out_compacted(*args).tap(&:error!)
end
end)
44 changes: 14 additions & 30 deletions lib/chefspec/extensions/chef/shell_out.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,33 @@ module ::ChefSpec::Extensions::Chef::ResourceShellOut
#
# Defang shell_out and friends so it can never run.
#
if ChefSpec::API::StubsFor::HAS_SHELLOUT_COMPACTED.satisfied_by?(Gem::Version.create(Chef::VERSION))
def shell_out_compacted(*args)
return super unless $CHEFSPEC_MODE
def shell_out_compacted(*args)
return super unless $CHEFSPEC_MODE

raise ChefSpec::Error::ShellOutNotStubbed.new(args: args, type: "resource", resource: self)
end

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).tap(&:error!)
end
else
def shell_out(*args)
return super unless $CHEFSPEC_MODE
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).tap(&:error!)
end
end

module ::ChefSpec::Extensions::Chef::MixinShellOut
#
# Defang shell_out and friends so it can never run.
#
if ChefSpec::API::StubsFor::HAS_SHELLOUT_COMPACTED.satisfied_by?(Gem::Version.create(Chef::VERSION))
def shell_out_compacted(*args)
return super unless $CHEFSPEC_MODE
def shell_out_compacted(*args)
return super unless $CHEFSPEC_MODE

raise ChefSpec::Error::LibraryShellOutNotStubbed.new(args: args, object: self)
end

def shell_out_compacted!(*args)
return super unless $CHEFSPEC_MODE
raise ChefSpec::Error::LibraryShellOutNotStubbed.new(args: args, object: self)
end

shell_out_compacted(*args).tap(&:error!)
end
else
def shell_out(*args)
return super unless $CHEFSPEC_MODE
def shell_out_compacted!(*args)
return super unless $CHEFSPEC_MODE

raise ChefSpec::Error::LibraryShellOutNotStubbed.new(args: args, object: self)
end
shell_out_compacted(*args).tap(&:error!)
end
end

Expand Down
Loading