-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
πποΈ Deprecate old Net::IMAP.new parameters
Converts `Net::IMAP#initialize` to use keyword parameters for all options. Creates `Net::IMAP::DeprecatedClientOptions`, which is prepended to `Net::IMAP` to override `#initialize`. Moved all of the original argument handling into it. Obsolete, but not yet deprecated: * Using an `options` hash instead of keyword parameters. * Sending `port` as the second parameter. Deprecated (will raise a warning): * Setting `ssl` params via `usessl`, `certs`, or `verify` arguments.
- Loading branch information
Showing
4 changed files
with
246 additions
and
41 deletions.
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,112 @@ | ||
# frozen_string_literal: true | ||
|
||
module Net | ||
class IMAP < Protocol | ||
|
||
# This module handles deprecated arguments to various Net::IMAP methods. | ||
module DeprecatedClientOptions | ||
|
||
# :call-seq: | ||
# Net::IMAP.new(host, **options) # standard keyword options | ||
# Net::IMAP.new(host, options) # obsolete hash options | ||
# Net::IMAP.new(host, port) # obsolete port argument | ||
# Net::IMAP.new(host, port, usessl, certs = nil, verify = true) # deprecated SSL arguments | ||
# | ||
# Translates Net::IMAP.new arguments for backward compatibility. | ||
# | ||
# ==== Obsolete arguments | ||
# | ||
# Using obsolete arguments does not a warning. Obsolete arguments will be | ||
# deprecated by a future release. | ||
# | ||
# If a second positional argument is given and it is a hash (or is | ||
# convertable via +#to_hash+), it is converted to keyword arguments. | ||
# | ||
# # Obsolete: | ||
# Net::IMAP.new("imap.example.com", options_hash) | ||
# # Use instead: | ||
# Net::IMAP.new("imap.example.com", **options_hash) | ||
# | ||
# If a second positional argument is given and it is not a hash, it is | ||
# converted to the +port+ keyword argument. | ||
# # Obsolete: | ||
# Net::IMAP.new("imap.example.com", 114433) | ||
# # Use instead: | ||
# Net::IMAP.new("imap.example.com", port: 114433) | ||
# | ||
# ==== Deprecated arguments | ||
# | ||
# Using deprecated arguments prints a warning. Convert to keyword | ||
# arguments to avoid the warning. Deprecated arguments will be removed in | ||
# a future release. | ||
# | ||
# If +usessl+ is false, +certs+, and +verify+ are ignored. When it true, | ||
# all three arguments are converted to the +ssl+ keyword argument. | ||
# Without +certs+ or +verify+, it is converted to <tt>ssl: true</tt>. | ||
# # DEPRECATED: | ||
# Net::IMAP.new("imap.example.com", nil, true) # => prints a warning | ||
# # Use instead: | ||
# Net::IMAP.new("imap.example.com", ssl: true) | ||
# | ||
# When +certs+ is a path to a directory, it is converted to <tt>ca_path: | ||
# certs</tt>. | ||
# # DEPRECATED: | ||
# Net::IMAP.new("imap.example.com", nil, true, "/path/to/certs") # => prints a warning | ||
# # Use instead: | ||
# Net::IMAP.new("imap.example.com", ssl: {ca_path: "/path/to/certs"}) | ||
# | ||
# When +certs+ is a path to a file, it is converted to <tt>ca_file: | ||
# certs</tt>. | ||
# # DEPRECATED: | ||
# Net::IMAP.new("imap.example.com", nil, true, "/path/to/cert.pem") # => prints a warning | ||
# # Use instead: | ||
# Net::IMAP.new("imap.example.com", ssl: {ca_file: "/path/to/cert.pem"}) | ||
# | ||
# When +verify+ is +false+, it is converted to <tt>verify_mode: | ||
# OpenSSL::SSL::VERIFY_NONE</tt>. | ||
# # DEPRECATED: | ||
# Net::IMAP.new("imap.example.com", nil, true, nil, false) # => prints a warning | ||
# # Use instead: | ||
# Net::IMAP.new("imap.example.com", ssl: {verify_mode: OpenSSL::SSL::VERIFY_NONE}) | ||
# | ||
def initialize(host, port_or_options = nil, *deprecated, **options) | ||
if port_or_options.nil? && deprecated.empty? | ||
super host, **options | ||
elsif options.any? | ||
# Net::IMAP.new(host, *__invalid__, **options) | ||
raise ArgumentError, "Do not combine deprecated and keyword arguments" | ||
elsif port_or_options.respond_to?(:to_hash) and deprecated.any? | ||
# Net::IMAP.new(host, options, *__invalid__) | ||
raise ArgumentError, "Do not use deprecated SSL params with options hash" | ||
elsif port_or_options.respond_to?(:to_hash) | ||
super host, **Hash.try_convert(port_or_options) | ||
elsif deprecated.empty? | ||
super host, port: port_or_options | ||
elsif deprecated.shift | ||
warn "DEPRECATED: Call Net::IMAP.new with keyword options", uplevel: 1 | ||
super host, port: port_or_options, ssl: create_ssl_params(*deprecated) | ||
else | ||
warn "DEPRECATED: Call Net::IMAP.new with keyword options", uplevel: 1 | ||
super host, port: port_or_options, ssl: false | ||
end | ||
end | ||
|
||
private | ||
|
||
def create_ssl_params(certs = nil, verify = true) | ||
params = {} | ||
if certs | ||
if File.file?(certs) | ||
params[:ca_file] = certs | ||
elsif File.directory?(certs) | ||
params[:ca_path] = certs | ||
end | ||
end | ||
params[:verify_mode] = | ||
verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE | ||
params | ||
end | ||
|
||
end | ||
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
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,118 @@ | ||
# frozen_string_literal: true | ||
|
||
require "net/imap" | ||
require "test/unit" | ||
require_relative "fake_server" | ||
|
||
class DeprecatedClientOptionsTest < Test::Unit::TestCase | ||
include Net::IMAP::FakeServer::TestHelper | ||
|
||
def setup | ||
@do_not_reverse_lookup = Socket.do_not_reverse_lookup | ||
Socket.do_not_reverse_lookup = true | ||
@threads = [] | ||
end | ||
|
||
def teardown | ||
assert_join_threads(@threads) unless @threads.empty? | ||
ensure | ||
Socket.do_not_reverse_lookup = @do_not_reverse_lookup | ||
end | ||
|
||
class InitializeTests < DeprecatedClientOptionsTest | ||
|
||
test "Convert obsolete options hash to keywords" do | ||
run_fake_server_in_thread do |server| | ||
with_client(server.host, {port: server.port, ssl: false}) do |client| | ||
assert_equal server.host, client.host | ||
assert_equal server.port, client.port | ||
assert_equal false, client.ssl_ctx_params | ||
end | ||
end | ||
end | ||
|
||
test "Convert obsolete port argument to :port keyword" do | ||
run_fake_server_in_thread do |server| | ||
with_client(server.host, server.port) do |client| | ||
assert_equal server.host, client.host | ||
assert_equal server.port, client.port | ||
assert_equal false, client.ssl_ctx_params | ||
end | ||
end | ||
end | ||
|
||
test "Convert deprecated usessl (= false) with warning" do | ||
run_fake_server_in_thread do |server| | ||
assert_deprecated_warning(/Call Net::IMAP\.new with keyword/i) do | ||
with_client(server.host, server.port, false, :who, :cares) do |client| | ||
assert_equal server.host, client.host | ||
assert_equal server.port, client.port | ||
assert_equal false, client.ssl_ctx_params | ||
end | ||
end | ||
end | ||
end | ||
|
||
test "Convert deprecated usessl (= true) and certs, with warning" do | ||
run_fake_server_in_thread(implicit_tls: true) do |server| | ||
certs = server.config.tls[:ca_file] | ||
assert_deprecated_warning(/Call Net::IMAP\.new with keyword/i) do | ||
with_client("localhost", server.port, true, certs) do |client| | ||
assert_equal "localhost", client.host | ||
assert_equal server.port, client.port | ||
assert_equal( | ||
{ca_file: certs, verify_mode: OpenSSL::SSL::VERIFY_PEER}, | ||
client.ssl_ctx_params | ||
) | ||
end | ||
end | ||
end | ||
end | ||
|
||
test "Convert deprecated usessl (= true) and verify (= false), with warning" do | ||
run_fake_server_in_thread(implicit_tls: true) do |server| | ||
assert_deprecated_warning(/Call Net::IMAP\.new with keyword/i) do | ||
with_client("localhost", server.port, true, nil, false) do |client| | ||
assert_equal "localhost", client.host | ||
assert_equal server.port, client.port | ||
assert_equal( | ||
{verify_mode: OpenSSL::SSL::VERIFY_NONE}, | ||
client.ssl_ctx_params | ||
) | ||
assert_equal OpenSSL::SSL::VERIFY_NONE, client.ssl_ctx.verify_mode | ||
end | ||
end | ||
end | ||
end | ||
|
||
test "combined options hash and keyword args raises ArgumentError" do | ||
ex = nil | ||
run_fake_server_in_thread( | ||
ignore_io_error: true, implicit_tls: true | ||
) do |server| | ||
imap = Net::IMAP.new("localhost", {port: 993}, ssl: true) | ||
rescue => ex | ||
nil | ||
ensure | ||
imap&.disconnect | ||
end | ||
assert_equal ArgumentError, ex.class | ||
end | ||
|
||
test "combined options hash and ssl args raises ArgumentError" do | ||
ex = nil | ||
run_fake_server_in_thread( | ||
ignore_io_error: true, implicit_tls: true | ||
) do |server| | ||
imap = Net::IMAP.new("localhost", {port: 993}, true) | ||
rescue => ex | ||
nil | ||
ensure | ||
imap&.disconnect | ||
end | ||
assert_equal ArgumentError, ex.class | ||
end | ||
|
||
end | ||
|
||
end |