Skip to content

Commit

Permalink
Avoid distutils imports in tests
Browse files Browse the repository at this point in the history
The tests still don't run without distutils
because they require virtualenv < 20 (and virtualenv 16 uses distutils),
but at least they don't import distutils directly now.

Fixes pypa#11521
  • Loading branch information
hroncok committed Oct 17, 2022
1 parent a8ba0ee commit fe7948a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
Empty file.
43 changes: 37 additions & 6 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import distutils
import os
import re
import ssl
import sys
import sysconfig
import textwrap
from os.path import curdir, join, pardir
from pathlib import Path
Expand Down Expand Up @@ -1145,6 +1145,39 @@ def main(): pass
assert "--no-warn-script-location" not in result.stderr, str(result)


def _change_root(new_root: str, pathname: str) -> str:
"""
Adapted from distutils.
Return 'pathname' with 'new_root' prepended. If 'pathname' is
relative, this is equivalent to "os.path.join(new_root,pathname)".
Otherwise, it requires making 'pathname' relative and then joining the
two, which is tricky on DOS/Windows and Mac OS.
"""
try:
from distutils.util import change_root
except ImportError:
pass
else:
return change_root(new_root, pathname)

if os.name == "posix":
if not os.path.isabs(pathname):
return os.path.join(new_root, pathname)
else:
return os.path.join(new_root, pathname[1:])

elif os.name == "nt":
drive, path = os.path.splitdrive(pathname)
if path[0] == "\\":
path = path[1:]
return os.path.join(new_root, path)

else:
# distutils raise DistutilsPlatformError here
raise RuntimeError(f"nothing known about platform '{os.name}'")


@pytest.mark.usefixtures("with_wheel")
def test_install_package_with_root(script: PipTestEnvironment, data: TestData) -> None:
"""
Expand All @@ -1163,10 +1196,8 @@ def test_install_package_with_root(script: PipTestEnvironment, data: TestData) -
normal_install_path = os.fspath(
script.base_path / script.site_packages / "simple-1.0.dist-info"
)
# use distutils to change the root exactly how the --root option does it
from distutils.util import change_root

root_path = change_root(os.path.join(script.scratch, "root"), normal_install_path)
root_path = _change_root(os.path.join(script.scratch, "root"), normal_install_path)
result.did_create(root_path)

# Should show find-links location in output
Expand Down Expand Up @@ -1195,7 +1226,7 @@ def test_install_package_with_prefix(

rel_prefix_path = script.scratch / "prefix"
install_path = join(
distutils.sysconfig.get_python_lib(prefix=rel_prefix_path),
sysconfig.get_path("purelib", vars={"base": rel_prefix_path}),
# we still test for egg-info because no-binary implies setup.py install
f"simple-1.0-py{pyversion}.egg-info",
)
Expand All @@ -1217,7 +1248,7 @@ def _test_install_editable_with_prefix(
"prefix", "lib", f"python{pyversion}", "site-packages"
)
else:
site_packages = distutils.sysconfig.get_python_lib(prefix="prefix")
site_packages = sysconfig.get_path("purelib", vars={"base": "prefix"})

# make sure target path is in PYTHONPATH
pythonpath = script.scratch_path / site_packages
Expand Down
6 changes: 4 additions & 2 deletions tests/functional/test_install_wheel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import base64
import csv
import distutils
import hashlib
import os
import shutil
import sysconfig
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -284,7 +284,9 @@ def test_install_wheel_with_prefix(
"--find-links",
tmpdir,
)
lib = distutils.sysconfig.get_python_lib(prefix=os.path.join("scratch", "prefix"))
lib = sysconfig.get_path(
"purelib", vars={"base": os.path.join("scratch", "prefix")}
)
result.did_create(lib)


Expand Down

0 comments on commit fe7948a

Please sign in to comment.