Skip to content

Commit 2c8b6da

Browse files
committed
✨ Responses with no args can return frozen dup hash
This adds a new `:frozen_dup` option to `config.responses_without_block` which allows it to return a frozen copy of the responses hash, with each response type array also being a frozen copy.
1 parent ad951c9 commit 2c8b6da

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

lib/net/imap.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2498,11 +2498,13 @@ def idle_done
24982498

24992499
RESPONSES_DEPRECATION_MSG =
25002500
"Pass a type or block to #responses, " \
2501-
"set config.responses_without_block to :silence_deprecation_warning, " \
2501+
"set config.responses_without_block to :frozen_dup " \
2502+
"or :silence_deprecation_warning, " \
25022503
"or use #extract_responses or #clear_responses."
25032504
private_constant :RESPONSES_DEPRECATION_MSG
25042505

25052506
# :call-seq:
2507+
# responses -> hash of {String => Array} (see config.responses_without_block)
25062508
# responses(type) -> frozen array
25072509
# responses {|hash| ...} -> block result
25082510
# responses(type) {|array| ...} -> block result
@@ -2532,6 +2534,10 @@ def idle_done
25322534
# Prints a warning and returns the mutable responses hash.
25332535
# <em>This is not thread-safe.</em>
25342536
#
2537+
# [+:frozen_dup+</em>]
2538+
# Returns a frozen copy of the unhandled responses hash, with frozen
2539+
# array values.
2540+
#
25352541
# [+:raise+ <em>(planned future default)</em>]
25362542
# Raise an +ArgumentError+ with the deprecation warning.
25372543
#
@@ -2603,6 +2609,13 @@ def responses(type = nil)
26032609
raise ArgumentError, RESPONSES_DEPRECATION_MSG
26042610
when :warn
26052611
warn(RESPONSES_DEPRECATION_MSG, uplevel: 1)
2612+
when :frozen_dup
2613+
synchronize {
2614+
responses = @responses.transform_values(&:freeze)
2615+
responses.default_proc = nil
2616+
responses.default = [].freeze
2617+
return responses.freeze
2618+
}
26062619
end
26072620
@responses
26082621
end

lib/net/imap/config.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ def self.[](config)
237237
# Prints a warning and returns the mutable responses hash.
238238
# <em>This is not thread-safe.</em>
239239
#
240+
# [+:frozen_dup+</em>]
241+
# Returns a frozen copy of the unhandled responses hash, with frozen
242+
# array values.
243+
#
244+
# Note that calling IMAP#responses with a +type+ and without a block is
245+
# not configurable and always behaves like +:frozen_dup+.
246+
#
247+
# <em>(+:frozen_dup+ config option was added in +v0.4.17+)</em>
248+
#
240249
# [+:raise+ <em>(planned future default)</em>]
241250
# Raise an ArgumentError with the deprecation warning.
242251
#

test/net/imap/test_imap_responses.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ def assert_responses_warn
166166
assert_equal [], imap.responses["FAKE"]
167167
end
168168
assert_empty stderr
169+
# opt-in to future behavior
170+
imap.config.responses_without_block = :frozen_dup
171+
stderr = EnvUtil.verbose_warning do
172+
assert imap.responses.frozen?
173+
assert imap.responses["CAPABILITY"].frozen?
174+
assert_equal(%w[IMAP4REV1 NAMESPACE MOVE IDLE UTF8=ACCEPT],
175+
imap.responses["CAPABILITY"].last)
176+
end
177+
assert_empty stderr
169178
end
170179
end
171180

0 commit comments

Comments
 (0)