diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index cca06e6..cd8af89 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -621,6 +621,7 @@ def started? # def start(*args, helo: nil, user: nil, secret: nil, password: nil, authtype: nil) raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..4)" if args.size > 4 + raise IOError, 'SMTP session already started' if @started helo ||= args[0] || 'localhost' user ||= args[1] secret ||= password || args[2] @@ -664,7 +665,6 @@ def tcp_socket(address, port) end def do_start(helo_domain, user, secret, authtype) - raise IOError, 'SMTP session already started' if @started if user || secret || authtype check_auth_args authtype, user, secret end diff --git a/test/net/smtp/test_smtp.rb b/test/net/smtp/test_smtp.rb index 3b9e245..5c0f905 100644 --- a/test/net/smtp/test_smtp.rb +++ b/test/net/smtp/test_smtp.rb @@ -530,6 +530,20 @@ def test_start_instance smtp.finish end + def test_nested_block_start_preserves_active_session + server = FakeServer.start + smtp = Net::SMTP.start("localhost", server.port) + + error = assert_raise(IOError) do + smtp.start { flunk("nested start must not yield") } + end + assert_equal "SMTP session already started", error.message + assert smtp.started? + assert smtp.rset.success? + ensure + smtp.finish if smtp&.started? + end + def test_start_instance_with_position_argument port = fake_server_start(auth: 'plain') smtp = Net::SMTP.new('localhost', port)