From 0627b814c1f99713dd7b53be86113221fac0dd9b Mon Sep 17 00:00:00 2001 From: Jason Madden Date: Thu, 26 Oct 2017 09:58:32 -0500 Subject: [PATCH] Coverage and deprecated functions for test_ftpserver.py. Coverage for asynerror.py --- src/zope/server/ftp/tests/test_ftpserver.py | 29 +++++++-------------- src/zope/server/tests/asyncerror.py | 2 +- src/zope/server/tests/test_asyncerror.py | 18 +++++++++++++ 3 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 src/zope/server/tests/test_asyncerror.py diff --git a/src/zope/server/ftp/tests/test_ftpserver.py b/src/zope/server/ftp/tests/test_ftpserver.py index caf4b3a..aa7b13e 100644 --- a/src/zope/server/ftp/tests/test_ftpserver.py +++ b/src/zope/server/ftp/tests/test_ftpserver.py @@ -44,9 +44,10 @@ def retrlines(ftpconn, cmd): return ''.join(res) -class Tests(unittest.TestCase, AsyncoreErrorHook): +class Tests(AsyncoreErrorHook, unittest.TestCase): def setUp(self): + super(Tests, self).setUp() # Avoid the tests hanging for a long time if something goes wrong socket.setdefaulttimeout(10) @@ -59,7 +60,6 @@ def setUp(self): # TODO tests should be more careful to clear the socket map. asyncore.poll(0.1) self.orig_map_size = len(asyncore.socket_map) - self.hook_asyncore_error() root_dir = demofs.Directory() root_dir['test'] = demofs.Directory() @@ -90,7 +90,7 @@ def setUp(self): self.thread.setDaemon(True) self.thread.start() self.thread_started.wait(10.0) - self.assert_(self.thread_started.isSet()) + self.assertTrue(self.thread_started.isSet()) def tearDown(self): self.run_loop = 0 @@ -108,7 +108,7 @@ def tearDown(self): break asyncore.poll(0.1) - self.unhook_asyncore_error() + super(Tests, self).tearDown() def loop(self): self.thread_started.set() @@ -123,8 +123,8 @@ def loop(self): continue except select.error as data: print("EXCEPTION POLLING IN LOOP(): %s" % data) - if data[0] == EBADF: - for key in asyncore.socket_map.keys(): + if data.args[0] == EBADF: + for key in asyncore.socket_map: print("") try: select.select([], [], [key], 0.0) @@ -172,7 +172,7 @@ def execute(self, commands, login=1): for command in commands: ftp.send(command.encode('ascii') + b'\r\n') result = ftp.recv(10000).decode('ascii') - self.failUnless(result.endswith('\r\n')) + self.assertTrue(result.endswith('\r\n')) finally: ftp.close() return result @@ -281,9 +281,9 @@ def testLIST(self): conn.login('anonymous', 'bar') self.assertRaises(ftplib.Error, retrlines, conn, 'LIST /foo') listing = retrlines(conn, 'LIST') - self.assert_(len(listing) > 0) + self.assertGreater(len(listing), 0) listing = retrlines(conn, 'LIST -la') - self.assert_(len(listing) > 0) + self.assertGreater(len(listing), 0) finally: conn.close() # Make sure no garbage was left behind. @@ -297,7 +297,7 @@ def testMKDLIST(self): conn.login('foo', 'bar') listing = [] conn.retrlines('LIST /test', listing.append) - self.assert_(len(listing) > 2) + self.assertGreaterEqual(len(listing), 2) listing = [] conn.retrlines('LIST -lad test/f1', listing.append) self.assertEqual(len(listing), 1) @@ -382,12 +382,3 @@ def testUSER(self): status_messages['PASS_REQUIRED']) self.assertEqual(self.execute('USER', 0).rstrip(), status_messages['ERR_ARGS']) - - - -def test_suite(): - loader = unittest.TestLoader() - return loader.loadTestsFromTestCase(Tests) - -if __name__=='__main__': - unittest.TextTestRunner().run(test_suite()) diff --git a/src/zope/server/tests/asyncerror.py b/src/zope/server/tests/asyncerror.py index 77cb1a5..579cdce 100644 --- a/src/zope/server/tests/asyncerror.py +++ b/src/zope/server/tests/asyncerror.py @@ -35,7 +35,7 @@ def setUp(self): self.hook_asyncore_error() def tearDown(self): - self.unhook_asycnore_error() + self.unhook_asyncore_error() def hook_asyncore_error(self): self._asyncore_traceback = asyncore.compact_traceback diff --git a/src/zope/server/tests/test_asyncerror.py b/src/zope/server/tests/test_asyncerror.py new file mode 100644 index 0000000..87af933 --- /dev/null +++ b/src/zope/server/tests/test_asyncerror.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +""" +Tests for asyncerror.py + +""" + +import unittest +from zope.server.tests.asyncerror import AsyncoreErrorHook + +class TestAsyncoreErrorHook(AsyncoreErrorHook, + unittest.TestCase): + + def test_handle_asynore_error(self): + try: + raise Exception() + except Exception: + with self.assertRaises(AssertionError): + self.handle_asyncore_error()