Skip to content

Commit 1d98a15

Browse files
committed
Extract grp/pwd handling to a unix_compat module.
1 parent 2a95a48 commit 1d98a15

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

distutils/tests/test_archive_util.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@
1414
from distutils.spawn import find_executable, spawn
1515
from distutils.tests import support
1616
from test.support import run_unittest, patch
17+
from .unix_compat import require_unix_id, require_uid_0, grp, pwd, UID_0_SUPPORT
1718

1819
from .py38compat import change_cwd
1920
from .py38compat import check_warnings
2021

21-
try:
22-
import grp
23-
import pwd
24-
UID_GID_SUPPORT = True
25-
except ImportError:
26-
UID_GID_SUPPORT = False
2722

2823
try:
2924
import zipfile
@@ -339,7 +334,7 @@ def test_make_archive_xztar(self):
339334
def test_make_archive_owner_group(self):
340335
# testing make_archive with owner and group, with various combinations
341336
# this works even if there's not gid/uid support
342-
if UID_GID_SUPPORT and sys.platform != "cygwin":
337+
if UID_0_SUPPORT:
343338
group = grp.getgrgid(0)[0]
344339
owner = pwd.getpwuid(0)[0]
345340
else:
@@ -364,8 +359,8 @@ def test_make_archive_owner_group(self):
364359
self.assertTrue(os.path.exists(res))
365360

366361
@unittest.skipUnless(ZLIB_SUPPORT, "Requires zlib")
367-
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
368-
@unittest.skipUnless(sys.platform != "cygwin", "Cygwin doesn't have UID=0")
362+
@require_unix_id
363+
@require_uid_0
369364
def test_tarfile_root_owner(self):
370365
tmpdir = self._create_files()
371366
base_name = os.path.join(self.mkdtemp(), 'archive')

distutils/tests/test_sdist.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""Tests for distutils.command.sdist."""
22
import os
3-
import sys
43
import tarfile
54
import unittest
65
import warnings
76
import zipfile
87
from os.path import join
98
from textwrap import dedent
109
from test.support import captured_stdout, run_unittest
10+
from .unix_compat import require_unix_id, require_uid_0, pwd, grp
1111

1212
from .py38compat import check_warnings
1313

@@ -17,13 +17,6 @@
1717
except ImportError:
1818
ZLIB_SUPPORT = False
1919

20-
try:
21-
import grp
22-
import pwd
23-
UID_GID_SUPPORT = True
24-
except ImportError:
25-
UID_GID_SUPPORT = False
26-
2720
from distutils.command.sdist import sdist, show_formats
2821
from distutils.core import Distribution
2922
from distutils.tests.test_config import BasePyPIRCCommandTestCase
@@ -441,8 +434,8 @@ def test_manual_manifest(self):
441434
'fake-1.0/README.manual'])
442435

443436
@unittest.skipUnless(ZLIB_SUPPORT, "requires zlib")
444-
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
445-
@unittest.skipUnless(sys.platform != "cygwin", "Cygwin doesn't have UID=0")
437+
@require_unix_id
438+
@require_uid_0
446439
@unittest.skipIf(find_executable('tar') is None,
447440
"The tar command is not found")
448441
@unittest.skipIf(find_executable('gzip') is None,

distutils/tests/unix_compat.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
import unittest
3+
4+
try:
5+
import grp
6+
import pwd
7+
except ImportError:
8+
grp = pwd = None
9+
10+
11+
UNIX_ID_SUPPORT = grp and pwd
12+
UID_0_SUPPORT = UNIX_ID_SUPPORT and sys.platform != "cygwin"
13+
14+
require_unix_id = unittest.skipUnless(
15+
UNIX_ID_SUPPORT, "Requires grp and pwd support")
16+
require_uid_0 = unittest.skipUnless(UID_0_SUPPORT, "Requires UID 0 support")

0 commit comments

Comments
 (0)