Skip to content

Commit 9a51fc8

Browse files
committed
Make the zipapp in a fixture
1 parent ef999f4 commit 9a51fc8

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

tests/conftest.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ def pytest_addoption(parser: Parser) -> None:
8686
)
8787
parser.addoption(
8888
"--use-zipapp",
89-
action="store",
90-
default=None,
91-
help="use given pip zipapp when running pip in tests",
89+
action="store_true",
90+
default=False,
91+
help="use a zipapp when running pip in tests",
9292
)
9393

9494

@@ -493,17 +493,17 @@ def with_wheel(virtualenv: VirtualEnvironment, wheel_install: Path) -> None:
493493

494494
class ScriptFactory(Protocol):
495495
def __call__(
496-
self, tmpdir: Path, virtualenv: Optional[VirtualEnvironment] = None, zipapp: Optional[str] = None
496+
self, tmpdir: Path, virtualenv: Optional[VirtualEnvironment] = None
497497
) -> PipTestEnvironment:
498498
...
499499

500500

501501
@pytest.fixture(scope="session")
502502
def script_factory(
503-
virtualenv_factory: Callable[[Path], VirtualEnvironment], deprecated_python: bool
503+
virtualenv_factory: Callable[[Path], VirtualEnvironment], deprecated_python: bool, zipapp: Optional[str]
504504
) -> ScriptFactory:
505505
def factory(
506-
tmpdir: Path, virtualenv: Optional[VirtualEnvironment] = None, zipapp: Optional[str] = None,
506+
tmpdir: Path, virtualenv: Optional[VirtualEnvironment] = None,
507507
) -> PipTestEnvironment:
508508
if virtualenv is None:
509509
virtualenv = virtualenv_factory(tmpdir.joinpath("venv"))
@@ -529,6 +529,29 @@ def factory(
529529
return factory
530530

531531

532+
@pytest.fixture(scope="session")
533+
def zipapp(request: pytest.FixtureRequest, tmpdir_factory: pytest.TempPathFactory) -> Optional[str]:
534+
"""
535+
If the user requested for pip to be run from a zipapp, build that zipapp
536+
and return its location. If the user didn't request a zipapp, return None.
537+
538+
This fixture is session scoped, so the zipapp will only be created once.
539+
"""
540+
if not request.config.getoption("--use-zipapp"):
541+
return None
542+
543+
temp_location = tmpdir_factory.mktemp("zipapp")
544+
pyz_file = temp_location / "pip.pyz"
545+
# What we want to do here is `pip wheel --wheel-dir temp_location <source_dir>`
546+
# and then build a zipapp from that wheel.
547+
# TODO: Remove hard coded file
548+
za = "pip-22.2.dev0.pyz"
549+
import warnings
550+
warnings.warn(f"Copying {za} to {pyz_file}")
551+
shutil.copyfile(za, pyz_file)
552+
return str(pyz_file)
553+
554+
532555
@pytest.fixture
533556
def script(
534557
request: pytest.FixtureRequest,
@@ -542,8 +565,7 @@ def script(
542565
test function. The returned object is a
543566
``tests.lib.PipTestEnvironment``.
544567
"""
545-
zipapp = request.config.getoption("--use-zipapp")
546-
return script_factory(tmpdir.joinpath("workspace"), virtualenv, zipapp)
568+
return script_factory(tmpdir.joinpath("workspace"), virtualenv)
547569

548570

549571
@pytest.fixture(scope="session")

0 commit comments

Comments
 (0)