Skip to content

Commit

Permalink
Merge pull request #2613 from pypa/feature/restore-editable-pythonpath
Browse files Browse the repository at this point in the history
Add test demonstrating how site dirs can be added for discoverability
  • Loading branch information
jaraco committed Mar 22, 2021
2 parents 8230bbf + 202c7a8 commit f5a062d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions setuptools/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import sys
import shutil
import subprocess

import pytest

Expand Down Expand Up @@ -58,3 +59,16 @@ def workaround_xdist_376(request):

with contextlib.suppress(ValueError):
sys.path.remove('')


@pytest.fixture
def sample_project(tmp_path):
"""
Clone the 'sampleproject' and return a path to it.
"""
cmd = ['git', 'clone', 'https://github.com/pypa/sampleproject']
try:
subprocess.check_call(cmd, cwd=str(tmp_path))
except Exception:
pytest.skip("Unable to clone sampleproject")
return tmp_path / 'sampleproject'
54 changes: 54 additions & 0 deletions setuptools/tests/test_develop.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io
import subprocess
import platform
import pathlib
import textwrap

from setuptools.command import test

Expand Down Expand Up @@ -199,3 +201,55 @@ def test_namespace_package_importable(self, tmpdir):
]
with test.test.paths_on_pythonpath([str(target)]):
subprocess.check_call(pkg_resources_imp)

@staticmethod
def install_workaround(site_packages):
site_packages.mkdir(parents=True)
sc = site_packages / 'sitecustomize.py'
sc.write_text(textwrap.dedent("""
import site
import pathlib
here = pathlib.Path(__file__).parent
site.addsitedir(str(here))
""").lstrip())

@pytest.mark.xfail(
platform.python_implementation() == 'PyPy',
reason="Workaround fails on PyPy (why?)",
)
def test_editable_prefix(self, tmp_path, sample_project):
"""
Editable install to a prefix should be discoverable.
"""
prefix = tmp_path / 'prefix'
prefix.mkdir()

# figure out where pip will likely install the package
site_packages = prefix / next(
pathlib.Path(path).relative_to(sys.prefix)
for path in sys.path
if 'site-packages' in path
and path.startswith(sys.prefix)
)

# install the workaround
self.install_workaround(site_packages)

env = dict(os.environ, PYTHONPATH=str(site_packages))
cmd = [
sys.executable,
'-m', 'pip',
'install',
'--editable',
str(sample_project),
'--prefix', str(prefix),
'--no-build-isolation',
]
subprocess.check_call(cmd, env=env)

# now run 'sample' with the prefix on the PYTHONPATH
bin = 'Scripts' if platform.system() == 'Windows' else 'bin'
exe = prefix / bin / 'sample'
if sys.version_info < (3, 7) and platform.system() == 'Windows':
exe = str(exe)
subprocess.check_call([exe], env=env)

0 comments on commit f5a062d

Please sign in to comment.