Skip to content

Commit

Permalink
compile: separately handle existing pins marked to-upgrade (-P), and …
Browse files Browse the repository at this point in the history
…exclude -P args not already pinned or primarily required; fixes #759

Co-Authored-By: Albert Tugushev <albert@tugushev.ru>

use long option style --upgrade-package in test

exact line-matching in expected output

more readable if/else instead of ternary
  • Loading branch information
AndydeCleyre committed Jan 19, 2020
1 parent 6709244 commit d1fb07e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
22 changes: 14 additions & 8 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ def cli(
key_from_req(install_req.req): install_req for install_req in upgrade_reqs_gen
}

existing_pins_to_upgrade = {}

# Proxy with a LocalRequirementsRepository if --upgrade is not specified
# (= default invocation)
if not upgrade and os.path.exists(output_file.name):
Expand All @@ -293,13 +295,14 @@ def cli(
)

# Exclude packages from --upgrade-package/-P from the existing
# constraints
existing_pins = {
key_from_req(ireq.req): ireq
for ireq in ireqs
if is_pinned_requirement(ireq)
and key_from_req(ireq.req) not in upgrade_install_reqs
}
# constraints, and separately gather pins to be upgraded
existing_pins = {}
for ireq in filter(is_pinned_requirement, ireqs):
key = key_from_req(ireq.req)
if key in upgrade_install_reqs:
existing_pins_to_upgrade[key] = ireq
else:
existing_pins[key] = ireq
repository = LocalRequirementsRepository(existing_pins, repository)

###
Expand Down Expand Up @@ -345,7 +348,10 @@ def cli(
key_from_ireq(ireq) for ireq in constraints if not ireq.constraint
}

constraints.extend(upgrade_install_reqs.values())
allowed_upgrades = primary_packages | set(existing_pins_to_upgrade)
constraints.extend(
ireq for key, ireq in upgrade_install_reqs.items() if key in allowed_upgrades
)

# Filter out pip environment markers which do not match (PEP496)
constraints = [
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,22 @@ def test_upgrade_packages_option(pip_conf, runner):
assert "small-fake-b==0.3" in out.stderr


def test_upgrade_packages_option_irrelevant(pip_conf, runner):
"""
piptools ignores --upgrade-package/-P items not already constrained.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a")
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1")

out = runner.invoke(cli, ["--upgrade-package", "small-fake-b"])

assert out.exit_code == 0
assert "small-fake-a==0.1" in out.stderr.splitlines()
assert "small-fake-b==0.3" not in out.stderr


def test_upgrade_packages_option_no_existing_file(pip_conf, runner):
"""
piptools respects --upgrade-package/-P inline list when the output file
Expand Down

0 comments on commit d1fb07e

Please sign in to comment.