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
0.1x deprecate fixes #1059
Merged
Merged
0.1x deprecate fixes #1059
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2e8694
Teach proxy_class how to work with root-level superclasses
technoweenie ddab77c
allow deprecated exception classes to rescue the current exception class
technoweenie 6198c75
squelch this
technoweenie 5b0ebf7
Update lib/faraday/deprecate.rb
technoweenie 620c4d1
remove unnecessary #tap
technoweenie c4ebca8
use rspec expect syntax to test the error handling
technoweenie File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# 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 | ||
begin | ||
raise SampleClass, nil | ||
rescue SampleDeprecatedClass | ||
rescue StandardError => exc | ||
fail "rescued #{exc.class.name} instead" | ||
end | ||
end | ||
|
||
it 'allows rescuing of a current error with a current error' do | ||
begin | ||
raise SampleClass, nil | ||
rescue SampleClass | ||
rescue StandardError => exc | ||
fail "rescued #{exc.class.name} instead" | ||
end | ||
end | ||
|
||
it 'allows rescuing of a deprecated error with a deprecated error' do | ||
begin | ||
raise SampleDeprecatedClass, nil | ||
rescue SampleDeprecatedClass | ||
rescue StandardError => exc | ||
fail "rescued #{exc.class.name} instead" | ||
end | ||
end | ||
|
||
it 'allows rescuing of a deprecated error with a current error' do | ||
begin | ||
raise SampleDeprecatedClass, nil | ||
rescue SampleClass | ||
rescue StandardError => exc | ||
fail "rescued #{exc.class.name} instead" | ||
end | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we find a way to output a message of success when we pass here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is a "not-fail" test case? Is that something you see in the results? What would a message of success here look like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed those examples to use
expect { raise ... }.to raise_error(...)
instead. Much shorter, and still tests the correct thing. If I remove the#===
implementation on the inherited meta class thatFaraday::DeprecatedClass.proxy_class
creates, I get these failures:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of "if we get here,
assert_equal(true, true, "it works")
" kind of thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh I see. I think the
expect
syntax works too. I don't think anexpect(true).to be_true
underneath is necessary.