Skip to content

Commit 4a4559a

Browse files
committed
🔊 Warn about deprecated responses usage
This was extracted from #93 and split into a separate PR. A new config option is added: `responses_without_block`. This is provided as a workaround, until dependant projects can update their usage. A future release may remove this backwards compatibility, but _no sooner_ than v0.6.
1 parent 962671d commit 4a4559a

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

lib/net/imap.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,7 @@ def idle_done
24992499
#
25002500
# Calling without a block is unsafe and deprecated. Future releases will
25012501
# raise ArgumentError unless a block is given.
2502+
# See Config#responses_without_block.
25022503
#
25032504
# Previously unhandled responses are automatically cleared before entering a
25042505
# mailbox with #select or #examine. Long-lived connections can receive many
@@ -2523,7 +2524,12 @@ def responses(type = nil)
25232524
elsif type
25242525
raise ArgumentError, "Pass a block or use #clear_responses"
25252526
else
2526-
# warn("DEPRECATED: pass a block or use #clear_responses", uplevel: 1)
2527+
case config.responses_without_block
2528+
when :raise
2529+
raise ArgumentError, "Pass a block or use #clear_responses"
2530+
when :warn
2531+
warn("DEPRECATED: pass a block or use #clear_responses", uplevel: 1)
2532+
end
25272533
@responses
25282534
end
25292535
end

lib/net/imap/config.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ def self.[](config) # :nodoc: unfinished API
104104
# The default value is +5+ seconds.
105105
attr_accessor :idle_response_timeout, type: Integer
106106

107+
# :markup: markdown
108+
#
109+
# Controls the behavior of Net::IMAP#responses when called without a
110+
# block. Valid options are `:warn`, `:raise`, or
111+
# `:silence_deprecation_warning`.
112+
#
113+
# | Starting with version | The default value is |
114+
# |-----------------------|--------------------------------|
115+
# | v0.4.13 | +:silence_deprecation_warning+ |
116+
# | v0.5 | +:warn+ |
117+
# | _eventually_ | +:raise+ |
118+
attr_accessor :responses_without_block
119+
107120
# Creates a new config object and initialize its attribute with +attrs+.
108121
#
109122
# If +parent+ is not given, the global config is used by default.
@@ -119,6 +132,7 @@ def initialize(parent = Config.global, **attrs)
119132
debug: false,
120133
open_timeout: 30,
121134
idle_response_timeout: 5,
135+
responses_without_block: :silence_deprecation_warning,
122136
).freeze
123137

124138
@global = default.new

test/net/imap/test_imap.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,10 +1105,22 @@ def test_responses
11051105
assert_equal(1, imap.responses("RECENT", &:last))
11061106
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
11071107
# Deprecated style, without a block:
1108-
# assert_warn(/Pass a block.*or.*clear_responses/i) do
1109-
# assert_equal(%i[Answered Flagged Deleted Seen Draft],
1110-
# imap.responses["FLAGS"]&.last)
1111-
# end
1108+
imap.config.responses_without_block = :raise
1109+
assert_raise(ArgumentError) do imap.responses end
1110+
imap.config.responses_without_block = :warn
1111+
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
1112+
assert_warn(/Pass a block.*or.*clear_responses/i) do
1113+
assert_equal(%i[Answered Flagged Deleted Seen Draft],
1114+
imap.responses["FLAGS"]&.last)
1115+
end
1116+
# TODO: assert_no_warn?
1117+
imap.config.responses_without_block = :silence_deprecation_warning
1118+
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
1119+
stderr = EnvUtil.verbose_warning {
1120+
assert_equal(%i[Answered Flagged Deleted Seen Draft],
1121+
imap.responses["FLAGS"]&.last)
1122+
}
1123+
assert_empty stderr
11121124
end
11131125
end
11141126

0 commit comments

Comments
 (0)