diff --git a/lib/faraday/deprecate.rb b/lib/faraday/deprecate.rb index 53a199563..1e59b1894 100644 --- a/lib/faraday/deprecate.rb +++ b/lib/faraday/deprecate.rb @@ -8,13 +8,17 @@ module Faraday # @see Faraday::Deprecate module DeprecatedClass def self.proxy_class(new_klass) - Class.new(new_klass).tap do |k| - class << k + Class.new(new_klass) do + class << self extend Faraday::Deprecate # Make this more human readable than # - klass_name = superclass.to_s[/^#$/, 1] + klass_name = superclass.to_s[/^#$/, 1] deprecate :new, "#{klass_name}.new", '1.0' deprecate :inherited, klass_name, '1.0' + + def ===(other) + superclass === other || super + end end end end diff --git a/spec/faraday/deprecate_spec.rb b/spec/faraday/deprecate_spec.rb new file mode 100644 index 000000000..a4a25c748 --- /dev/null +++ b/spec/faraday/deprecate_spec.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +RSpec.describe Faraday::DeprecatedClass do + class SampleClass < StandardError + attr_accessor :foo + + def initialize(foo = nil) + @foo = foo || :foo + end + end + + SampleDeprecatedClass = Faraday::DeprecatedClass.proxy_class(SampleClass) + + it 'does not raise error for deprecated classes but prints an error message' do + error_message, foobar = with_warn_squelching { SampleDeprecatedClass.new(:foo_bar) } + expect(foobar).to be_a(SampleClass) + expect(foobar.foo).to eq(:foo_bar) + expect(error_message).to match( + Regexp.new( + 'NOTE: SampleDeprecatedClass.new is deprecated; '\ + 'use SampleClass.new instead. It will be removed in or after version 1.0' + ) + ) + end + + it 'does not raise an error for inherited error-namespaced classes but prints an error message' do + error_message, = with_warn_squelching { Class.new(SampleDeprecatedClass) } + + expect(error_message).to match( + Regexp.new( + 'NOTE: Inheriting SampleDeprecatedClass is deprecated; '\ + 'use SampleClass instead. It will be removed in or after version 1.0' + ) + ) + end + + it 'allows backward-compatible class to be subclassed' do + expect { + with_warn_squelching { Class.new(SampleDeprecatedClass) } + }.not_to raise_error + end + + it 'allows rescuing of a current error with a deprecated error' do + expect { raise SampleClass, nil }.to raise_error(SampleDeprecatedClass) + end + + it 'allows rescuing of a current error with a current error' do + expect { raise SampleClass, nil }.to raise_error(SampleClass) + end + + it 'allows rescuing of a deprecated error with a deprecated error' do + expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleDeprecatedClass) + end + + it 'allows rescuing of a deprecated error with a current error' do + expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleClass) + end + + + def with_warn_squelching + stderr_catcher = StringIO.new + original_stderr = $stderr + $stderr = stderr_catcher + result = yield if block_given? + [stderr_catcher.tap(&:rewind).string, result] + ensure + $stderr = original_stderr + end +end diff --git a/spec/faraday/error_spec.rb b/spec/faraday/error_spec.rb index 9b3292ad6..f99ae0071 100644 --- a/spec/faraday/error_spec.rb +++ b/spec/faraday/error_spec.rb @@ -56,7 +56,7 @@ end it 'does not raise an error for inherited error-namespaced classes but prints an error message' do - error_message, = with_warn_squelching { class E < Faraday::Error::ClientError; end } + error_message, = with_warn_squelching { Class.new(Faraday::Error::ClientError) } expect(error_message).to match( Regexp.new( @@ -67,7 +67,25 @@ end it 'allows backward-compatible class to be subclassed' do - expect { class CustomError < Faraday::Error::ClientError; end }.not_to raise_error + expect { + with_warn_squelching { Class.new(Faraday::Error::ClientError) } + }.not_to raise_error + end + + it 'allows rescuing of a current error with a deprecated error' do + expect { raise Faraday::ClientError, nil }.to raise_error(Faraday::Error::ClientError) + end + + it 'allows rescuing of a current error with a current error' do + expect { raise Faraday::ClientError, nil }.to raise_error(Faraday::ClientError) + end + + it 'allows rescuing of a deprecated error with a deprecated error' do + expect { raise Faraday::Error::ClientError, nil }.to raise_error(Faraday::Error::ClientError) + end + + it 'allows rescuing of a deprecated error with a current error' do + expect { raise Faraday::Error::ClientError, nil }.to raise_error(Faraday::ClientError) end end