Skip to content

Commit 28d7730

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 6987847 commit 28d7730

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
@@ -1181,12 +1181,25 @@ def test_download_use_pep517_propagation(
11811181
on the command line, but to their dependencies too.
11821182
"""
11831183

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

11911204
download_dir = tmpdir / "download_dir"
11921205
script.pip(

tests/lib/__init__.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,12 +1212,16 @@ def create_basic_sdist_for_package(
12121212
fails_egg_info: bool = False,
12131213
fails_bdist_wheel: bool = False,
12141214
depends: Optional[List[str]] = None,
1215+
setup_py_prelude: str = "",
12151216
) -> pathlib.Path:
12161217
files = {
1217-
"setup.py": f"""\
1218+
"setup.py": textwrap.dedent(
1219+
"""\
12181220
import sys
12191221
from setuptools import find_packages, setup
12201222
1223+
{setup_py_prelude}
1224+
12211225
fails_bdist_wheel = {fails_bdist_wheel!r}
12221226
fails_egg_info = {fails_egg_info!r}
12231227
@@ -1228,19 +1232,21 @@ def create_basic_sdist_for_package(
12281232
raise Exception("Simulated failure for building a wheel.")
12291233
12301234
setup(name={name!r}, version={version!r},
1231-
install_requires={depends or []!r})
1232-
""",
1235+
install_requires={depends!r})
1236+
"""
1237+
).format(
1238+
name=name,
1239+
version=version,
1240+
depends=depends or [],
1241+
setup_py_prelude=setup_py_prelude,
1242+
fails_bdist_wheel=fails_bdist_wheel,
1243+
fails_egg_info=fails_egg_info,
1244+
),
12331245
}
12341246

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

1238-
# Replace key-values with formatted values
1239-
for key, value in list(files.items()):
1240-
del files[key]
1241-
key = key.format(name=name)
1242-
files[key] = textwrap.dedent(value)
1243-
12441250
# Add new files after formatting
12451251
if extra_files:
12461252
files.update(extra_files)

0 commit comments

Comments
 (0)