Skip to content

Commit 5f12016

Browse files
authored
🔀 Merge pull request #293 from ruby/config-responses_without_block
🔊 Add config option for `responses_without_block`
2 parents 7e3148c + ca8e26d commit 5f12016

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ 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, type: [
129+
:silence_deprecation_warning, :warn, :raise,
130+
]
131+
117132
# Creates a new config object and initialize its attribute with +attrs+.
118133
#
119134
# If +parent+ is not given, the global config is used by default.
@@ -130,6 +145,7 @@ def initialize(parent = Config.global, **attrs)
130145
open_timeout: 30,
131146
idle_response_timeout: 5,
132147
sasl_ir: true,
148+
responses_without_block: :silence_deprecation_warning,
133149
).freeze
134150

135151
@global = default.new

lib/net/imap/config/attr_type_coercion.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def self.attr_accessor(attr, type: nil)
2626
return unless type
2727
if :boolean == type then boolean attr
2828
elsif Integer == type then integer attr
29+
elsif Array === type then enum attr, type
2930
else raise ArgumentError, "unknown type coercion %p" % [type]
3031
end
3132
end
@@ -39,6 +40,17 @@ def self.integer(attr)
3940
define_method :"#{attr}=" do |val| super Integer val end
4041
end
4142

43+
def self.enum(attr, enum)
44+
enum = enum.dup.freeze
45+
expected = -"one of #{enum.map(&:inspect).join(", ")}"
46+
define_method :"#{attr}=" do |val|
47+
unless enum.include?(val)
48+
raise ArgumentError, "expected %s, got %p" % [expected, val]
49+
end
50+
super val
51+
end
52+
end
53+
4254
end
4355
end
4456
end

test/net/imap/test_config.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ class ConfigTest < Test::Unit::TestCase
5050
assert_equal 333, config.open_timeout
5151
end
5252

53+
test "enum type constraint" do
54+
config = Config.new
55+
config.responses_without_block = :silence_deprecation_warning
56+
assert_equal :silence_deprecation_warning, config.responses_without_block
57+
config.responses_without_block = :warn
58+
assert_equal :warn, config.responses_without_block
59+
config.responses_without_block = :raise
60+
assert_equal :raise, config.responses_without_block
61+
assert_raise(ArgumentError) do config.responses_without_block = false end
62+
assert_equal :raise, config.responses_without_block
63+
assert_raise(ArgumentError) do config.responses_without_block = 12345 end
64+
assert_equal :raise, config.responses_without_block
65+
assert_raise(ArgumentError) do config.responses_without_block = "warn" end
66+
assert_equal :raise, config.responses_without_block
67+
end
68+
5369
test ".default" do
5470
default = Config.default
5571
assert default.equal?(Config.default)

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)