Skip to content

Commit 3c0be7d

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 7e3148c commit 3c0be7d

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
@@ -2501,6 +2501,7 @@ def idle_done
25012501
#
25022502
# Calling without a block is unsafe and deprecated. Future releases will
25032503
# raise ArgumentError unless a block is given.
2504+
# See Config#responses_without_block.
25042505
#
25052506
# Previously unhandled responses are automatically cleared before entering a
25062507
# mailbox with #select or #examine. Long-lived connections can receive many
@@ -2525,7 +2526,12 @@ def responses(type = nil)
25252526
elsif type
25262527
raise ArgumentError, "Pass a block or use #clear_responses"
25272528
else
2528-
# warn("DEPRECATED: pass a block or use #clear_responses", uplevel: 1)
2529+
case config.responses_without_block
2530+
when :raise
2531+
raise ArgumentError, "Pass a block or use #clear_responses"
2532+
when :warn
2533+
warn("DEPRECATED: pass a block or use #clear_responses", uplevel: 1)
2534+
end
25292535
@responses
25302536
end
25312537
end

lib/net/imap/config.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ def self.[](config) # :nodoc: unfinished API
114114
# | v0.4 | +true+ <em>(support added)</em> |
115115
attr_accessor :sasl_ir, type: :boolean
116116

117+
# :markup: markdown
118+
#
119+
# Controls the behavior of Net::IMAP#responses when called without a
120+
# block. Valid options are `:warn`, `:raise`, or
121+
# `:silence_deprecation_warning`.
122+
#
123+
# | Starting with version | The default value is |
124+
# |-----------------------|--------------------------------|
125+
# | v0.4.13 | +:silence_deprecation_warning+ |
126+
# | v0.5 | +:warn+ |
127+
# | _eventually_ | +:raise+ |
128+
attr_accessor :responses_without_block
129+
117130
# Creates a new config object and initialize its attribute with +attrs+.
118131
#
119132
# If +parent+ is not given, the global config is used by default.
@@ -130,6 +143,7 @@ def initialize(parent = Config.global, **attrs)
130143
open_timeout: 30,
131144
idle_response_timeout: 5,
132145
sasl_ir: true,
146+
responses_without_block: :silence_deprecation_warning,
133147
).freeze
134148

135149
@global = default.new

test/net/imap/test_imap.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,10 +1122,22 @@ def test_responses
11221122
assert_equal(1, imap.responses("RECENT", &:last))
11231123
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
11241124
# Deprecated style, without a block:
1125-
# assert_warn(/Pass a block.*or.*clear_responses/i) do
1126-
# assert_equal(%i[Answered Flagged Deleted Seen Draft],
1127-
# imap.responses["FLAGS"]&.last)
1128-
# end
1125+
imap.config.responses_without_block = :raise
1126+
assert_raise(ArgumentError) do imap.responses end
1127+
imap.config.responses_without_block = :warn
1128+
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
1129+
assert_warn(/Pass a block.*or.*clear_responses/i) do
1130+
assert_equal(%i[Answered Flagged Deleted Seen Draft],
1131+
imap.responses["FLAGS"]&.last)
1132+
end
1133+
# TODO: assert_no_warn?
1134+
imap.config.responses_without_block = :silence_deprecation_warning
1135+
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
1136+
stderr = EnvUtil.verbose_warning {
1137+
assert_equal(%i[Answered Flagged Deleted Seen Draft],
1138+
imap.responses["FLAGS"]&.last)
1139+
}
1140+
assert_empty stderr
11291141
end
11301142
end
11311143

0 commit comments

Comments
 (0)