Skip to content

Commit ca9a356

Browse files
committed
Handle pathlib.Path.owner() and pathlib.Path.group()
- return the current user/group name under Posix - raises NotImplementedError under Windows - fixes #629
1 parent f4aac8e commit ca9a356

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The released versions correspond to PyPi releases.
1010
(see [#614](../../issues/614))
1111
* do not import pandas and related modules if it is not patched
1212
(see [#627](../../issues/627))
13+
* handle `pathlib.Path.owner()` and `pathlib.Path.group` by returning
14+
the current user/group name (see [#629](../../issues/629))
1315

1416
### Infrastructure
1517
* added test dependency check (see [#608](../../issues/608))

pyfakefs/fake_pathlib.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,12 +731,40 @@ class WindowsPath(FakePath, PureWindowsPath):
731731
"""
732732
__slots__ = ()
733733

734+
def owner(self):
735+
raise NotImplementedError(
736+
"Path.owner() is unsupported on this system")
737+
738+
def group(self):
739+
raise NotImplementedError(
740+
"Path.group() is unsupported on this system")
741+
742+
def is_mount(self):
743+
raise NotImplementedError(
744+
"Path.is_mount() is unsupported on this system")
745+
734746
class PosixPath(FakePath, PurePosixPath):
735747
"""A subclass of Path and PurePosixPath that represents
736748
concrete non-Windows filesystem paths.
737749
"""
738750
__slots__ = ()
739751

752+
def owner(self):
753+
"""Return the current user name. It is assumed that the fake
754+
file system was created by the current user.
755+
"""
756+
import pwd
757+
758+
return pwd.getpwuid(os.getuid()).pw_name
759+
760+
def group(self):
761+
"""Return the current group name. It is assumed that the fake
762+
file system was created by the current user.
763+
"""
764+
import grp
765+
766+
return grp.getgrgid(os.getgid()).gr_name
767+
740768
Path = FakePath
741769

742770
def __getattr__(self, name):

pyfakefs/tests/fake_pathlib_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,24 @@ def test_truncate(self):
10561056
st = self.os.stat(path)
10571057
self.assertEqual(4, st.st_size)
10581058

1059+
@unittest.skipIf(sys.platform == 'win32',
1060+
'no pwd and grp modules in Windows')
1061+
def test_owner_and_group_posix(self):
1062+
self.check_posix_only()
1063+
path = self.make_path('some_file')
1064+
self.create_file(path)
1065+
self.assertTrue(self.path(path).owner())
1066+
self.assertTrue(self.path(path).group())
1067+
1068+
def test_owner_and_group_windows(self):
1069+
self.check_windows_only()
1070+
path = self.make_path('some_file')
1071+
self.create_file(path)
1072+
with self.assertRaises(NotImplementedError):
1073+
self.path(path).owner()
1074+
with self.assertRaises(NotImplementedError):
1075+
self.path(path).group()
1076+
10591077

10601078
class RealPathlibUsageInOsFunctionsTest(FakePathlibUsageInOsFunctionsTest):
10611079
def use_real_fs(self):

0 commit comments

Comments
 (0)