From 7c3a3817923de67956656ea058e80669b77bed1f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 8 Aug 2020 11:44:12 -0400 Subject: [PATCH 1/4] Add tests capturing expected distutils behavior. Ref #417. --- setuptools/tests/test_distutils_adoption.py | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 setuptools/tests/test_distutils_adoption.py diff --git a/setuptools/tests/test_distutils_adoption.py b/setuptools/tests/test_distutils_adoption.py new file mode 100644 index 0000000000..f4c7072d5c --- /dev/null +++ b/setuptools/tests/test_distutils_adoption.py @@ -0,0 +1,37 @@ +import os +import pytest + + +@pytest.fixture +def env(virtualenv): + virtualenv.run(['pip', 'uninstall', '-y', 'setuptools']) + virtualenv.run(['pip', 'install', os.getcwd()]) + return virtualenv + + +def find_distutils(env, imports='distutils'): + py_cmd = 'import {imports}; print(distutils.__file__)'.format(**locals()) + cmd = ['python', '-c', py_cmd] + return env.run(cmd, capture=True, text=True) + + +def test_distutils_stdlib(env): + """ + Ensure stdlib distutils is used when appropriate. + """ + assert '.env' not in find_distutils(env).split(os.sep) + + +def test_distutils_local_with_setuptools(env): + """ + Ensure local distutils is used when appropriate. + """ + env.env.update(SETUPTOOLS_USE_DISTUTILS='local') + loc = find_distutils(env, imports='setuptools, distutils') + assert '.env' in loc.split(os.sep) + + +@pytest.mark.xfail(reason="#2259") +def test_distutils_local(env): + env.env.update(SETUPTOOLS_USE_DISTUTILS='local') + assert '.env' in find_distutils(env).split(os.sep) From 6281bd0917882b930d5923d76cc72ff4ee5a7695 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 8 Aug 2020 12:15:13 -0400 Subject: [PATCH 2/4] Support Python 3.5 and 3.6 in the tests. --- setuptools/tests/test_distutils_adoption.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/setuptools/tests/test_distutils_adoption.py b/setuptools/tests/test_distutils_adoption.py index f4c7072d5c..a945a69fb3 100644 --- a/setuptools/tests/test_distutils_adoption.py +++ b/setuptools/tests/test_distutils_adoption.py @@ -1,7 +1,18 @@ import os +import sys +import functools + import pytest +def popen_text(call): + """ + Augment the Popen call with the parameters to ensure unicode text. + """ + return functools.partial(call, universal_newlines=True) \ + if sys.version_info < (3, 7) else functools.partial(call, text=True) + + @pytest.fixture def env(virtualenv): virtualenv.run(['pip', 'uninstall', '-y', 'setuptools']) @@ -12,7 +23,7 @@ def env(virtualenv): def find_distutils(env, imports='distutils'): py_cmd = 'import {imports}; print(distutils.__file__)'.format(**locals()) cmd = ['python', '-c', py_cmd] - return env.run(cmd, capture=True, text=True) + return popen_text(env.run)(cmd, capture=True) def test_distutils_stdlib(env): From 41deeb76fc88382f27b3d37cce75eda2c54cbc2a Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 8 Aug 2020 15:53:41 -0400 Subject: [PATCH 3/4] Bypass pytest-virtualenv due to bugs in 'run' command. Instead, use jaraco.envs.VirtualEnv and build a bespoke fixture with a run command. --- setup.cfg | 1 + setuptools/tests/requirements.txt | 1 + setuptools/tests/test_distutils_adoption.py | 48 +++++++++++++-------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/setup.cfg b/setup.cfg index 485714a3b6..87fa6bf273 100644 --- a/setup.cfg +++ b/setup.cfg @@ -72,6 +72,7 @@ tests = paver; python_version>="3.6" futures; python_version=="2.7" pip>=19.1 # For proper file:// URLs support. + jaraco.envs docs = # Keep these in sync with docs/requirements.txt diff --git a/setuptools/tests/requirements.txt b/setuptools/tests/requirements.txt index 19bf5aefd0..d0d07f70c0 100644 --- a/setuptools/tests/requirements.txt +++ b/setuptools/tests/requirements.txt @@ -10,3 +10,4 @@ pytest-cov>=2.5.1 paver; python_version>="3.6" futures; python_version=="2.7" pip>=19.1 # For proper file:// URLs support. +jaraco.envs diff --git a/setuptools/tests/test_distutils_adoption.py b/setuptools/tests/test_distutils_adoption.py index a945a69fb3..476b0a9e4b 100644 --- a/setuptools/tests/test_distutils_adoption.py +++ b/setuptools/tests/test_distutils_adoption.py @@ -1,8 +1,27 @@ import os import sys import functools +import subprocess import pytest +import jaraco.envs +import path + + +class VirtualEnv(jaraco.envs.VirtualEnv): + name = '.env' + + def run(self, cmd, *args, **kwargs): + cmd = [self.exe(cmd[0])] + cmd[1:] + return subprocess.check_output(cmd, *args, cwd=self.root, **kwargs) + + +@pytest.fixture +def venv(tmpdir): + env = VirtualEnv() + env.root = path.Path(tmpdir) + env.req = os.getcwd() + return env.create() def popen_text(call): @@ -13,36 +32,29 @@ def popen_text(call): if sys.version_info < (3, 7) else functools.partial(call, text=True) -@pytest.fixture -def env(virtualenv): - virtualenv.run(['pip', 'uninstall', '-y', 'setuptools']) - virtualenv.run(['pip', 'install', os.getcwd()]) - return virtualenv - - -def find_distutils(env, imports='distutils'): +def find_distutils(venv, imports='distutils', **kwargs): py_cmd = 'import {imports}; print(distutils.__file__)'.format(**locals()) cmd = ['python', '-c', py_cmd] - return popen_text(env.run)(cmd, capture=True) + return popen_text(venv.run)(cmd, **kwargs) -def test_distutils_stdlib(env): +def test_distutils_stdlib(venv): """ Ensure stdlib distutils is used when appropriate. """ - assert '.env' not in find_distutils(env).split(os.sep) + assert venv.name not in find_distutils(venv, env=dict()).split(os.sep) -def test_distutils_local_with_setuptools(env): +def test_distutils_local_with_setuptools(venv): """ Ensure local distutils is used when appropriate. """ - env.env.update(SETUPTOOLS_USE_DISTUTILS='local') - loc = find_distutils(env, imports='setuptools, distutils') - assert '.env' in loc.split(os.sep) + env = dict(SETUPTOOLS_USE_DISTUTILS='local') + loc = find_distutils(venv, imports='setuptools, distutils', env=env) + assert venv.name in loc.split(os.sep) @pytest.mark.xfail(reason="#2259") -def test_distutils_local(env): - env.env.update(SETUPTOOLS_USE_DISTUTILS='local') - assert '.env' in find_distutils(env).split(os.sep) +def test_distutils_local(venv): + env = dict(SETUPTOOLS_USE_DISTUTILS='local') + assert venv.name in find_distutils(venv, env=env).split(os.sep) From 47ae38fd6f233e6423404bfebfd24d3b98fb897c Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 8 Aug 2020 17:01:41 -0400 Subject: [PATCH 4/4] On Windows, SYSTEMROOT must be supplied. --- setuptools/tests/test_distutils_adoption.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/setuptools/tests/test_distutils_adoption.py b/setuptools/tests/test_distutils_adoption.py index 476b0a9e4b..7f28a217d6 100644 --- a/setuptools/tests/test_distutils_adoption.py +++ b/setuptools/tests/test_distutils_adoption.py @@ -2,6 +2,7 @@ import sys import functools import subprocess +import platform import pytest import jaraco.envs @@ -32,10 +33,12 @@ def popen_text(call): if sys.version_info < (3, 7) else functools.partial(call, text=True) -def find_distutils(venv, imports='distutils', **kwargs): +def find_distutils(venv, imports='distutils', env=None, **kwargs): py_cmd = 'import {imports}; print(distutils.__file__)'.format(**locals()) cmd = ['python', '-c', py_cmd] - return popen_text(venv.run)(cmd, **kwargs) + if platform.system() == 'Windows': + env['SYSTEMROOT'] = os.environ['SYSTEMROOT'] + return popen_text(venv.run)(cmd, env=env, **kwargs) def test_distutils_stdlib(venv):