Skip to content

Commit

Permalink
test_download_use_pep517_propagation: use a different approach
Browse files Browse the repository at this point in the history
The approach it uses now doesn't work anymore due to 452d7da.
The installation of `fake_dep` now succeeds whether or not `setuptools`
is installed in the test environment.

Use a different approach instead: try to import `pip` in the `setup.py`
script. If it succeeds, then we are not running in an isolated environment,
and therefore PEP 517 isn't being used.

To add this custom logic to `setup.py`, add a new argument to
`create_basic_sdist_for_package`. Note that to make this work, I had to
switch from f-strings to `str.format`, since the `dedent` has to happen
before formatting.
  • Loading branch information
SpecLad committed May 1, 2022
1 parent 573e1f6 commit 539a394
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
23 changes: 18 additions & 5 deletions tests/functional/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,12 +1178,25 @@ def test_download_use_pep517_propagation(
on the command line, but to their dependencies too.
"""

# Remove setuptools to ensure that metadata retrieval fails unless PEP 517
# is used.
script.pip("uninstall", "-y", "setuptools")

create_basic_sdist_for_package(script, "fake_proj", "1.0", depends=["fake_dep"])
create_basic_sdist_for_package(script, "fake_dep", "1.0")

# If --use-pep517 is in effect, then setup.py should be running in an isolated
# environment that doesn't have pip in it.
create_basic_sdist_for_package(
script,
"fake_dep",
"1.0",
setup_py_prelude=textwrap.dedent(
"""\
try:
import pip
except ImportError:
pass
else:
raise Exception(f"not running in isolation")
"""
),
)

download_dir = tmpdir / "download_dir"
script.pip(
Expand Down
24 changes: 15 additions & 9 deletions tests/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,12 +1189,16 @@ def create_basic_sdist_for_package(
fails_egg_info: bool = False,
fails_bdist_wheel: bool = False,
depends: Optional[List[str]] = None,
setup_py_prelude: str = "",
) -> Path:
files = {
"setup.py": f"""\
"setup.py": textwrap.dedent(
"""\
import sys
from setuptools import find_packages, setup
{setup_py_prelude}
fails_bdist_wheel = {fails_bdist_wheel!r}
fails_egg_info = {fails_egg_info!r}
Expand All @@ -1205,19 +1209,21 @@ def create_basic_sdist_for_package(
raise Exception("Simulated failure for building a wheel.")
setup(name={name!r}, version={version!r},
install_requires={depends or []!r})
""",
install_requires={depends!r})
"""
).format(
name=name,
version=version,
depends=depends or [],
setup_py_prelude=setup_py_prelude,
fails_bdist_wheel=fails_bdist_wheel,
fails_egg_info=fails_egg_info,
),
}

# Some useful shorthands
archive_name = f"{name}-{version}.tar.gz"

# Replace key-values with formatted values
for key, value in list(files.items()):
del files[key]
key = key.format(name=name)
files[key] = textwrap.dedent(value)

# Add new files after formatting
if extra_files:
files.update(extra_files)
Expand Down

0 comments on commit 539a394

Please sign in to comment.