diff options
-rw-r--r-- | NEWS.d/2018-10-18-17-57-28.bpo-35022.KeEF4T.rst | 2 | ||||
-rw-r--r-- | mock/mock.py | 3 | ||||
-rw-r--r-- | mock/tests/testmagicmethods.py | 12 |
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): |