From 5da96ec8382d0ef67fda8847aacec10443d9c914 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Sat, 27 Jun 2026 22:43:18 -0700 Subject: [PATCH] Remove dead pre-Chef-15 branches from the monkey patches The gemspec requires chef >= 17, which makes several version-conditional branches in the extension monkey patches unreachable: - lwrp_base.rb guarded on `defined?(Chef::Provider::InlineResources)`, which Chef removed in 13.0; only the >= 13 branch can run now. - provider.rb and shell_out.rb (both modules) branched on HAS_SHELLOUT_COMPACTED ("> 14.2"); at chef >= 17 the legacy `shell_out` fallbacks are dead, so only the `shell_out_compacted` hooks remain. - stubs_for.rb chose the hook method via the same always-true check. Drop the dead branches and the now-unused HAS_SHELLOUT_COMPACTED constant. Behavior is unchanged on supported Chef versions. Signed-off-by: Tim Smith --- lib/chefspec/api/stubs_for.rb | 11 ++---- lib/chefspec/extensions/chef/lwrp_base.rb | 29 ++++----------- lib/chefspec/extensions/chef/provider.rb | 22 ++++-------- lib/chefspec/extensions/chef/shell_out.rb | 44 ++++++++--------------- 4 files changed, 29 insertions(+), 77 deletions(-) diff --git a/lib/chefspec/api/stubs_for.rb b/lib/chefspec/api/stubs_for.rb index b3b63624..a48192e3 100644 --- a/lib/chefspec/api/stubs_for.rb +++ b/lib/chefspec/api/stubs_for.rb @@ -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. # @@ -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 diff --git a/lib/chefspec/extensions/chef/lwrp_base.rb b/lib/chefspec/extensions/chef/lwrp_base.rb index 74fd8f5d..115f5f2a 100644 --- a/lib/chefspec/extensions/chef/lwrp_base.rb +++ b/lib/chefspec/extensions/chef/lwrp_base.rb @@ -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) diff --git a/lib/chefspec/extensions/chef/provider.rb b/lib/chefspec/extensions/chef/provider.rb index 0b03ad40..3c4648ad 100644 --- a/lib/chefspec/extensions/chef/provider.rb +++ b/lib/chefspec/extensions/chef/provider.rb @@ -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) diff --git a/lib/chefspec/extensions/chef/shell_out.rb b/lib/chefspec/extensions/chef/shell_out.rb index dcc5bbad..64b9a7e8 100644 --- a/lib/chefspec/extensions/chef/shell_out.rb +++ b/lib/chefspec/extensions/chef/shell_out.rb @@ -8,24 +8,16 @@ 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 @@ -33,24 +25,16 @@ 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