test_freeze_with_setuptools: use mocks

This makes it possible to test both branches on any Python version.
This commit is contained in:
Роман Донченко 2023-05-29 02:23:01 +04:00
parent 5dc65eabb7
commit 393ccfbc31
2 changed files with 41 additions and 13 deletions

View File

@ -1,6 +1,6 @@
import sys
from optparse import Values
from typing import List
from typing import AbstractSet, List
from pip._internal.cli import cmdoptions
from pip._internal.cli.base_command import Command
@ -8,10 +8,18 @@ from pip._internal.cli.status_codes import SUCCESS
from pip._internal.operations.freeze import freeze
from pip._internal.utils.compat import stdlib_pkgs
DEV_PKGS = {"pip"}
if sys.version_info < (3, 12):
DEV_PKGS |= {"setuptools", "distribute", "wheel"}
def _should_suppress_build_backends() -> bool:
return sys.version_info < (3, 12)
def _dev_pkgs() -> AbstractSet[str]:
pkgs = {"pip"}
if _should_suppress_build_backends():
pkgs |= {"setuptools", "distribute", "wheel"}
return pkgs
class FreezeCommand(Command):
@ -64,7 +72,7 @@ class FreezeCommand(Command):
action="store_true",
help=(
"Do not skip these packages in the output:"
" {}".format(", ".join(DEV_PKGS))
" {}".format(", ".join(_dev_pkgs()))
),
)
self.cmd_opts.add_option(
@ -80,7 +88,7 @@ class FreezeCommand(Command):
def run(self, options: Values, args: List[str]) -> int:
skip = set(stdlib_pkgs)
if not options.freeze_all:
skip.update(DEV_PKGS)
skip.update(_dev_pkgs())
if options.excludes:
skip.update(options.excludes)

View File

@ -98,18 +98,38 @@ def test_freeze_with_pip(script: PipTestEnvironment) -> None:
def test_freeze_with_setuptools(script: PipTestEnvironment) -> None:
"""
Test that pip shows setuptools only when --all is used
or Python version is >=3.12
or _should_suppress_build_backends() returns false
"""
result = script.pip("freeze")
if sys.version_info >= (3, 12):
assert "setuptools==" in result.stdout
else:
assert "setuptools==" not in result.stdout
result = script.pip("freeze", "--all")
assert "setuptools==" in result.stdout
(script.site_packages_path / "mock.pth").write_text("import mock\n")
(script.site_packages_path / "mock.py").write_text(
textwrap.dedent(
"""\
import pip._internal.commands.freeze as freeze
freeze._should_suppress_build_backends = lambda: False
"""
)
)
result = script.pip("freeze")
assert "setuptools==" in result.stdout
(script.site_packages_path / "mock.py").write_text(
textwrap.dedent(
"""\
import pip._internal.commands.freeze as freeze
freeze._should_suppress_build_backends = lambda: True
"""
)
)
result = script.pip("freeze")
assert "setuptools==" not in result.stdout
def test_exclude_and_normalization(script: PipTestEnvironment, tmpdir: Path) -> None:
req_path = wheel.make_wheel(name="Normalizable_Name", version="1.0").save_to_dir(