Skip to content

Commit 7b6a81b

Browse files
committed
test_freeze_with_setuptools: use mocks
This makes it possible to test both branches on any Python version.
1 parent 5212021 commit 7b6a81b

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/pip/_internal/commands/freeze.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import sys
22
from optparse import Values
3-
from typing import List
3+
from typing import AbstractSet, List
44

55
from pip._internal.cli import cmdoptions
66
from pip._internal.cli.base_command import Command
77
from pip._internal.cli.status_codes import SUCCESS
88
from pip._internal.operations.freeze import freeze
99
from pip._internal.utils.compat import stdlib_pkgs
1010

11-
DEV_PKGS = {"pip"}
1211

13-
if sys.version_info < (3, 12):
14-
DEV_PKGS |= {"setuptools", "distribute", "wheel"}
12+
def _should_suppress_build_backends() -> bool:
13+
return sys.version_info < (3, 12)
14+
15+
16+
def _dev_pkgs() -> AbstractSet[str]:
17+
pkgs = {"pip"}
18+
19+
if _should_suppress_build_backends():
20+
pkgs |= {"setuptools", "distribute", "wheel"}
21+
22+
return pkgs
1523

1624

1725
class FreezeCommand(Command):
@@ -64,7 +72,7 @@ def add_options(self) -> None:
6472
action="store_true",
6573
help=(
6674
"Do not skip these packages in the output:"
67-
" {}".format(", ".join(DEV_PKGS))
75+
" {}".format(", ".join(_dev_pkgs()))
6876
),
6977
)
7078
self.cmd_opts.add_option(
@@ -80,7 +88,7 @@ def add_options(self) -> None:
8088
def run(self, options: Values, args: List[str]) -> int:
8189
skip = set(stdlib_pkgs)
8290
if not options.freeze_all:
83-
skip.update(DEV_PKGS)
91+
skip.update(_dev_pkgs())
8492

8593
if options.excludes:
8694
skip.update(options.excludes)

tests/functional/test_freeze.py

+27-7
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,38 @@ def test_freeze_with_pip(script: PipTestEnvironment) -> None:
9898
def test_freeze_with_setuptools(script: PipTestEnvironment) -> None:
9999
"""
100100
Test that pip shows setuptools only when --all is used
101-
or Python version is >=3.12
101+
or _should_suppress_build_backends() returns false
102102
"""
103103

104-
result = script.pip("freeze")
105-
if sys.version_info >= (3, 12):
106-
assert "setuptools==" in result.stdout
107-
else:
108-
assert "setuptools==" not in result.stdout
109-
110104
result = script.pip("freeze", "--all")
111105
assert "setuptools==" in result.stdout
112106

107+
(script.site_packages_path / "mock.pth").write_text("import mock\n")
108+
109+
(script.site_packages_path / "mock.py").write_text(
110+
textwrap.dedent(
111+
"""\
112+
import pip._internal.commands.freeze as freeze
113+
freeze._should_suppress_build_backends = lambda: False
114+
"""
115+
)
116+
)
117+
118+
result = script.pip("freeze")
119+
assert "setuptools==" in result.stdout
120+
121+
(script.site_packages_path / "mock.py").write_text(
122+
textwrap.dedent(
123+
"""\
124+
import pip._internal.commands.freeze as freeze
125+
freeze._should_suppress_build_backends = lambda: True
126+
"""
127+
)
128+
)
129+
130+
result = script.pip("freeze")
131+
assert "setuptools==" not in result.stdout
132+
113133

114134
def test_exclude_and_normalization(script: PipTestEnvironment, tmpdir: Path) -> None:
115135
req_path = wheel.make_wheel(name="Normalizable_Name", version="1.0").save_to_dir(

0 commit comments

Comments
 (0)