Skip to content

Commit

Permalink
Add a --dry-run option to pip install
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed May 7, 2022
1 parent e5d17be commit 2cdca0b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/11096.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``--dry-run`` to pip install.
22 changes: 22 additions & 0 deletions src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ def add_options(self) -> None:
self.cmd_opts.add_option(cmdoptions.pre())

self.cmd_opts.add_option(cmdoptions.editable())
self.cmd_opts.add_option(
"--dry-run",
action="store_true",
dest="dry_run",
default=False,
help=(
"Don't actually install anything, just print what would be. "
"Can be used in combination with --ignore-installed "
"to 'resolve' the requirements."
),
)
self.cmd_opts.add_option(
"-t",
"--target",
Expand Down Expand Up @@ -341,6 +352,17 @@ def run(self, options: Values, args: List[str]) -> int:
reqs, check_supported_wheels=not options.target_dir
)

if options.dry_run:
items = [
f"{item.name}-{item.metadata['version']}"
for item in sorted(
requirement_set.all_requirements, key=lambda x: str(x.name)
)
]
if items:
write_output("Would install %s", " ".join(items))
return SUCCESS

try:
pip_req = requirement_set.get_requirement("pip")
except KeyError:
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2243,3 +2243,12 @@ def test_install_logs_pip_version_in_debug(
result = script.pip("install", "-v", fake_package)
pattern = "Using pip .* from .*"
assert_re_match(pattern, result.stdout)


def test_install_dry_run(script: PipTestEnvironment, data: TestData) -> None:
"""Test that pip install --dry-run logs what it would install."""
result = script.pip(
"install", "--dry-run", "--find-links", data.find_links, "simple"
)
assert "Would install simple-3.0" in result.stdout
assert "Successfully installed" not in result.stdout

0 comments on commit 2cdca0b

Please sign in to comment.