Skip to content

Commit

Permalink
test_freeze_with_setuptools: use mocks
Browse files Browse the repository at this point in the history
This makes it possible to test both branches on any Python version.
  • Loading branch information
SpecLad committed Jul 6, 2023
1 parent 5dc65ea commit 393ccfb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
20 changes: 14 additions & 6 deletions src/pip/_internal/commands/freeze.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
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
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):
Expand Down Expand Up @@ -64,7 +72,7 @@ def add_options(self) -> None:
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(
Expand All @@ -80,7 +88,7 @@ def add_options(self) -> None:
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)
Expand Down
34 changes: 27 additions & 7 deletions tests/functional/test_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 393ccfb

Please sign in to comment.