Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The released versions correspond to PyPi releases.
(see [#614](../../issues/614))
* do not import pandas and related modules if it is not patched
(see [#627](../../issues/627))
* handle `pathlib.Path.owner()` and `pathlib.Path.group` by returning
the current user/group name (see [#629](../../issues/629))

### Infrastructure
* added test dependency check (see [#608](../../issues/608))
Expand Down
28 changes: 28 additions & 0 deletions pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,40 @@ class WindowsPath(FakePath, PureWindowsPath):
"""
__slots__ = ()

def owner(self):
raise NotImplementedError(
"Path.owner() is unsupported on this system")

def group(self):
raise NotImplementedError(
"Path.group() is unsupported on this system")

def is_mount(self):
raise NotImplementedError(
"Path.is_mount() is unsupported on this system")

class PosixPath(FakePath, PurePosixPath):
"""A subclass of Path and PurePosixPath that represents
concrete non-Windows filesystem paths.
"""
__slots__ = ()

def owner(self):
"""Return the current user name. It is assumed that the fake
file system was created by the current user.
"""
import pwd

return pwd.getpwuid(os.getuid()).pw_name

def group(self):
"""Return the current group name. It is assumed that the fake
file system was created by the current user.
"""
import grp

return grp.getgrgid(os.getgid()).gr_name

Path = FakePath

def __getattr__(self, name):
Expand Down
18 changes: 18 additions & 0 deletions pyfakefs/tests/fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,24 @@ def test_truncate(self):
st = self.os.stat(path)
self.assertEqual(4, st.st_size)

@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())

def test_owner_and_group_windows(self):
self.check_windows_only()
path = self.make_path('some_file')
self.create_file(path)
with self.assertRaises(NotImplementedError):
self.path(path).owner()
with self.assertRaises(NotImplementedError):
self.path(path).group()


class RealPathlibUsageInOsFunctionsTest(FakePathlibUsageInOsFunctionsTest):
def use_real_fs(self):
Expand Down