D3375: cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp

durin42 (Augie Fackler) phabricator at mercurial-scm.org
Sat Apr 14 15:21:09 UTC 2018


durin42 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  The latter is deprecated on Python 3.7 and causes our tests to fail
  due to the warning.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3375

AFFECTED FILES
  tests/test-simplekeyvaluefile.py
  tests/test-wireproto-clientreactor.py
  tests/test-wireproto-framing.py
  tests/test-wsgirequest.py

CHANGE DETAILS

diff --git a/tests/test-wsgirequest.py b/tests/test-wsgirequest.py
--- a/tests/test-wsgirequest.py
+++ b/tests/test-wsgirequest.py
@@ -196,21 +196,26 @@
         self.assertEqual(r.dispatchparts, [b'pathinfo'])
         self.assertEqual(r.dispatchpath, b'pathinfo')
 
+    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
+        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
+        # the regex version.
+        assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
+
     def testreponame(self):
         """repository path components get stripped from URL."""
 
-        with self.assertRaisesRegexp(error.ProgrammingError,
+        with self.assertRaisesRegex(error.ProgrammingError,
                                      b'reponame requires PATH_INFO'):
             parse(DEFAULT_ENV, reponame=b'repo')
 
-        with self.assertRaisesRegexp(error.ProgrammingError,
+        with self.assertRaisesRegex(error.ProgrammingError,
                                      b'PATH_INFO does not begin with repo '
                                      b'name'):
             parse(DEFAULT_ENV, reponame=b'repo', extra={
                 r'PATH_INFO': r'/pathinfo',
             })
 
-        with self.assertRaisesRegexp(error.ProgrammingError,
+        with self.assertRaisesRegex(error.ProgrammingError,
                                      b'reponame prefix of PATH_INFO'):
             parse(DEFAULT_ENV, reponame=b'repo', extra={
                 r'PATH_INFO': r'/repoextra/path',
diff --git a/tests/test-wireproto-framing.py b/tests/test-wireproto-framing.py
--- a/tests/test-wireproto-framing.py
+++ b/tests/test-wireproto-framing.py
@@ -103,19 +103,24 @@
             ffs(b'1 1 0 command-data eos %s' % data.getvalue()),
         ])
 
+    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
+        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
+        # the regex version.
+        assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
+
     def testtextoutputformattingstringtype(self):
         """Formatting string must be bytes."""
-        with self.assertRaisesRegexp(ValueError, 'must use bytes formatting '):
+        with self.assertRaisesRegex(ValueError, 'must use bytes formatting '):
             list(framing.createtextoutputframe(None, 1, [
                 (b'foo'.decode('ascii'), [], [])]))
 
     def testtextoutputargumentbytes(self):
-        with self.assertRaisesRegexp(ValueError, 'must use bytes for argument'):
+        with self.assertRaisesRegex(ValueError, 'must use bytes for argument'):
             list(framing.createtextoutputframe(None, 1, [
                 (b'foo', [b'foo'.decode('ascii')], [])]))
 
     def testtextoutputlabelbytes(self):
-        with self.assertRaisesRegexp(ValueError, 'must use bytes for labels'):
+        with self.assertRaisesRegex(ValueError, 'must use bytes for labels'):
             list(framing.createtextoutputframe(None, 1, [
                 (b'foo', [], [b'foo'.decode('ascii')])]))
 
diff --git a/tests/test-wireproto-clientreactor.py b/tests/test-wireproto-clientreactor.py
--- a/tests/test-wireproto-clientreactor.py
+++ b/tests/test-wireproto-clientreactor.py
@@ -24,6 +24,12 @@
 
 class SingleSendTests(unittest.TestCase):
     """A reactor that can only send once rejects subsequent sends."""
+
+    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
+        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
+        # the regex version.
+        assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
+
     def testbasic(self):
         reactor = framing.clientreactor(hasmultiplesend=False, buffersends=True)
 
@@ -39,11 +45,11 @@
 
         self.assertEqual(request.state, b'sent')
 
-        with self.assertRaisesRegexp(error.ProgrammingError,
+        with self.assertRaisesRegex(error.ProgrammingError,
                                      'cannot issue new commands'):
             reactor.callcommand(b'foo', {})
 
-        with self.assertRaisesRegexp(error.ProgrammingError,
+        with self.assertRaisesRegex(error.ProgrammingError,
                                      'cannot issue new commands'):
             reactor.callcommand(b'foo', {})
 
@@ -77,6 +83,11 @@
         self.assertEqual(request.state, b'sent')
 
 class BadFrameRecvTests(unittest.TestCase):
+    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
+        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
+        # the regex version.
+        assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
+
     def testoddstream(self):
         reactor = framing.clientreactor()
 
@@ -101,7 +112,7 @@
         for frame in meta[b'framegen']:
             pass
 
-        with self.assertRaisesRegexp(error.ProgrammingError,
+        with self.assertRaisesRegex(error.ProgrammingError,
                                      'unhandled frame type'):
             sendframe(reactor, ffs(b'1 0 stream-begin text-output 0 foo'))
 
diff --git a/tests/test-simplekeyvaluefile.py b/tests/test-simplekeyvaluefile.py
--- a/tests/test-simplekeyvaluefile.py
+++ b/tests/test-simplekeyvaluefile.py
@@ -51,24 +51,29 @@
         dr = scmutil.simplekeyvaluefile(self.vfs, 'kvfile').read()
         self.assertEqual(dr, dw)
 
+    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
+        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
+        # the regex version.
+        assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
+
     def testinvalidkeys(self):
         d = {'0key1': 'value1', 'Key2': 'value2'}
-        with self.assertRaisesRegexp(error.ProgrammingError,
+        with self.assertRaisesRegex(error.ProgrammingError,
                                      'keys must start with a letter.*'):
             scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
 
         d = {'key1@': 'value1', 'Key2': 'value2'}
-        with self.assertRaisesRegexp(error.ProgrammingError, 'invalid key.*'):
+        with self.assertRaisesRegex(error.ProgrammingError, 'invalid key.*'):
             scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
 
     def testinvalidvalues(self):
         d = {'key1': 'value1', 'Key2': 'value2\n'}
-        with self.assertRaisesRegexp(error.ProgrammingError,  'invalid val.*'):
+        with self.assertRaisesRegex(error.ProgrammingError,  'invalid val.*'):
             scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
 
     def testcorruptedfile(self):
         self.vfs.contents['badfile'] = 'ababagalamaga\n'
-        with self.assertRaisesRegexp(error.CorruptedState,
+        with self.assertRaisesRegex(error.CorruptedState,
                                      'dictionary.*element.*'):
             scmutil.simplekeyvaluefile(self.vfs, 'badfile').read()
 



To: durin42, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list