aboutsummaryrefslogtreecommitdiff
path: root/pyfakefs
diff options
context:
space:
mode:
authormrbean-bremen <hansemrbean@googlemail.com>2022-06-06 18:18:36 +0200
committermrbean-bremen <mrbean-bremen@users.noreply.github.com>2022-06-07 19:32:35 +0200
commitbbba0c626746f8cb93caf42f11fc85fa387d675b (patch)
tree3f7269496df307a29e83b6cdfe7a52930019c7db /pyfakefs
parent95e9fb5ee24c759f333be6eb3b95e811149c15ea (diff)
downloadpyfakefs-bbba0c626746f8cb93caf42f11fc85fa387d675b.tar.gz
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
Diffstat (limited to 'pyfakefs')
-rw-r--r--pyfakefs/fake_pathlib.py14
-rw-r--r--pyfakefs/tests/fake_pathlib_test.py29
2 files changed, 36 insertions, 7 deletions
diff --git a/pyfakefs/fake_pathlib.py b/pyfakefs/fake_pathlib.py
index 5935c61..e1179a2 100644
--- a/pyfakefs/fake_pathlib.py
+++ b/pyfakefs/fake_pathlib.py
@@ -754,20 +754,22 @@ class FakePathlibModule:
__slots__ = ()
def owner(self):
- """Return the current user name. It is assumed that the fake
- file system was created by the current user.
+ """Return the username of the file owner.
+ It is assumed that `st_uid` is related to a real user,
+ otherwise `KeyError` is raised.
"""
import pwd
- return pwd.getpwuid(os.getuid()).pw_name
+ return pwd.getpwuid(self.stat().st_uid).pw_name
def group(self):
- """Return the current group name. It is assumed that the fake
- file system was created by the current user.
+ """Return the group name of the file group.
+ It is assumed that `st_gid` is related to a real group,
+ otherwise `KeyError` is raised.
"""
import grp
- return grp.getgrgid(os.getgid()).gr_name
+ return grp.getgrgid(self.stat().st_gid).gr_name
Path = FakePath
diff --git a/pyfakefs/tests/fake_pathlib_test.py b/pyfakefs/tests/fake_pathlib_test.py
index 0ffe082..70baef9 100644
--- a/pyfakefs/tests/fake_pathlib_test.py
+++ b/pyfakefs/tests/fake_pathlib_test.py
@@ -26,6 +26,8 @@ import pathlib
import stat
import sys
import unittest
+from collections import namedtuple
+from unittest import mock
from pyfakefs import fake_pathlib, fake_filesystem, fake_filesystem_unittest
from pyfakefs.fake_filesystem import is_root
@@ -1073,12 +1075,37 @@ class FakePathlibUsageInOsFunctionsTest(RealPathlibTestCase):
@unittest.skipIf(sys.platform == 'win32',
'no pwd and grp modules in Windows')
def test_owner_and_group_posix(self):
- self.check_posix_only()
path = self.make_path('some_file')
self.create_file(path)
self.assertTrue(self.path(path).owner())
self.assertTrue(self.path(path).group())
+ @unittest.skipIf(sys.platform == 'win32',
+ 'no pwd and grp modules in Windows')
+ def test_changed_owner_and_group(self):
+ def fake_getpwuid(uid):
+ if uid == 42:
+ user_struct = namedtuple('user', 'pw_name, pw_uid')
+ user_struct.pw_name = 'NewUser'
+ return user_struct
+ raise KeyError
+
+ def fake_getgrgid(uid):
+ if uid == 5:
+ group_struct = namedtuple('group', 'gr_name, gr_gid')
+ group_struct.gr_name = 'NewGroup'
+ return group_struct
+ raise KeyError
+
+ self.skip_real_fs()
+ path = self.make_path('some_file')
+ self.create_file(path)
+ self.os.chown(path, 42, 5)
+ with mock.patch('pwd.getpwuid', fake_getpwuid):
+ with mock.patch('grp.getgrgid', fake_getgrgid):
+ self.assertEqual('NewUser', self.path(path).owner())
+ self.assertEqual('NewGroup', self.path(path).group())
+
def test_owner_and_group_windows(self):
self.check_windows_only()
path = self.make_path('some_file')