summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax BeĢlanger <aeromax@gmail.com>2018-10-25 14:48:58 -0700
committerChris Withers <chris@withers.org>2019-04-30 08:39:55 +0100
commit55971296dc33650318a6397b21d4c4296a8dbca1 (patch)
tree28920f1b7aa06ba51428ffb4f569e714bd9089f2
parentd42410a09aeac50d5bb8a84bc1ab97dfd70ce909 (diff)
downloadmock-55971296dc33650318a6397b21d4c4296a8dbca1.tar.gz
bpo-35022: unittest.mock.MagicMock now also supports __fspath__ (GH-9960)
The MagicMock class supports many magic methods, but not __fspath__. To ease testing with modules such as os.path, this function is now supported by default. Backports: 6c83d9f4a72905d968418bef670bb3091d2744db Signed-off-by: Chris Withers <chris@simplistix.co.uk> Needed some re-working as this can only work and apply on Python 3
-rw-r--r--NEWS.d/2018-10-18-17-57-28.bpo-35022.KeEF4T.rst2
-rw-r--r--mock/mock.py3
-rw-r--r--mock/tests/testmagicmethods.py12
3 files changed, 16 insertions, 1 deletions
diff --git a/NEWS.d/2018-10-18-17-57-28.bpo-35022.KeEF4T.rst b/NEWS.d/2018-10-18-17-57-28.bpo-35022.KeEF4T.rst
new file mode 100644
index 0000000..426be70
--- /dev/null
+++ b/NEWS.d/2018-10-18-17-57-28.bpo-35022.KeEF4T.rst
@@ -0,0 +1,2 @@
+:class:`unittest.mock.MagicMock` now supports the ``__fspath__`` method
+(from :class:`os.PathLike`).
diff --git a/mock/mock.py b/mock/mock.py
index a414f3a..6e790f1 100644
--- a/mock/mock.py
+++ b/mock/mock.py
@@ -1864,7 +1864,7 @@ inplace = ' '.join('i%s' % n for n in numerics.split())
right = ' '.join('r%s' % n for n in numerics.split())
extra = ''
if six.PY3:
- extra = 'bool next '
+ extra = 'bool next fspath '
else:
extra = 'unicode long nonzero oct hex truediv rtruediv '
@@ -1909,6 +1909,7 @@ _calculate_return_value = {
'__str__': lambda self: object.__str__(self),
'__sizeof__': lambda self: object.__sizeof__(self),
'__unicode__': lambda self: unicode(object.__str__(self)),
+ '__fspath__': lambda self: type(self).__name__+'/'+self._extract_mock_name()+'/'+str(id(self)),
}
_return_values = {
diff --git a/mock/tests/testmagicmethods.py b/mock/tests/testmagicmethods.py
index 1056b2a..705b9d1 100644
--- a/mock/tests/testmagicmethods.py
+++ b/mock/tests/testmagicmethods.py
@@ -12,6 +12,7 @@ except NameError:
long = int
import math
+import os
import sys
import textwrap
import unittest
@@ -361,6 +362,17 @@ class TestMockingMagicMethods(unittest.TestCase):
self.assertEqual(mock, object())
+ def test_magic_methods_fspath(self):
+ mock = MagicMock()
+ if six.PY2:
+ self.assertRaises(AttributeError, lambda: mock.__fspath__)
+ else:
+ expected_path = mock.__fspath__()
+ mock.reset_mock()
+ self.assertEqual(os.fspath(mock), expected_path)
+ mock.__fspath__.assert_called_once()
+
+
def test_magic_methods_and_spec(self):
class Iterable(object):
def __iter__(self):