Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for Chef::Mixin::shellout #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions lib/chefspec/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SearchNotStubbed < NotStubbed; end
class DataBagNotStubbed < NotStubbed; end
class DataBagItemNotStubbed < NotStubbed; end
class ShellOutNotStubbed < ChefSpecError; end
class MixinShellOutNotStubbed < ChefSpecError; end

class CookbookPathNotFound < ChefSpecError; end
class GemLoadError < ChefSpecError; end
Expand Down
1 change: 1 addition & 0 deletions lib/chefspec/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Chef

# STOP! DO NOT ALPHABETIZE!
require_relative "extensions/chef/data_query" # must be before Chef::Resource loads
require_relative "extensions/chef/shell_out" # must come before client extensions or anything that winds up loading resources
require_relative "extensions/chef/resource" # must come before client extensions or anything that winds up loading resources
require_relative "extensions/chef/provider"
require_relative "extensions/chef/securable"
Expand Down
23 changes: 0 additions & 23 deletions lib/chefspec/extensions/chef/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,6 @@ def run_action(action, notification_type = nil, notifying_resource = nil)
end
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

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

def shell_out_compacted!(*args)
return super unless $CHEFSPEC_MODE

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

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

#
# tracking
#
Expand Down
58 changes: 58 additions & 0 deletions lib/chefspec/extensions/chef/shell_out.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require "chef/mixin/shell_out"
require "chef/resource"
require "chef/version"
require_relative "../../api/stubs_for"
require_relative "../../errors"

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

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

def shell_out_compacted!(*args)
return super unless $CHEFSPEC_MODE

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

raise ChefSpec::Error::ShellOutNotStubbed.new(args: args, type: "resource", resource: self)
end
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

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

def shell_out_compacted!(*args)
return super unless $CHEFSPEC_MODE

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

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

::Chef::Mixin::ShellOut.prepend(::ChefSpec::Extensions::Chef::MixinShellOut)
::Chef::Resource.prepend(::ChefSpec::Extensions::Chef::ResourceShellOut)
7 changes: 7 additions & 0 deletions templates/errors/mixin_shell_out_not_stubbed.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Executing a real shell_out in <%= @object.class %> is not allowed:

shell_out(<%= @args.inspect[1..-2] %>)

You can stub this with:

allow(<%= @object.class %>).to receive(:shell_out).with(<%= @args.inspect[1..-2] %>)