From f545f29fe2981187b634cd25945601a4a7adb0fe Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 09:06:36 -0400 Subject: [PATCH 01/20] Revert "Exclude Python 3.11 on macOS due to lack of wheels. Ref pypa/distutils#165." This reverts commit 36f96c147ba9f0d471e48efed12da8f6f0b56d3f. --- .github/workflows/main.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 62f6fcefef..e244014dd4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,10 +20,6 @@ jobs: - ubuntu-latest - macos-latest - windows-latest - exclude: - # macOS is failing to build pyobjc (#165) - - platform: macos-latest - python: ~3.11.0-0 runs-on: ${{ matrix.platform }} steps: - uses: actions/checkout@v2 From 3f39e178279ca76f5d6090fb439000dcb3b4b2cd Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 12:17:20 -0400 Subject: [PATCH 02/20] Restore metadata tests (not discovered due to class name). --- distutils/tests/test_dist.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index ddfaf92167..25056af5d2 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -271,7 +271,7 @@ def _expander(path): @pytest.mark.usefixtures('save_env') @pytest.mark.usefixtures('save_argv') -class MetadataTestCase(support.TempdirManager): +class TestMetadata(support.TempdirManager): def format_metadata(self, dist): sio = io.StringIO() dist.metadata.write_pkg_file(sio) @@ -498,9 +498,10 @@ def test_fix_help_options(self): assert fancy_options[0] == ('a', 'b', 'c') assert fancy_options[1] == (1, 2, 3) - def test_show_help(self): + def test_show_help(self, request): # smoke test, just makes sure some help is displayed - self.addCleanup(log.set_threshold, log._global_log.threshold) + reset_log = functools.partial(log.set_threshold, log._global_log.threshold) + request.addfinalizer(reset_log) dist = Distribution() sys.argv = [] dist.help = 1 From ef78ba5de0acb557c3f8aecde9f8e2d8681b85b8 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Thu, 1 Sep 2022 21:33:53 +0100 Subject: [PATCH 03/20] Add DISTUTILS_EXTRA_CONFIG option for passing setup.cfg overrides during build --- distutils/dist.py | 4 ++++ distutils/tests/test_dist.py | 21 +++++++++++++++++++++ docs/distutils/configfile.rst | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/distutils/dist.py b/distutils/dist.py index 0406ab19cb..d854cd99ba 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -359,6 +359,10 @@ def find_config_files(self): if os.path.isfile(local_file): files.append(local_file) + extra_file = os.getenv("DISTUTILS_EXTRA_CONFIG") + if extra_file and os.path.isfile(extra_file): + files.append(extra_file) + if DEBUG: self.announce("using config files: %s" % ', '.join(files)) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 25056af5d2..e14d7da141 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -492,6 +492,27 @@ def test_custom_pydistutils(self): finally: os.remove(user_filename) + def test_extra_pydistutils(self): + # make sure pydistutils.cfg is found + extra_filename = "overrides.cfg" + + temp_dir = self.mkdtemp() + extra_filename = os.path.join(temp_dir, extra_filename) + with open(extra_filename, 'w') as f: + f.write('.') + + # Testing will have been going terribly if this was set, but preserve + # it anyway (so it goes terribly but *consistently*) + old_extra_filename = os.environ.get("DISTUTILS_EXTRA_CONFIG") + os.environ["DISTUTILS_EXTRA_CONFIG"] = extra_filename + try: + dist = Distribution() + files = dist.find_config_files() + assert user_filename in files + finally: + os.remove(user_filename) + os.environ["DISTUTILS_EXTRA_CONFIG"] = old_extra_filename + def test_fix_help_options(self): help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)] fancy_options = fix_help_options(help_tuples) diff --git a/docs/distutils/configfile.rst b/docs/distutils/configfile.rst index 2a5c8329e3..e03d6d437f 100644 --- a/docs/distutils/configfile.rst +++ b/docs/distutils/configfile.rst @@ -36,7 +36,8 @@ consequences: :file:`setup.py` * installers can override anything in :file:`setup.cfg` using the command-line - options to :file:`setup.py` + options to :file:`setup.py` or by pointing :envvar:`DISTUTILS_EXTRA_CONFIG` + to another configuration file The basic syntax of the configuration file is simple: From 825f02ab1073d430c2481bab8c44a41b6f061776 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Thu, 1 Sep 2022 22:12:07 +0100 Subject: [PATCH 04/20] Fix name in test --- distutils/tests/test_dist.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index e14d7da141..beb9924d70 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -508,9 +508,9 @@ def test_extra_pydistutils(self): try: dist = Distribution() files = dist.find_config_files() - assert user_filename in files + assert extra_filename in files finally: - os.remove(user_filename) + os.remove(extra_filename) os.environ["DISTUTILS_EXTRA_CONFIG"] = old_extra_filename def test_fix_help_options(self): From 457df3899d61839dbe7966d6152a34d0fc6a1318 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 12:08:36 -0400 Subject: [PATCH 05/20] Prefer monkeypatch for setting environment variable --- distutils/tests/test_dist.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index beb9924d70..33da08cb3e 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -492,7 +492,7 @@ def test_custom_pydistutils(self): finally: os.remove(user_filename) - def test_extra_pydistutils(self): + def test_extra_pydistutils(self, monkeypatch): # make sure pydistutils.cfg is found extra_filename = "overrides.cfg" @@ -501,17 +501,13 @@ def test_extra_pydistutils(self): with open(extra_filename, 'w') as f: f.write('.') - # Testing will have been going terribly if this was set, but preserve - # it anyway (so it goes terribly but *consistently*) - old_extra_filename = os.environ.get("DISTUTILS_EXTRA_CONFIG") - os.environ["DISTUTILS_EXTRA_CONFIG"] = extra_filename + monkeypatch.setenv('DISTUTILS_EXTRA_CONFIG', extra_filename) try: dist = Distribution() files = dist.find_config_files() assert extra_filename in files finally: os.remove(extra_filename) - os.environ["DISTUTILS_EXTRA_CONFIG"] = old_extra_filename def test_fix_help_options(self): help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)] From a3614615895f69a0ff9b0c3139ecfd6180d30882 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 12:20:35 -0400 Subject: [PATCH 06/20] Use tmp_path and jaraco.path.build to build files. --- distutils/tests/test_dist.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 33da08cb3e..5c05ad402f 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -8,6 +8,7 @@ import unittest.mock as mock import pytest +import jaraco.path from distutils.dist import Distribution, fix_help_options from distutils.cmd import Command @@ -492,22 +493,14 @@ def test_custom_pydistutils(self): finally: os.remove(user_filename) - def test_extra_pydistutils(self, monkeypatch): - # make sure pydistutils.cfg is found - extra_filename = "overrides.cfg" - - temp_dir = self.mkdtemp() - extra_filename = os.path.join(temp_dir, extra_filename) - with open(extra_filename, 'w') as f: - f.write('.') + def test_extra_pydistutils(self, monkeypatch, tmp_path): + jaraco.path.build({'overrides.cfg': '.'}, tmp_path) + filename = tmp_path / 'overrides.cfg' - monkeypatch.setenv('DISTUTILS_EXTRA_CONFIG', extra_filename) - try: - dist = Distribution() - files = dist.find_config_files() - assert extra_filename in files - finally: - os.remove(extra_filename) + monkeypatch.setenv('DISTUTILS_EXTRA_CONFIG', filename) + dist = Distribution() + files = dist.find_config_files() + assert str(filename) in files def test_fix_help_options(self): help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)] From 57de35b347650fe43a1cc98109c46c3174ebd090 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 12:28:05 -0400 Subject: [PATCH 07/20] Use functools.lru_cache to memoize check_environ. --- distutils/tests/test_util.py | 7 +++---- distutils/util.py | 12 +++--------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/distutils/tests/test_util.py b/distutils/tests/test_util.py index 605b0d40b7..070a277069 100644 --- a/distutils/tests/test_util.py +++ b/distutils/tests/test_util.py @@ -136,17 +136,16 @@ def _join(*path): # XXX platforms to be covered: mac def test_check_environ(self): - util._environ_checked = 0 + util.check_environ.cache_clear() os.environ.pop('HOME', None) check_environ() assert os.environ['PLAT'] == get_platform() - assert util._environ_checked == 1 @pytest.mark.skipif("os.name != 'posix'") def test_check_environ_getpwuid(self): - util._environ_checked = 0 + util.check_environ.cache_clear() os.environ.pop('HOME', None) import pwd @@ -159,7 +158,7 @@ def test_check_environ_getpwuid(self): check_environ() assert os.environ['HOME'] == '/home/distutils' - util._environ_checked = 0 + util.check_environ.cache_clear() os.environ.pop('HOME', None) # bpo-10496: Catch pwd.getpwuid() error diff --git a/distutils/util.py b/distutils/util.py index d95992ec99..4763202b67 100644 --- a/distutils/util.py +++ b/distutils/util.py @@ -11,6 +11,8 @@ import subprocess import sys import sysconfig +import functools + from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError from distutils.dep_util import newer from distutils.spawn import spawn @@ -170,9 +172,7 @@ def change_root(new_root, pathname): raise DistutilsPlatformError(f"nothing known about platform '{os.name}'") -_environ_checked = 0 - - +@functools.lru_cache() def check_environ(): """Ensure that 'os.environ' has all the environment variables we guarantee that users can use in config files, command-line options, @@ -181,10 +181,6 @@ def check_environ(): PLAT - description of the current platform, including hardware and OS (see 'get_platform()') """ - global _environ_checked - if _environ_checked: - return - if os.name == 'posix' and 'HOME' not in os.environ: try: import pwd @@ -198,8 +194,6 @@ def check_environ(): if 'PLAT' not in os.environ: os.environ['PLAT'] = get_platform() - _environ_checked = 1 - def subst_vars(s, local_vars): """ From c7d65dd5ee20e61bdd8f58768aeafb2562db8aa4 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 12:50:50 -0400 Subject: [PATCH 08/20] Use pathlib for generating paths and generate the paths in a separate function, consolidating 'is_file' check. --- distutils/dist.py | 38 +++++++++++++----------------------- distutils/tests/test_dist.py | 18 ++++++----------- 2 files changed, 20 insertions(+), 36 deletions(-) diff --git a/distutils/dist.py b/distutils/dist.py index 0406ab19cb..7cdda5b9c3 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -7,6 +7,7 @@ import sys import os import re +import pathlib from email import message_from_file try: @@ -331,38 +332,27 @@ def find_config_files(self): The file in the user's home directory can be disabled with the --no-user-cfg option. """ - files = [] check_environ() + files = [str(path) for path in self._gen_paths() if path.is_file()] - # Where to look for the system-wide Distutils config file - sys_dir = os.path.dirname(sys.modules['distutils'].__file__) + if DEBUG: + self.announce("using config files: %s" % ', '.join(files)) - # Look for the system config file - sys_file = os.path.join(sys_dir, "distutils.cfg") - if os.path.isfile(sys_file): - files.append(sys_file) + return files - # What to call the per-user config file - if os.name == 'posix': - user_filename = ".pydistutils.cfg" - else: - user_filename = "pydistutils.cfg" + def _gen_paths(self): + # The system-wide Distutils config file + sys_dir = pathlib.Path(sys.modules['distutils'].__file__).parent + yield sys_dir / "distutils.cfg" - # And look for the user config file + # The per-user config file + prefix = '.' * (os.name == 'posix') + filename = prefix + 'pydistutils.cfg' if self.want_user_cfg: - user_file = os.path.join(os.path.expanduser('~'), user_filename) - if os.path.isfile(user_file): - files.append(user_file) + yield pathlib.Path('~').expanduser() / filename # All platforms support local setup.cfg - local_file = "setup.cfg" - if os.path.isfile(local_file): - files.append(local_file) - - if DEBUG: - self.announce("using config files: %s" % ', '.join(files)) - - return files + yield pathlib.Path('setup.cfg') def parse_config_files(self, filenames=None): # noqa: C901 from configparser import ConfigParser diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 25056af5d2..43d946d1a2 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -240,7 +240,7 @@ def test_announce(self): with pytest.raises(ValueError): dist.announce(args, kwargs) - def test_find_config_files_disable(self): + def test_find_config_files_disable(self, monkeypatch): # Ticket #1180: Allow user to disable their home config file. temp_home = self.mkdtemp() if os.name == 'posix': @@ -251,19 +251,13 @@ def test_find_config_files_disable(self): with open(user_filename, 'w') as f: f.write('[distutils]\n') - def _expander(path): - return temp_home + monkeypatch.setenv('HOME', temp_home) - old_expander = os.path.expanduser - os.path.expanduser = _expander - try: - d = Distribution() - all_files = d.find_config_files() + d = Distribution() + all_files = d.find_config_files() - d = Distribution(attrs={'script_args': ['--no-user-cfg']}) - files = d.find_config_files() - finally: - os.path.expanduser = old_expander + d = Distribution(attrs={'script_args': ['--no-user-cfg']}) + files = d.find_config_files() # make sure --no-user-cfg disables the user cfg file assert len(all_files) - 1 == len(files) From 98a46dbaebc53fcb10b0643fadad98f76ba36e1b Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:20:16 -0400 Subject: [PATCH 09/20] Update docs to reference environment variable. --- distutils/dist.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/distutils/dist.py b/distutils/dist.py index 25227584ec..cf6a8bf683 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -324,14 +324,14 @@ def find_config_files(self): should be parsed. The filenames returned are guaranteed to exist (modulo nasty race conditions). - There are three possible config files: distutils.cfg in the - Distutils installation directory (ie. where the top-level - Distutils __inst__.py file lives), a file in the user's home - directory named .pydistutils.cfg on Unix and pydistutils.cfg - on Windows/Mac; and setup.cfg in the current directory. - - The file in the user's home directory can be disabled with the - --no-user-cfg option. + There are multiple possible config files: + - distutils.cfg in the Distutils installation directory (i.e. + where the top-level Distutils __inst__.py file lives) + - a file in the user's home directory named .pydistutils.cfg + on Unix and pydistutils.cfg on Windows/Mac; may be disabled + with the ``--no-user-cfg`` option + - setup.cfg in the current directory + - a file named by an environment variable """ check_environ() files = [str(path) for path in self._gen_paths() if path.is_file()] @@ -355,6 +355,7 @@ def _gen_paths(self): # All platforms support local setup.cfg yield pathlib.Path('setup.cfg') + # Additional config indicated in the environment with contextlib.suppress(TypeError): yield pathlib.Path(os.getenv("DISTUTILS_EXTRA_CONFIG")) From 8620c041db42ee84f71fa16a6680e6219b9d0dd7 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:22:50 -0400 Subject: [PATCH 10/20] Rename environment variable to DIST_EXTRA_CONFIG, decoupling it from the name of the implementation. --- distutils/dist.py | 2 +- distutils/tests/test_dist.py | 2 +- docs/distutils/configfile.rst | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/distutils/dist.py b/distutils/dist.py index cf6a8bf683..1dc25fe541 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -357,7 +357,7 @@ def _gen_paths(self): # Additional config indicated in the environment with contextlib.suppress(TypeError): - yield pathlib.Path(os.getenv("DISTUTILS_EXTRA_CONFIG")) + yield pathlib.Path(os.getenv("DIST_EXTRA_CONFIG")) def parse_config_files(self, filenames=None): # noqa: C901 from configparser import ConfigParser diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 3058d942a7..333ce0142b 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -491,7 +491,7 @@ def test_extra_pydistutils(self, monkeypatch, tmp_path): jaraco.path.build({'overrides.cfg': '.'}, tmp_path) filename = tmp_path / 'overrides.cfg' - monkeypatch.setenv('DISTUTILS_EXTRA_CONFIG', filename) + monkeypatch.setenv('DIST_EXTRA_CONFIG', filename) dist = Distribution() files = dist.find_config_files() assert str(filename) in files diff --git a/docs/distutils/configfile.rst b/docs/distutils/configfile.rst index e03d6d437f..bdd7c4550a 100644 --- a/docs/distutils/configfile.rst +++ b/docs/distutils/configfile.rst @@ -36,7 +36,7 @@ consequences: :file:`setup.py` * installers can override anything in :file:`setup.cfg` using the command-line - options to :file:`setup.py` or by pointing :envvar:`DISTUTILS_EXTRA_CONFIG` + options to :file:`setup.py` or by pointing :envvar:`DIST_EXTRA_CONFIG` to another configuration file The basic syntax of the configuration file is simple: From 19c0e938a312cdd7114957a5600002f53265d732 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:35:19 -0400 Subject: [PATCH 11/20] Simplify logic in test_custom_pydistutils --- distutils/tests/test_dist.py | 52 +++++++++++++----------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 333ce0142b..7ee5fac1d4 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -451,41 +451,27 @@ def test_long_description(self): meta = meta.replace('\n' + 8 * ' ', '\n') assert long_desc in meta - def test_custom_pydistutils(self): - # fixes #2166 - # make sure pydistutils.cfg is found - if os.name == 'posix': - user_filename = ".pydistutils.cfg" - else: - user_filename = "pydistutils.cfg" + def test_custom_pydistutils(self, tmp_path): + """ + pydistutils.cfg is found + """ + prefix = '.' * (os.name == 'posix') + filename = prefix + 'pydistutils.cfg' + jaraco.path.build({filename: '.'}, tmp_path) + config_path = tmp_path / filename - temp_dir = self.mkdtemp() - user_filename = os.path.join(temp_dir, user_filename) - f = open(user_filename, 'w') - try: - f.write('.') - finally: - f.close() + dist = Distribution() - try: - dist = Distribution() - - # linux-style - if sys.platform in ('linux', 'darwin'): - os.environ['HOME'] = temp_dir - files = dist.find_config_files() - assert user_filename in files - - # win32-style - if sys.platform == 'win32': - # home drive should be found - os.environ['USERPROFILE'] = temp_dir - files = dist.find_config_files() - assert user_filename in files, '{!r} not found in {!r}'.format( - user_filename, files - ) - finally: - os.remove(user_filename) + # linux-style + if sys.platform in ('linux', 'darwin'): + os.environ['HOME'] = str(tmp_path) + + # win32-style + if sys.platform == 'win32': + # home drive should be found + os.environ['USERPROFILE'] = str(tmp_path) + + assert str(config_path) in dist.find_config_files() def test_extra_pydistutils(self, monkeypatch, tmp_path): jaraco.path.build({'overrides.cfg': '.'}, tmp_path) From 4ea21eaf5730c6e4920753a2e4ae50c17c5fe688 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:36:40 -0400 Subject: [PATCH 12/20] Inline variables used once --- distutils/tests/test_dist.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 7ee5fac1d4..7b01c43edb 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -460,8 +460,6 @@ def test_custom_pydistutils(self, tmp_path): jaraco.path.build({filename: '.'}, tmp_path) config_path = tmp_path / filename - dist = Distribution() - # linux-style if sys.platform in ('linux', 'darwin'): os.environ['HOME'] = str(tmp_path) @@ -471,16 +469,13 @@ def test_custom_pydistutils(self, tmp_path): # home drive should be found os.environ['USERPROFILE'] = str(tmp_path) - assert str(config_path) in dist.find_config_files() + assert str(config_path) in Distribution().find_config_files() def test_extra_pydistutils(self, monkeypatch, tmp_path): jaraco.path.build({'overrides.cfg': '.'}, tmp_path) filename = tmp_path / 'overrides.cfg' - monkeypatch.setenv('DIST_EXTRA_CONFIG', filename) - dist = Distribution() - files = dist.find_config_files() - assert str(filename) in files + assert str(filename) in Distribution().find_config_files() def test_fix_help_options(self): help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)] From 66d0ccd88f5ed8982dac5ead51d10ddcc7127a6e Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:37:15 -0400 Subject: [PATCH 13/20] Remove meaningless dot from config files --- distutils/tests/test_dist.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 7b01c43edb..e4c5a45de9 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -457,7 +457,7 @@ def test_custom_pydistutils(self, tmp_path): """ prefix = '.' * (os.name == 'posix') filename = prefix + 'pydistutils.cfg' - jaraco.path.build({filename: '.'}, tmp_path) + jaraco.path.build({filename: ''}, tmp_path) config_path = tmp_path / filename # linux-style @@ -472,7 +472,7 @@ def test_custom_pydistutils(self, tmp_path): assert str(config_path) in Distribution().find_config_files() def test_extra_pydistutils(self, monkeypatch, tmp_path): - jaraco.path.build({'overrides.cfg': '.'}, tmp_path) + jaraco.path.build({'overrides.cfg': ''}, tmp_path) filename = tmp_path / 'overrides.cfg' monkeypatch.setenv('DIST_EXTRA_CONFIG', filename) assert str(filename) in Distribution().find_config_files() From 1587bbb27703bed0bd8306e33bf06f29d346ba9e Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:43:52 -0400 Subject: [PATCH 14/20] Extract a temp_home fixture. --- conftest.py | 7 +++++++ distutils/tests/test_dist.py | 15 +++------------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/conftest.py b/conftest.py index 018e1075bc..2040659e10 100644 --- a/conftest.py +++ b/conftest.py @@ -156,3 +156,10 @@ def suppress_path_mangle(monkeysession): monkeysession.setattr( ccompiler.CCompiler, '_make_relative', staticmethod(lambda x: x) ) + + +@pytest.fixture +def temp_home(tmp_path, monkeypatch): + var = 'USERPROFILE' if platform.system() == 'Windows' else 'HOME' + monkeypatch.setenv(var, str(tmp_path)) + return tmp_path diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index e4c5a45de9..2c2c74091c 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -451,23 +451,14 @@ def test_long_description(self): meta = meta.replace('\n' + 8 * ' ', '\n') assert long_desc in meta - def test_custom_pydistutils(self, tmp_path): + def test_custom_pydistutils(self, temp_home): """ pydistutils.cfg is found """ prefix = '.' * (os.name == 'posix') filename = prefix + 'pydistutils.cfg' - jaraco.path.build({filename: ''}, tmp_path) - config_path = tmp_path / filename - - # linux-style - if sys.platform in ('linux', 'darwin'): - os.environ['HOME'] = str(tmp_path) - - # win32-style - if sys.platform == 'win32': - # home drive should be found - os.environ['USERPROFILE'] = str(tmp_path) + jaraco.path.build({filename: ''}, temp_home) + config_path = temp_home / filename assert str(config_path) in Distribution().find_config_files() From b26aa119959532f1dee8fa88f9bbbc50e542ecac Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:45:01 -0400 Subject: [PATCH 15/20] Re-use temp_home in test_find_config_files_disable --- distutils/tests/test_dist.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 2c2c74091c..762fb666c3 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -241,9 +241,8 @@ def test_announce(self): with pytest.raises(ValueError): dist.announce(args, kwargs) - def test_find_config_files_disable(self, monkeypatch): + def test_find_config_files_disable(self, temp_home): # Ticket #1180: Allow user to disable their home config file. - temp_home = self.mkdtemp() if os.name == 'posix': user_filename = os.path.join(temp_home, ".pydistutils.cfg") else: @@ -252,8 +251,6 @@ def test_find_config_files_disable(self, monkeypatch): with open(user_filename, 'w') as f: f.write('[distutils]\n') - monkeypatch.setenv('HOME', temp_home) - d = Distribution() all_files = d.find_config_files() From aac4d4fcb3f7b9121bc2f717f3008a0bb154a20c Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:48:29 -0400 Subject: [PATCH 16/20] Extract property for pydistutils.cfg name --- distutils/tests/test_dist.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 762fb666c3..f576e7ad0a 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -448,14 +448,17 @@ def test_long_description(self): meta = meta.replace('\n' + 8 * ' ', '\n') assert long_desc in meta + @property + def pydistutilscfg(self): + prefix = '.' * (os.name == 'posix') + return prefix + 'pydistutils.cfg' + def test_custom_pydistutils(self, temp_home): """ pydistutils.cfg is found """ - prefix = '.' * (os.name == 'posix') - filename = prefix + 'pydistutils.cfg' - jaraco.path.build({filename: ''}, temp_home) - config_path = temp_home / filename + jaraco.path.build({self.pydistutilscfg: ''}, temp_home) + config_path = temp_home / self.pydistutilscfg assert str(config_path) in Distribution().find_config_files() From 00608cbc578f57eec45c80c6b2e6a4b4d69f29e8 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:53:15 -0400 Subject: [PATCH 17/20] Move property to a module attribute and re-use jaraco.path for simpler tests. --- distutils/tests/test_dist.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index f576e7ad0a..3f6adef9a0 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -19,6 +19,9 @@ from distutils import log +pydistutils_cfg = '.' * (os.name == 'posix') + 'pydistutils.cfg' + + class test_dist(Command): """Sample distutils extension command.""" @@ -243,13 +246,7 @@ def test_announce(self): def test_find_config_files_disable(self, temp_home): # Ticket #1180: Allow user to disable their home config file. - if os.name == 'posix': - user_filename = os.path.join(temp_home, ".pydistutils.cfg") - else: - user_filename = os.path.join(temp_home, "pydistutils.cfg") - - with open(user_filename, 'w') as f: - f.write('[distutils]\n') + jaraco.path.build({pydistutils_cfg: '[distutils]\n'}, temp_home) d = Distribution() all_files = d.find_config_files() @@ -448,17 +445,12 @@ def test_long_description(self): meta = meta.replace('\n' + 8 * ' ', '\n') assert long_desc in meta - @property - def pydistutilscfg(self): - prefix = '.' * (os.name == 'posix') - return prefix + 'pydistutils.cfg' - def test_custom_pydistutils(self, temp_home): """ pydistutils.cfg is found """ - jaraco.path.build({self.pydistutilscfg: ''}, temp_home) - config_path = temp_home / self.pydistutilscfg + jaraco.path.build({pydistutils_cfg: ''}, temp_home) + config_path = temp_home / pydistutils_cfg assert str(config_path) in Distribution().find_config_files() From 01d6f1d10453ee08462edbd2d1192b7114851c66 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 14:20:03 -0400 Subject: [PATCH 18/20] Use jaraco.path for more tests --- distutils/tests/test_dist.py | 54 +++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 3f6adef9a0..b4a2092ef3 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -101,26 +101,26 @@ def test_venv_install_options(self, request): fakepath = '/somedir' - with open(TESTFN, "w") as f: - print( - ( - "[install]\n" - "install-base = {0}\n" - "install-platbase = {0}\n" - "install-lib = {0}\n" - "install-platlib = {0}\n" - "install-purelib = {0}\n" - "install-headers = {0}\n" - "install-scripts = {0}\n" - "install-data = {0}\n" - "prefix = {0}\n" - "exec-prefix = {0}\n" - "home = {0}\n" - "user = {0}\n" - "root = {0}" - ).format(fakepath), - file=f, - ) + jaraco.path.build( + { + TESTFN: f""" + [install] + install-base = {fakepath} + install-platbase = {fakepath} + install-lib = {fakepath} + install-platlib = {fakepath} + install-purelib = {fakepath} + install-headers = {fakepath} + install-scripts = {fakepath} + install-data = {fakepath} + prefix = {fakepath} + exec-prefix = {fakepath} + home = {fakepath} + user = {fakepath} + root = {fakepath} + """, + } + ) # Base case: Not in a Virtual Environment with mock.patch.multiple(sys, prefix='/a', base_prefix='/a'): @@ -161,12 +161,14 @@ def test_venv_install_options(self, request): def test_command_packages_configfile(self, request, clear_argv): sys.argv.append("build") request.addfinalizer(functools.partial(os.unlink, TESTFN)) - f = open(TESTFN, "w") - try: - print("[global]", file=f) - print("command_packages = foo.bar, splat", file=f) - finally: - f.close() + jaraco.path.build( + { + TESTFN: """ + [global] + command_packages = foo.bar, splat + """, + } + ) d = self.create_distribution([TESTFN]) assert d.get_command_packages() == ["distutils.command", "foo.bar", "splat"] From d82d926fa2a1f98cae05b21528fe2fafebb52938 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 14:22:33 -0400 Subject: [PATCH 19/20] Fix warning in test --- distutils/tests/test_dist.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index b4a2092ef3..a943832620 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -459,7 +459,7 @@ def test_custom_pydistutils(self, temp_home): def test_extra_pydistutils(self, monkeypatch, tmp_path): jaraco.path.build({'overrides.cfg': ''}, tmp_path) filename = tmp_path / 'overrides.cfg' - monkeypatch.setenv('DIST_EXTRA_CONFIG', filename) + monkeypatch.setenv('DIST_EXTRA_CONFIG', str(filename)) assert str(filename) in Distribution().find_config_files() def test_fix_help_options(self): From 517587c209e25630a25de0f50e196e490463be71 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 14:27:03 -0400 Subject: [PATCH 20/20] Add changelog. --- changelog.d/3609.change.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/3609.change.rst diff --git a/changelog.d/3609.change.rst b/changelog.d/3609.change.rst new file mode 100644 index 0000000000..5ed5cc2512 --- /dev/null +++ b/changelog.d/3609.change.rst @@ -0,0 +1 @@ +Merge with pypa/distutils@d82d926 including support for DIST_EXTRA_CONFIG in pypa/distutils#177.