Skip to content

Commit

Permalink
Change pathlib.Path.owner()/group() to behave like real os
Browse files Browse the repository at this point in the history
- 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 pytest-dev#678
  • Loading branch information
mrbean-bremen committed Jun 7, 2022
1 parent 95e9fb5 commit bbba0c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
8 changes: 6 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ The released versions correspond to PyPi releases.
longer officially supported by pyfakefs
** `os.stat_float_times` has been removed in Python 3.7 and is therefore no
longer supported
* added some support for the upcoming Python version 3.11
(see [#677](../../issues/677))
* under Windows, the root path is now effectively `C:\` instead of `\`; a
path starting with `\` points to the current drive as in the real file
system (see [#673](../../issues/673))
* fake `pathlib.Path.owner()` and `pathlib.Path.group()` now behave like the
real methods - they look up the real user/group name for the user/group id
that is associated with the fake file (see [#678](../../issues/678))

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

## [Version 4.5.6](https://pypi.python.org/pypi/pyfakefs/4.5.6) (2022-03-17)
Fixes a regression which broke tests with older pytest versions (< 3.9).
Expand Down
14 changes: 8 additions & 6 deletions pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,20 +754,22 @@ class PosixPath(FakePath, PurePosixPath):
__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

Expand Down
29 changes: 28 additions & 1 deletion pyfakefs/tests/fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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
Expand Down Expand Up @@ -1073,12 +1075,37 @@ def test_truncate(self):
@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')
Expand Down

0 comments on commit bbba0c6

Please sign in to comment.