Skip to content

Commit

Permalink
🧪 Add Net::IMAP::FakeServer::TestHelper
Browse files Browse the repository at this point in the history
This simplifies using Net::IMAP::FakeServer in multiple test suites.
  • Loading branch information
nevans committed Jul 22, 2023
1 parent 24c5f0f commit 25d75d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/net/imap/fake_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Net::IMAP::FakeServer
autoload :ResponseWriter, "#{dir}/response_writer"
autoload :Socket, "#{dir}/socket"
autoload :Session, "#{dir}/session"
autoload :TestHelper, "#{dir}/test_helper"

# Returns the server's FakeServer::Configuration
attr_reader :config
Expand Down
29 changes: 29 additions & 0 deletions test/net/imap/fake_server/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require_relative "../fake_server"

module Net::IMAP::FakeServer::TestHelper

def with_fake_server(select: nil, timeout: 5, **opts)
Timeout.timeout(timeout) do
server = Net::IMAP::FakeServer.new(timeout: timeout, **opts)
@threads << Thread.new do server.run end if @threads
tls = opts[:implicit_tls]
tls = {ca_file: server.config.tls[:ca_file]} if tls == true
client = Net::IMAP.new("localhost", port: server.port, ssl: tls)
begin
if select
client.select(select)
server.commands.pop
end
yield server, client
ensure
client.logout rescue pp $!
client.disconnect if !client.disconnected?
end
ensure
server&.shutdown
end
end

end

0 comments on commit 25d75d4

Please sign in to comment.