Skip to content

Commit bbba0c6

Browse files
committed
Change pathlib.Path.owner()/group() to behave like real os
- both methods now look up the real user/group name for the user/group id that is associated with the fake file - assumes a valid user and group id is set - closes #678
1 parent 95e9fb5 commit bbba0c6

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

CHANGES.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ The released versions correspond to PyPi releases.
88
longer officially supported by pyfakefs
99
** `os.stat_float_times` has been removed in Python 3.7 and is therefore no
1010
longer supported
11-
* added some support for the upcoming Python version 3.11
12-
(see [#677](../../issues/677))
1311
* under Windows, the root path is now effectively `C:\` instead of `\`; a
1412
path starting with `\` points to the current drive as in the real file
1513
system (see [#673](../../issues/673))
14+
* fake `pathlib.Path.owner()` and `pathlib.Path.group()` now behave like the
15+
real methods - they look up the real user/group name for the user/group id
16+
that is associated with the fake file (see [#678](../../issues/678))
1617

18+
### New Features
19+
* added some support for the upcoming Python version 3.11
20+
(see [#677](../../issues/677))
1721

1822
## [Version 4.5.6](https://pypi.python.org/pypi/pyfakefs/4.5.6) (2022-03-17)
1923
Fixes a regression which broke tests with older pytest versions (< 3.9).

pyfakefs/fake_pathlib.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -754,20 +754,22 @@ class PosixPath(FakePath, PurePosixPath):
754754
__slots__ = ()
755755

756756
def owner(self):
757-
"""Return the current user name. It is assumed that the fake
758-
file system was created by the current user.
757+
"""Return the username of the file owner.
758+
It is assumed that `st_uid` is related to a real user,
759+
otherwise `KeyError` is raised.
759760
"""
760761
import pwd
761762

762-
return pwd.getpwuid(os.getuid()).pw_name
763+
return pwd.getpwuid(self.stat().st_uid).pw_name
763764

764765
def group(self):
765-
"""Return the current group name. It is assumed that the fake
766-
file system was created by the current user.
766+
"""Return the group name of the file group.
767+
It is assumed that `st_gid` is related to a real group,
768+
otherwise `KeyError` is raised.
767769
"""
768770
import grp
769771

770-
return grp.getgrgid(os.getgid()).gr_name
772+
return grp.getgrgid(self.stat().st_gid).gr_name
771773

772774
Path = FakePath
773775

pyfakefs/tests/fake_pathlib_test.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import stat
2727
import sys
2828
import unittest
29+
from collections import namedtuple
30+
from unittest import mock
2931

3032
from pyfakefs import fake_pathlib, fake_filesystem, fake_filesystem_unittest
3133
from pyfakefs.fake_filesystem import is_root
@@ -1073,12 +1075,37 @@ def test_truncate(self):
10731075
@unittest.skipIf(sys.platform == 'win32',
10741076
'no pwd and grp modules in Windows')
10751077
def test_owner_and_group_posix(self):
1076-
self.check_posix_only()
10771078
path = self.make_path('some_file')
10781079
self.create_file(path)
10791080
self.assertTrue(self.path(path).owner())
10801081
self.assertTrue(self.path(path).group())
10811082

1083+
@unittest.skipIf(sys.platform == 'win32',
1084+
'no pwd and grp modules in Windows')
1085+
def test_changed_owner_and_group(self):
1086+
def fake_getpwuid(uid):
1087+
if uid == 42:
1088+
user_struct = namedtuple('user', 'pw_name, pw_uid')
1089+
user_struct.pw_name = 'NewUser'
1090+
return user_struct
1091+
raise KeyError
1092+
1093+
def fake_getgrgid(uid):
1094+
if uid == 5:
1095+
group_struct = namedtuple('group', 'gr_name, gr_gid')
1096+
group_struct.gr_name = 'NewGroup'
1097+
return group_struct
1098+
raise KeyError
1099+
1100+
self.skip_real_fs()
1101+
path = self.make_path('some_file')
1102+
self.create_file(path)
1103+
self.os.chown(path, 42, 5)
1104+
with mock.patch('pwd.getpwuid', fake_getpwuid):
1105+
with mock.patch('grp.getgrgid', fake_getgrgid):
1106+
self.assertEqual('NewUser', self.path(path).owner())
1107+
self.assertEqual('NewGroup', self.path(path).group())
1108+
10821109
def test_owner_and_group_windows(self):
10831110
self.check_windows_only()
10841111
path = self.make_path('some_file')

0 commit comments

Comments
 (0)