Skip to content

Commit f5a062d

Browse files
authored
Merge pull request #2613 from pypa/feature/restore-editable-pythonpath
Add test demonstrating how site dirs can be added for discoverability
2 parents 8230bbf + 202c7a8 commit f5a062d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

setuptools/tests/fixtures.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import contextlib
22
import sys
33
import shutil
4+
import subprocess
45

56
import pytest
67

@@ -58,3 +59,16 @@ def workaround_xdist_376(request):
5859

5960
with contextlib.suppress(ValueError):
6061
sys.path.remove('')
62+
63+
64+
@pytest.fixture
65+
def sample_project(tmp_path):
66+
"""
67+
Clone the 'sampleproject' and return a path to it.
68+
"""
69+
cmd = ['git', 'clone', 'https://github.com/pypa/sampleproject']
70+
try:
71+
subprocess.check_call(cmd, cwd=str(tmp_path))
72+
except Exception:
73+
pytest.skip("Unable to clone sampleproject")
74+
return tmp_path / 'sampleproject'

setuptools/tests/test_develop.py

+54
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import io
88
import subprocess
99
import platform
10+
import pathlib
11+
import textwrap
1012

1113
from setuptools.command import test
1214

@@ -199,3 +201,55 @@ def test_namespace_package_importable(self, tmpdir):
199201
]
200202
with test.test.paths_on_pythonpath([str(target)]):
201203
subprocess.check_call(pkg_resources_imp)
204+
205+
@staticmethod
206+
def install_workaround(site_packages):
207+
site_packages.mkdir(parents=True)
208+
sc = site_packages / 'sitecustomize.py'
209+
sc.write_text(textwrap.dedent("""
210+
import site
211+
import pathlib
212+
here = pathlib.Path(__file__).parent
213+
site.addsitedir(str(here))
214+
""").lstrip())
215+
216+
@pytest.mark.xfail(
217+
platform.python_implementation() == 'PyPy',
218+
reason="Workaround fails on PyPy (why?)",
219+
)
220+
def test_editable_prefix(self, tmp_path, sample_project):
221+
"""
222+
Editable install to a prefix should be discoverable.
223+
"""
224+
prefix = tmp_path / 'prefix'
225+
prefix.mkdir()
226+
227+
# figure out where pip will likely install the package
228+
site_packages = prefix / next(
229+
pathlib.Path(path).relative_to(sys.prefix)
230+
for path in sys.path
231+
if 'site-packages' in path
232+
and path.startswith(sys.prefix)
233+
)
234+
235+
# install the workaround
236+
self.install_workaround(site_packages)
237+
238+
env = dict(os.environ, PYTHONPATH=str(site_packages))
239+
cmd = [
240+
sys.executable,
241+
'-m', 'pip',
242+
'install',
243+
'--editable',
244+
str(sample_project),
245+
'--prefix', str(prefix),
246+
'--no-build-isolation',
247+
]
248+
subprocess.check_call(cmd, env=env)
249+
250+
# now run 'sample' with the prefix on the PYTHONPATH
251+
bin = 'Scripts' if platform.system() == 'Windows' else 'bin'
252+
exe = prefix / bin / 'sample'
253+
if sys.version_info < (3, 7) and platform.system() == 'Windows':
254+
exe = str(exe)
255+
subprocess.check_call([exe], env=env)

0 commit comments

Comments
 (0)