Skip to content

Commit 572244c

Browse files
chryslesbidoulwebknjaz
committed
Add pip_editable_parts pytest fixture
And run Python in isolated mode when building sdists in `noxfile.py`. Co-authored-by: Stéphane Bidoul <[email protected]> Co-authored-by: Sviatoslav Sydorenko (Святослав Сидоренко) <[email protected]>
1 parent 1ca95b6 commit 572244c

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test(session: nox.Session) -> None:
9191
# fmt: off
9292
session.install("build")
9393
session.run(
94-
"python", "-m", "build", "--sdist", "--outdir", sdist_dir,
94+
"python", "-I", "-m", "build", "--sdist", "--outdir", sdist_dir,
9595
silent=True,
9696
)
9797
# fmt: on

tests/conftest.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import re
77
import shutil
8+
import subprocess
89
import sys
910
import threading
1011
from dataclasses import dataclass
@@ -371,6 +372,37 @@ def not_code_files_and_folders(path: str, names: List[str]) -> Iterable[str]:
371372
return pip_src
372373

373374

375+
@pytest.fixture(scope="session")
376+
def pip_editable_parts(
377+
pip_src: Path, tmpdir_factory: pytest.TempPathFactory
378+
) -> Tuple[Path, Path, str]:
379+
pip_editable = tmpdir_factory.mktemp("pip") / "pip"
380+
shutil.copytree(pip_src, pip_editable, symlinks=True)
381+
# noxfile.py is Python 3 only
382+
assert compileall.compile_dir(
383+
pip_editable,
384+
quiet=1,
385+
rx=re.compile("noxfile.py$"),
386+
)
387+
pip_self_install_path = tmpdir_factory.mktemp("pip_self_install")
388+
subprocess.check_call(
389+
[
390+
sys.executable,
391+
"-m",
392+
"pip",
393+
"install",
394+
"--target",
395+
pip_self_install_path,
396+
"-e",
397+
pip_editable,
398+
]
399+
)
400+
pth = next(pip_self_install_path.glob("*pip*.pth"))
401+
dist_info = next(pip_self_install_path.glob("*.dist-info"))
402+
dist_info_name = os.path.basename(dist_info)
403+
return (pth, dist_info, dist_info_name)
404+
405+
374406
def _common_wheel_editable_install(
375407
tmpdir_factory: pytest.TempPathFactory, common_wheels: Path, package: str
376408
) -> Path:
@@ -434,6 +466,7 @@ def virtualenv_template(
434466
request: pytest.FixtureRequest,
435467
tmpdir_factory: pytest.TempPathFactory,
436468
pip_src: Path,
469+
pip_editable_parts: Tuple[Path, Path, str],
437470
setuptools_install: Path,
438471
wheel_install: Path,
439472
coverage_install: Path,
@@ -451,16 +484,16 @@ def virtualenv_template(
451484
# Install setuptools, wheel and pip.
452485
install_pth_link(venv, "setuptools", setuptools_install)
453486
install_pth_link(venv, "wheel", wheel_install)
454-
pip_editable = tmpdir_factory.mktemp("pip") / "pip"
455-
shutil.copytree(pip_src, pip_editable, symlinks=True)
456487

457-
# noxfile.py is Python 3 only
458-
assert compileall.compile_dir(
459-
str(pip_editable),
460-
quiet=1,
461-
rx=re.compile("noxfile.py$"),
462-
)
463-
install_pth_link(venv, "pip", pip_editable / "src")
488+
pth, dist_info, dist_info_name = pip_editable_parts
489+
490+
# Preserve ``.dist-info`` directory inside ``site-packages``
491+
dist_info_path = os.path.join(venv.site, dist_info_name)
492+
os.mkdir(dist_info_path)
493+
494+
shutil.copy(pth, venv.site)
495+
shutil.copytree(dist_info, dist_info_path, dirs_exist_ok=True, symlinks=True)
496+
venv.site.joinpath("easy-install.pth").touch()
464497

465498
# Install coverage and pth file for executing it in any spawned processes
466499
# in this virtual environment.

0 commit comments

Comments
 (0)