Skip to content

Commit 539a394

Browse files
committed
test_download_use_pep517_propagation: use a different approach
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.
1 parent 573e1f6 commit 539a394

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

tests/functional/test_download.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,12 +1178,25 @@ def test_download_use_pep517_propagation(
11781178
on the command line, but to their dependencies too.
11791179
"""
11801180

1181-
# Remove setuptools to ensure that metadata retrieval fails unless PEP 517
1182-
# is used.
1183-
script.pip("uninstall", "-y", "setuptools")
1184-
11851181
create_basic_sdist_for_package(script, "fake_proj", "1.0", depends=["fake_dep"])
1186-
create_basic_sdist_for_package(script, "fake_dep", "1.0")
1182+
1183+
# If --use-pep517 is in effect, then setup.py should be running in an isolated
1184+
# environment that doesn't have pip in it.
1185+
create_basic_sdist_for_package(
1186+
script,
1187+
"fake_dep",
1188+
"1.0",
1189+
setup_py_prelude=textwrap.dedent(
1190+
"""\
1191+
try:
1192+
import pip
1193+
except ImportError:
1194+
pass
1195+
else:
1196+
raise Exception(f"not running in isolation")
1197+
"""
1198+
),
1199+
)
11871200

11881201
download_dir = tmpdir / "download_dir"
11891202
script.pip(

tests/lib/__init__.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,12 +1189,16 @@ def create_basic_sdist_for_package(
11891189
fails_egg_info: bool = False,
11901190
fails_bdist_wheel: bool = False,
11911191
depends: Optional[List[str]] = None,
1192+
setup_py_prelude: str = "",
11921193
) -> Path:
11931194
files = {
1194-
"setup.py": f"""\
1195+
"setup.py": textwrap.dedent(
1196+
"""\
11951197
import sys
11961198
from setuptools import find_packages, setup
11971199
1200+
{setup_py_prelude}
1201+
11981202
fails_bdist_wheel = {fails_bdist_wheel!r}
11991203
fails_egg_info = {fails_egg_info!r}
12001204
@@ -1205,19 +1209,21 @@ def create_basic_sdist_for_package(
12051209
raise Exception("Simulated failure for building a wheel.")
12061210
12071211
setup(name={name!r}, version={version!r},
1208-
install_requires={depends or []!r})
1209-
""",
1212+
install_requires={depends!r})
1213+
"""
1214+
).format(
1215+
name=name,
1216+
version=version,
1217+
depends=depends or [],
1218+
setup_py_prelude=setup_py_prelude,
1219+
fails_bdist_wheel=fails_bdist_wheel,
1220+
fails_egg_info=fails_egg_info,
1221+
),
12101222
}
12111223

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

1215-
# Replace key-values with formatted values
1216-
for key, value in list(files.items()):
1217-
del files[key]
1218-
key = key.format(name=name)
1219-
files[key] = textwrap.dedent(value)
1220-
12211227
# Add new files after formatting
12221228
if extra_files:
12231229
files.update(extra_files)

0 commit comments

Comments
 (0)