Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(8)

Unified Diff: Lib/test/test_imaplib.py

Issue 4972: context managerment support in imaplib, smtplib, ftplib
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/imaplib.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_imaplib.py
+++ b/Lib/test/test_imaplib.py
@@ -96,16 +96,20 @@ else:
class SimpleIMAPHandler(socketserver.StreamRequestHandler):
timeout = 1
continuation = None
capabilities = ''
+ def setup(self):
+ super().setup()
+ self.server.logged = None
+
def _send(self, message):
if verbose: print("SENT: %r" % message.strip())
self.wfile.write(message)
def _send_line(self, message):
self._send(message + b'\r\n')
def _send_textline(self, message):
@@ -156,19 +160,24 @@ class SimpleIMAPHandler(socketserver.Str
self._send_tagged(tag, 'BAD', cmd + ' unknown')
def cmd_CAPABILITY(self, tag, args):
caps = 'IMAP4rev1 ' + self.capabilities if self.capabilities else 'IMAP4rev1'
self._send_textline('* CAPABILITY ' + caps)
self._send_tagged(tag, 'OK', 'CAPABILITY completed')
def cmd_LOGOUT(self, tag, args):
+ self.server.logged = None
self._send_textline('* BYE IMAP4ref1 Server logging out')
self._send_tagged(tag, 'OK', 'LOGOUT completed')
+ def cmd_LOGIN(self, tag, args):
+ self.server.logged = args[0]
+ self._send_tagged(tag, 'OK', 'LOGIN completed')
+
class BaseThreadedNetworkedTests(unittest.TestCase):
def make_server(self, addr, hdlr):
class MyServer(self.server_class):
def handle_error(self, request, client_address):
self.close_request(request)
@@ -333,16 +342,42 @@ class BaseThreadedNetworkedTests(unittes
def handle(self):
# Send a very long response line
self.wfile.write(b'* OK ' + imaplib._MAXLINE*b'x' + b'\r\n')
with self.reaped_server(TooLongHandler) as server:
self.assertRaises(imaplib.IMAP4.error,
self.imap_class, *server.server_address)
+ @reap_threads
+ def test_simple_with_statement(self):
+ # simplest call
+ with self.reaped_server(SimpleIMAPHandler) as server:
+ with self.imap_class(*server.server_address):
+ pass
+
+ @reap_threads
+ def test_with_statement(self):
+ with self.reaped_server(SimpleIMAPHandler) as server:
+ with self.imap_class(*server.server_address) as imap:
+ imap.login('user', 'pass')
+ self.assertEqual(server.logged, 'user')
+ self.assertIsNone(server.logged)
+
+ @reap_threads
+ def test_with_statement_logout(self):
+ # what happens if already logout in the block?
+ with self.reaped_server(SimpleIMAPHandler) as server:
+ with self.imap_class(*server.server_address) as imap:
+ imap.login('user', 'pass')
+ self.assertEqual(server.logged, 'user')
+ imap.logout()
+ self.assertIsNone(server.logged)
+ self.assertIsNone(server.logged)
+
class ThreadedNetworkedTests(BaseThreadedNetworkedTests):
server_class = socketserver.TCPServer
imap_class = imaplib.IMAP4
@unittest.skipUnless(ssl, "SSL not available")
« no previous file with comments | « Lib/imaplib.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+