Skip to content

Commit 518ab38

Browse files
committed
Add support for pathlib.Path.readlink (new in Python 3.9)
- fixes #584
1 parent b4e08dd commit 518ab38

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The released versions correspond to PyPi releases.
66
#### New Features
77
* added support for `pathlib.Path.link_to` (new in Python 3.8)
88
(see [#580](../../issues/580))
9+
* added support for `pathlib.Path.readlink` (new in Python 3.9)
10+
(see [#584](../../issues/584))
911
* added `FakeFilesystem.create_link` convenience method which creates
1012
intermittent directories (see [#580](../../issues/580))
1113

pyfakefs/fake_pathlib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ def lchmod(self, pathobj, mode):
128128
lambda fs, file_path, link_target:
129129
FakeFilesystem.link(fs, file_path, link_target))
130130

131+
if sys.version_info >= (3, 9):
132+
readlink = _wrap_strfunc(FakeFilesystem.readlink)
133+
131134
utime = _wrap_strfunc(FakeFilesystem.utime)
132135

133136

pyfakefs/tests/fake_pathlib_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ def test_relative_to(self):
238238
with self.assertRaises(ValueError):
239239
self.path('passwd').relative_to('/usr')
240240

241+
@unittest.skipIf(sys.version_info < (3, 9) or pathlib2,
242+
'readlink new in Python 3.9')
243+
def test_is_relative_to(self):
244+
path = self.path('/etc/passwd')
245+
self.assertTrue(path.is_relative_to('/etc'))
246+
self.assertFalse(path.is_relative_to('/src'))
247+
241248
def test_with_name(self):
242249
self.check_windows_only()
243250
self.assertEqual(
@@ -664,6 +671,16 @@ def test_pathlib2_does_not_have_link_to(self):
664671
path.link_to(link_name)
665672
self.assertFalse(self.os.path.exists(link_name))
666673

674+
@unittest.skipIf(sys.version_info < (3, 9) or pathlib2,
675+
'readlink new in Python 3.9')
676+
def test_readlink(self):
677+
self.skip_if_symlink_not_supported()
678+
link_path = self.make_path('foo', 'bar', 'baz')
679+
target = self.make_path('tarJAY')
680+
self.create_symlink(link_path, target)
681+
path = self.path(link_path)
682+
self.assert_equal_paths(path.readlink(), self.path(target))
683+
667684
def test_mkdir(self):
668685
dir_name = self.make_path('foo', 'bar')
669686
self.assert_raises_os_error(errno.ENOENT,
@@ -1074,4 +1091,4 @@ def test_add_existing_real_directory_with_pathlib_path(self):
10741091

10751092

10761093
if __name__ == '__main__':
1077-
unittest.main()
1094+
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)