Skip to content

Commit 001f6db

Browse files
committed
move root checks to test.support
1 parent 52c0186 commit 001f6db

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

Lib/test/support/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"BrokenIter",
7272
"in_systemd_nspawn_sync_suppressed",
7373
"run_no_yield_async_fn", "run_yielding_async_fn", "async_yield",
74-
"reset_code", "on_github_actions"
74+
"reset_code", "on_github_actions", "requires_root", "requires_non_root",
7575
]
7676

7777

@@ -3317,3 +3317,8 @@ def control_characters_c0() -> list[str]:
33173317
C0 control characters defined as the byte range 0x00-0x1F, and 0x7F.
33183318
"""
33193319
return [chr(c) for c in range(0x00, 0x20)] + ["\x7F"]
3320+
3321+
3322+
_ROOT_IN_POSIX = hasattr(os, 'geteuid') and os.geteuid() == 0
3323+
requires_root = unittest.skipUnless(_ROOT_IN_POSIX, "test needs root privilege")
3324+
requires_non_root = unittest.skipIf(_ROOT_IN_POSIX, "test needs non-root account")

Lib/test/test_mailbox.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from test.support import import_helper, warnings_helper
1212
from test.support import os_helper
1313
from test.support import refleak_helper
14+
from test.support import requires_root
1415
from test.support import socket_helper
1516
import unittest
1617
import textwrap
@@ -1086,6 +1087,7 @@ def test_permissions_after_flush(self):
10861087

10871088
self.assertEqual(os.stat(self._path).st_mode, mode)
10881089

1090+
@requires_root
10891091
@unittest.skipUnless(hasattr(os, 'chown'), 'requires os.chown')
10901092
def test_ownership_after_flush(self):
10911093
# See issue gh-117467
@@ -1108,10 +1110,7 @@ def test_ownership_after_flush(self):
11081110
else:
11091111
self.skipTest("test needs more than one group")
11101112

1111-
try:
1112-
os.chown(self._path, other_uid, other_gid)
1113-
except OSError:
1114-
self.skipTest('test needs root privilege')
1113+
os.chown(self._path, other_uid, other_gid)
11151114
# Change permissions as in test_permissions_after_flush.
11161115
mode = st.st_mode | 0o666
11171116
os.chmod(self._path, mode)

Lib/test/test_os/test_os.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from test.support import os_helper
3434
from test.support import socket_helper
3535
from test.support import infinite_recursion
36+
from test.support import requires_root
37+
from test.support import requires_non_root
3638
from test.support import warnings_helper
3739
from platform import win32_is_iot
3840
from .utils import create_file
@@ -67,10 +69,6 @@
6769
from test.support.os_helper import FakePath
6870

6971

70-
root_in_posix = False
71-
if hasattr(os, 'geteuid'):
72-
root_in_posix = (os.geteuid() == 0)
73-
7472
# Detect whether we're on a Linux system that uses the (now outdated
7573
# and unmaintained) linuxthreads threading library. There's an issue
7674
# when combining linuxthreads with a failed execv call: see
@@ -2257,8 +2255,8 @@ def test_chown_gid(self):
22572255
gid = os.stat(os_helper.TESTFN).st_gid
22582256
self.assertEqual(gid, gid_2)
22592257

2260-
@unittest.skipUnless(root_in_posix and len(all_users) > 1,
2261-
"test needs root privilege and more than one user")
2258+
@requires_root
2259+
@unittest.skipUnless(len(all_users) > 1, "test needs more than one user")
22622260
def test_chown_with_root(self):
22632261
uid_1, uid_2 = all_users[:2]
22642262
gid = os.stat(os_helper.TESTFN).st_gid
@@ -2269,8 +2267,8 @@ def test_chown_with_root(self):
22692267
uid = os.stat(os_helper.TESTFN).st_uid
22702268
self.assertEqual(uid, uid_2)
22712269

2272-
@unittest.skipUnless(not root_in_posix and len(all_users) > 1,
2273-
"test needs non-root account and more than one user")
2270+
@requires_non_root
2271+
@unittest.skipUnless(len(all_users) > 1, "test needs and more than one user")
22742272
def test_chown_without_permission(self):
22752273
uid_1, uid_2 = all_users[:2]
22762274
gid = os.stat(os_helper.TESTFN).st_gid

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from test.support import is_emscripten, is_wasi, is_wasm32
2121
from test.support import infinite_recursion
2222
from test.support import os_helper
23+
from test.support import requires_root
2324
from test.support.os_helper import TESTFN, FS_NONASCII, FakePath
2425
try:
2526
import fcntl
@@ -35,11 +36,6 @@
3536
posix = None
3637

3738

38-
root_in_posix = False
39-
if hasattr(os, 'geteuid'):
40-
root_in_posix = (os.geteuid() == 0)
41-
42-
4339
def patch_replace(old_test):
4440
def new_replace(self, target):
4541
raise OSError(errno.EXDEV, "Cross-device link", self, target)
@@ -1554,7 +1550,7 @@ def raiser(*args, **kwargs):
15541550
self.assertRaises(FileNotFoundError, source.copy, target)
15551551

15561552
@unittest.skipIf(sys.platform == "win32" or sys.platform == "wasi", "directories are always readable on Windows and WASI")
1557-
@unittest.skipIf(root_in_posix, "test fails with root privilege")
1553+
@requires_root
15581554
def test_copy_dir_no_read_permission(self):
15591555
base = self.cls(self.base)
15601556
source = base / 'dirE'
@@ -2027,7 +2023,7 @@ def test_owner(self):
20272023
self.assertEqual(expected_name, p.owner())
20282024

20292025
@unittest.skipUnless(pwd, "the pwd module is needed for this test")
2030-
@unittest.skipUnless(root_in_posix, "test needs root privilege")
2026+
@requires_root
20312027
def test_owner_no_follow_symlinks(self):
20322028
all_users = [u.pw_uid for u in pwd.getpwall()]
20332029
if len(all_users) < 2:
@@ -2062,7 +2058,7 @@ def test_group(self):
20622058
self.assertEqual(expected_name, p.group())
20632059

20642060
@unittest.skipUnless(grp, "the grp module is needed for this test")
2065-
@unittest.skipUnless(root_in_posix, "test needs root privilege")
2061+
@requires_root
20662062
def test_group_no_follow_symlinks(self):
20672063
all_groups = [g.gr_gid for g in grp.getgrall()]
20682064
if len(all_groups) < 2:
@@ -3216,15 +3212,14 @@ def test_touch_mode(self):
32163212
st = os.stat(self.parser.join(self.base, 'masked_new_file'))
32173213
self.assertEqual(stat.S_IMODE(st.st_mode), 0o750)
32183214

3215+
@unittest.skipUnless(pwd, "the pwd module is needed for this test")
32193216
@unittest.skipUnless(hasattr(pwd, 'getpwall'),
32203217
'pwd module does not expose getpwall()')
32213218
@unittest.skipIf(sys.platform == "vxworks",
32223219
"no home directory on VxWorks")
32233220
@needs_posix
32243221
def test_expanduser_posix(self):
32253222
P = self.cls
3226-
import_helper.import_module('pwd')
3227-
import pwd
32283223
pwdent = pwd.getpwuid(os.getuid())
32293224
username = pwdent.pw_name
32303225
userhome = pwdent.pw_dir.rstrip('/') or '/'

0 commit comments

Comments
 (0)