changeset 32279:68c43a416585

tests: use context manager form of assertRaises Support for using unittest.TestCase.assertRaises as a context manager was added in Python 2.7. This form is more readable, especially for complex tests. While I was here, I also restored the use of assertRaisesRegexp, which was removed in c6921568cd20 for Python 2.6 compatibility.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 13 May 2017 11:52:44 -0700
parents 7c3ef55dedbe
children 9b81fb217820
files tests/test-atomictempfile.py tests/test-ctxmanager.py tests/test-lock.py tests/test-manifest.py tests/test-simplekeyvaluefile.py tests/test-simplemerge.py
diffstat 6 files changed, 26 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/tests/test-atomictempfile.py	Sat May 13 11:42:42 2017 -0700
+++ b/tests/test-atomictempfile.py	Sat May 13 11:52:44 2017 -0700
@@ -47,7 +47,8 @@
     # if a programmer screws up and passes bad args to atomictempfile, they
     # get a plain ordinary TypeError, not infinite recursion
     def testoops(self):
-        self.assertRaises(TypeError, atomictempfile)
+        with self.assertRaises(TypeError):
+            atomictempfile()
 
     # checkambig=True avoids ambiguity of timestamp
     def testcheckambig(self):
--- a/tests/test-ctxmanager.py	Sat May 13 11:42:42 2017 -0700
+++ b/tests/test-ctxmanager.py	Sat May 13 11:52:44 2017 -0700
@@ -55,23 +55,21 @@
     def test_raise_on_enter(self):
         trace = []
         addtrace = trace.append
-        def go():
+        with self.assertRaises(ctxerror):
             with util.ctxmanager(ctxmgr('a', addtrace),
                                  lambda: raise_on_enter('b', addtrace)) as c:
                 c.enter()
                 addtrace('unreachable')
-        self.assertRaises(ctxerror, go)
         self.assertEqual(trace, [('enter', 'a'), ('raise', 'b'), ('exit', 'a')])
 
     def test_raise_on_exit(self):
         trace = []
         addtrace = trace.append
-        def go():
+        with self.assertRaises(ctxerror):
             with util.ctxmanager(ctxmgr('a', addtrace),
                                  lambda: raise_on_exit('b', addtrace)) as c:
                 c.enter()
                 addtrace('running')
-        self.assertRaises(ctxerror, go)
         self.assertEqual(trace, [('enter', 'a'), ('enter', 'b'), 'running',
                                  ('raise', 'b'), ('exit', 'a')])
 
--- a/tests/test-lock.py	Sat May 13 11:42:42 2017 -0700
+++ b/tests/test-lock.py	Sat May 13 11:52:44 2017 -0700
@@ -260,12 +260,10 @@
         lock = state.makelock(inheritchecker=check)
         state.assertacquirecalled(True)
 
-        def tryinherit():
+        with self.assertRaises(error.LockInheritanceContractViolation):
             with lock.inherit():
                 pass
 
-        self.assertRaises(error.LockInheritanceContractViolation, tryinherit)
-
         lock.release()
 
     def testfrequentlockunlock(self):
--- a/tests/test-manifest.py	Sat May 13 11:42:42 2017 -0700
+++ b/tests/test-manifest.py	Sat May 13 11:52:44 2017 -0700
@@ -128,7 +128,8 @@
         self.assertEqual('l', m.flags('bar/baz/qux.py'))
         self.assertEqual(BIN_HASH_1, m['foo'])
         self.assertEqual('', m.flags('foo'))
-        self.assertRaises(KeyError, lambda : m['wat'])
+        with self.assertRaises(KeyError):
+            m['wat']
 
     def testParseManifestV2(self):
         m1 = self.parsemanifest(A_SHORT_MANIFEST)
@@ -213,7 +214,8 @@
         self.assertEqual('', m.flags('alpha'))
         self.assertEqual('l', m.flags('bar/baz/qux.py'))
         self.assertEqual('', m.flags('beta'))
-        self.assertRaises(KeyError, lambda : m['foo'])
+        with self.assertRaises(KeyError):
+            m['foo']
 
     def testSetGetNodeSuffix(self):
         clean = self.parsemanifest(A_SHORT_MANIFEST)
@@ -255,12 +257,14 @@
                 assert False
             return True
         match.matchfn = filt
-        self.assertRaises(AssertionError, m.matches, match)
+        with self.assertRaises(AssertionError):
+            m.matches(match)
 
     def testRemoveItem(self):
         m = self.parsemanifest(A_SHORT_MANIFEST)
         del m['foo']
-        self.assertRaises(KeyError, lambda : m['foo'])
+        with self.assertRaises(KeyError):
+            m['foo']
         self.assertEqual(1, len(m))
         self.assertEqual(1, len(list(m)))
         # now restore and make sure everything works right
--- a/tests/test-simplekeyvaluefile.py	Sat May 13 11:42:42 2017 -0700
+++ b/tests/test-simplekeyvaluefile.py	Sat May 13 11:52:44 2017 -0700
@@ -53,24 +53,24 @@
 
     def testinvalidkeys(self):
         d = {'0key1': 'value1', 'Key2': 'value2'}
-        self.assertRaises(error.ProgrammingError,
-                          scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write,
-                          d)
+        with self.assertRaisesRegexp(error.ProgrammingError,
+                                     'keys must start with a letter.*'):
+            scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
+
         d = {'key1@': 'value1', 'Key2': 'value2'}
-        self.assertRaises(error.ProgrammingError,
-                          scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write,
-                          d)
+        with self.assertRaisesRegexp(error.ProgrammingError, 'invalid key.*'):
+            scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
 
     def testinvalidvalues(self):
         d = {'key1': 'value1', 'Key2': 'value2\n'}
-        self.assertRaises(error.ProgrammingError,
-                          scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write,
-                          d)
+        with self.assertRaisesRegexp(error.ProgrammingError,  'invalid val.*'):
+            scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
 
     def testcorruptedfile(self):
         self.vfs.contents['badfile'] = 'ababagalamaga\n'
-        self.assertRaises(error.CorruptedState,
-                          scmutil.simplekeyvaluefile(self.vfs, 'badfile').read)
+        with self.assertRaisesRegexp(error.CorruptedState,
+                                     'dictionary.*element.*'):
+            scmutil.simplekeyvaluefile(self.vfs, 'badfile').read()
 
     def testfirstline(self):
         dw = {'key1': 'value1'}
--- a/tests/test-simplemerge.py	Sat May 13 11:42:42 2017 -0700
+++ b/tests/test-simplemerge.py	Sat May 13 11:52:44 2017 -0700
@@ -326,7 +326,8 @@
         self.assertEquals(ml, MERGED_RESULT)
 
     def test_binary(self):
-        self.assertRaises(error.Abort, Merge3, ['\x00'], ['a'], ['b'])
+        with self.assertRaises(error.Abort):
+            Merge3(['\x00'], ['a'], ['b'])
 
     def test_dos_text(self):
         base_text = 'a\r\n'