]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41877 Check for asert, aseert, assrt in mocks (GH-23165)
authorvabr-g <vabr@google.com>
Thu, 5 Nov 2020 17:04:38 +0000 (18:04 +0100)
committerGitHub <noreply@github.com>
Thu, 5 Nov 2020 17:04:38 +0000 (09:04 -0800)
Currently, a Mock object which is not unsafe will raise an
AttributeError if an attribute with the prefix assert or assret is
accessed on it. This protects against misspellings of real assert
method calls, which lead to tests passing silently even if the tested
code does not satisfy the intended assertion.

Recently a check was done in a large code base (Google) and three
more frequent ways of misspelling assert were found causing harm:
asert, aseert, assrt. These are now added to the existing check.

Lib/unittest/mock.py
Lib/unittest/test/testmock/testmock.py
Misc/NEWS.d/next/Library/2020-11-05-16-00-03.bpo-41877.FHbngM.rst [new file with mode: 0644]

index b495a5f6ccc017eed3859be17843acbd7c857fca..f5f502f257244cc22afa7a9934690272f0d0d957 100644 (file)
@@ -631,9 +631,9 @@ class NonCallableMock(Base):
         elif _is_magic(name):
             raise AttributeError(name)
         if not self._mock_unsafe:
-            if name.startswith(('assert', 'assret')):
+            if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')):
                 raise AttributeError("Attributes cannot start with 'assert' "
-                                     "or 'assret'")
+                                     "or its misspellings")
 
         result = self._mock_children.get(name)
         if result is _deleted:
index ce674e713e99cd5a2ab7ae18c7fcf83ae9efd3b7..194ce3f61bbfdde666c6c96dcb2fe23c5798b14f 100644 (file)
@@ -1598,14 +1598,23 @@ class MockTest(unittest.TestCase):
     #Issue21238
     def test_mock_unsafe(self):
         m = Mock()
-        msg = "Attributes cannot start with 'assert' or 'assret'"
+        msg = "Attributes cannot start with 'assert' or its misspellings"
         with self.assertRaisesRegex(AttributeError, msg):
             m.assert_foo_call()
         with self.assertRaisesRegex(AttributeError, msg):
             m.assret_foo_call()
+        with self.assertRaisesRegex(AttributeError, msg):
+            m.asert_foo_call()
+        with self.assertRaisesRegex(AttributeError, msg):
+            m.aseert_foo_call()
+        with self.assertRaisesRegex(AttributeError, msg):
+            m.assrt_foo_call()
         m = Mock(unsafe=True)
         m.assert_foo_call()
         m.assret_foo_call()
+        m.asert_foo_call()
+        m.aseert_foo_call()
+        m.assrt_foo_call()
 
     #Issue21262
     def test_assert_not_called(self):
diff --git a/Misc/NEWS.d/next/Library/2020-11-05-16-00-03.bpo-41877.FHbngM.rst b/Misc/NEWS.d/next/Library/2020-11-05-16-00-03.bpo-41877.FHbngM.rst
new file mode 100644 (file)
index 0000000..6f6fccb
--- /dev/null
@@ -0,0 +1,2 @@
+Mock objects which are not unsafe will now raise an AttributeError if an attribute with the prefix asert, aseert,\r
+or assrt is accessed, in addition to this already happening for the prefixes assert or assret.
\ No newline at end of file