Skip to content

Commit f9a611b

Browse files
committed
Improve and extend the tests
1 parent d4070a3 commit f9a611b

File tree

1 file changed

+66
-21
lines changed

1 file changed

+66
-21
lines changed

Lib/test/test_pathlib.py

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
except ImportError:
2222
grp = pwd = None
2323

24+
try:
25+
import pwd
26+
all_users = [u.pw_uid for u in pwd.getpwall()]
27+
except (ImportError, AttributeError):
28+
all_users = []
29+
30+
root_in_posix = False
31+
if hasattr(os, 'geteuid'):
32+
root_in_posix = (os.geteuid() == 0)
2433

2534
class _BaseFlavourTest(object):
2635

@@ -1874,33 +1883,69 @@ def test_chmod(self):
18741883
p.chmod(new_mode)
18751884
self.assertEqual(p.stat().st_mode, new_mode)
18761885

1877-
def test_chown(self):
1886+
@unittest.skipUnless(root_in_posix and len(all_users) > 1,
1887+
"test needs root privilege and more than one user")
1888+
def test_chown_with_root(self):
1889+
# original uid and gid
18781890
p = self.cls(BASE) / 'fileA'
18791891
uid = p.stat().st_uid
18801892
gid = p.stat().st_gid
1881-
new_uid = 2000
1882-
new_gid = 2000
1883-
p.chown(uid=new_uid, gid=new_gid)
1884-
self.assertEqual(p.stat().st_uid, new_uid)
1885-
self.assertEqual(p.stat().st_gid, new_gid)
1886-
# Set back
1893+
1894+
# get users and groups for testing
1895+
uid_1, uid_2 = all_users[:2]
1896+
groups = os.getgroups()
1897+
if len(groups) < 2:
1898+
self.skipTest("test needs at least 2 groups")
1899+
gid_1, gid_2 = groups[:2]
1900+
1901+
p.chown(uid=uid_1, gid=gid_1)
1902+
self.assertEqual(p.stat().st_uid, uid_1)
1903+
self.assertEqual(p.stat().st_gid, gid_1)
1904+
p.chown(uid=uid_2, gid=gid_2)
1905+
self.assertEqual(p.stat().st_uid, uid_2)
1906+
self.assertEqual(p.stat().st_gid, gid_2)
1907+
1908+
# Set back to original
18871909
p.chown(uid=uid, gid=gid)
1888-
self.assertEqual(p.stat().st_uid, uid)
1889-
self.assertEqual(p.stat().st_gid, gid)
18901910

1891-
def test_lchown(self):
1911+
@unittest.skipUnless(not root_in_posix and len(all_users) > 1,
1912+
"test needs non-root account and more than one user")
1913+
def test_chown_without_permission(self):
18921914
p = self.cls(BASE) / 'fileA'
1893-
uid = p.stat().st_uid
1894-
gid = p.stat().st_gid
1895-
new_uid = 2000
1896-
new_gid = 2000
1897-
p.lchown(uid=new_uid, gid=new_gid)
1898-
self.assertEqual(p.stat().st_uid, new_uid)
1899-
self.assertEqual(p.stat().st_gid, new_gid)
1900-
# Set back
1901-
p.lchown(uid=uid, gid=gid)
1902-
self.assertEqual(p.stat().st_uid, uid)
1903-
self.assertEqual(p.stat().st_gid, gid)
1915+
1916+
new_uid = 503
1917+
new_gid = 503
1918+
with self.assertRaises(PermissionError):
1919+
p.chown(uid=new_uid, gid=new_gid)
1920+
1921+
@only_nt
1922+
def test_chown_windows(self):
1923+
p = self.cls(BASE) / 'fileA'
1924+
1925+
new_uid = 503
1926+
new_gid = 503
1927+
with self.assertRaises(NotImplementedError):
1928+
p.chown(uid=new_uid, gid=new_gid)
1929+
1930+
@only_posix
1931+
@mock.patch('pathlib.Path.chown')
1932+
def test_lchown(self, chown_mock):
1933+
new_uid = 503
1934+
new_gid = 503
1935+
1936+
p = self.cls(BASE) / 'fileA'
1937+
1938+
p.lchown(new_uid, new_gid)
1939+
chown_mock.assert_called_with(new_uid, new_gid, dir_fd=None, follow_symlinks=False)
1940+
1941+
@only_nt
1942+
def test_lchown_windows(self):
1943+
p = self.cls(BASE) / 'fileA'
1944+
1945+
new_uid = 503
1946+
new_gid = 503
1947+
with self.assertRaises(NotImplementedError):
1948+
p.lchown(uid=new_uid, gid=new_gid)
19041949

19051950
# On Windows, os.chmod does not follow symlinks (issue #15411)
19061951
@only_posix

0 commit comments

Comments
 (0)