1import os 2import sys 3import functools 4import subprocess 5import platform 6 7import pytest 8import jaraco.envs 9import path 10 11 12IS_PYPY = '__pypy__' in sys.builtin_module_names 13 14 15class VirtualEnv(jaraco.envs.VirtualEnv): 16 name = '.env' 17 18 def run(self, cmd, *args, **kwargs): 19 cmd = [self.exe(cmd[0])] + cmd[1:] 20 return subprocess.check_output(cmd, *args, cwd=self.root, **kwargs) 21 22 23@pytest.fixture 24def venv(tmp_path, tmp_src): 25 env = VirtualEnv() 26 env.root = path.Path(tmp_path / 'venv') 27 env.req = str(tmp_src) 28 return env.create() 29 30 31def popen_text(call): 32 """ 33 Augment the Popen call with the parameters to ensure unicode text. 34 """ 35 return functools.partial(call, universal_newlines=True) \ 36 if sys.version_info < (3, 7) else functools.partial(call, text=True) 37 38 39def find_distutils(venv, imports='distutils', env=None, **kwargs): 40 py_cmd = 'import {imports}; print(distutils.__file__)'.format(**locals()) 41 cmd = ['python', '-c', py_cmd] 42 if platform.system() == 'Windows': 43 env['SYSTEMROOT'] = os.environ['SYSTEMROOT'] 44 return popen_text(venv.run)(cmd, env=env, **kwargs) 45 46 47def test_distutils_stdlib(venv): 48 """ 49 Ensure stdlib distutils is used when appropriate. 50 """ 51 env = dict(SETUPTOOLS_USE_DISTUTILS='stdlib') 52 assert venv.name not in find_distutils(venv, env=env).split(os.sep) 53 54 55def test_distutils_local_with_setuptools(venv): 56 """ 57 Ensure local distutils is used when appropriate. 58 """ 59 env = dict(SETUPTOOLS_USE_DISTUTILS='local') 60 loc = find_distutils(venv, imports='setuptools, distutils', env=env) 61 assert venv.name in loc.split(os.sep) 62 63 64@pytest.mark.xfail('IS_PYPY', reason='pypy imports distutils on startup') 65def test_distutils_local(venv): 66 """ 67 Even without importing, the setuptools-local copy of distutils is 68 preferred. 69 """ 70 env = dict(SETUPTOOLS_USE_DISTUTILS='local') 71 assert venv.name in find_distutils(venv, env=env).split(os.sep) 72