Mercurial > hg
changeset 37715:1859b9a7ddef
cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
The latter is deprecated on Python 3.7 and causes our tests to fail
due to the warning.
Differential Revision: https://phab.mercurial-scm.org/D3375
author | Augie Fackler <augie@google.com> |
---|---|
date | Sat, 14 Apr 2018 11:20:38 -0400 |
parents | 5dd71e9ae68a |
children | dfc51a482031 |
files | tests/test-simplekeyvaluefile.py tests/test-wireproto-clientreactor.py tests/test-wireproto-framing.py tests/test-wsgirequest.py |
diffstat | 4 files changed, 44 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/tests/test-simplekeyvaluefile.py Sat Apr 14 11:07:24 2018 -0400 +++ b/tests/test-simplekeyvaluefile.py Sat Apr 14 11:20:38 2018 -0400 @@ -51,24 +51,30 @@ 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 = (# camelcase-required + 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()
--- a/tests/test-wireproto-clientreactor.py Sat Apr 14 11:07:24 2018 -0400 +++ b/tests/test-wireproto-clientreactor.py Sat Apr 14 11:20:38 2018 -0400 @@ -24,6 +24,13 @@ 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 = (# camelcase-required + unittest.TestCase.assertRaisesRegexp) + def testbasic(self): reactor = framing.clientreactor(hasmultiplesend=False, buffersends=True) @@ -39,11 +46,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 +84,12 @@ 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 = (# camelcase-required + unittest.TestCase.assertRaisesRegexp) + def testoddstream(self): reactor = framing.clientreactor() @@ -101,7 +114,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'))
--- a/tests/test-wireproto-framing.py Sat Apr 14 11:07:24 2018 -0400 +++ b/tests/test-wireproto-framing.py Sat Apr 14 11:20:38 2018 -0400 @@ -103,19 +103,25 @@ 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 = (# camelcase-required + 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')])]))
--- a/tests/test-wsgirequest.py Sat Apr 14 11:07:24 2018 -0400 +++ b/tests/test-wsgirequest.py Sat Apr 14 11:20:38 2018 -0400 @@ -196,21 +196,27 @@ 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 = (# camelcase-required + 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',