Skip to content

Commit

Permalink
Always obey --upgrade-package, and allow it together with --upgrade
Browse files Browse the repository at this point in the history
Fixes jazzband#829. Fixes jazzband#830.

Allow the two options together, essentially making `--upgrade-package` a way of passing an extra constraint. Always parse `--upgrade-package`, regardless of whether the output file exists.
  • Loading branch information
adamchainz committed May 30, 2019
1 parent 4438d98 commit 1a78dd2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
21 changes: 8 additions & 13 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,6 @@ def cli(
# Close the file at the end of the context execution
ctx.call_on_close(safecall(output_file.close_intelligently))

if upgrade and upgrade_packages:
raise click.BadParameter(
"Only one of --upgrade or --upgrade-package can be provided as an argument."
)

###
# Setup
###
Expand Down Expand Up @@ -260,7 +255,12 @@ def cli(
session = pip_command._build_session(pip_options)
repository = PyPIRepository(pip_options, session, build_isolation)

upgrade_install_reqs = {}
# Parse all constraints coming from --upgrade-package/-P
upgrade_reqs_gen = (install_req_from_line(pkg) for pkg in upgrade_packages)
upgrade_install_reqs = {
key_from_req(install_req.req): install_req for install_req in upgrade_reqs_gen
}

# Proxy with a LocalRequirementsRepository if --upgrade is not specified
# (= default invocation)
if not upgrade and os.path.exists(output_file.name):
Expand All @@ -270,14 +270,9 @@ def cli(
session=repository.session,
options=pip_options,
)
# Exclude packages from --upgrade-package/-P from
# the existing pins: We want to upgrade.
upgrade_reqs_gen = (install_req_from_line(pkg) for pkg in upgrade_packages)
upgrade_install_reqs = {
key_from_req(install_req.req): install_req
for install_req in upgrade_reqs_gen
}

# Exclude packages from --upgrade-package/-P from the existing
# constraints
existing_pins = {
key_from_req(ireq.req): ireq
for ireq in ireqs
Expand Down
59 changes: 35 additions & 24 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,38 +362,65 @@ def test_input_file_without_extension(runner):
assert os.path.exists("requirements.txt")


def test_upgrade_packages_option(runner):
@pytest.mark.parametrize("existing_requirements_txt", [True, False])
def test_upgrade_packages_option(runner, existing_requirements_txt):
"""
piptools respects --upgrade-package/-P inline list.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\nsmall-fake-b")
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1\nsmall-fake-b==0.1")
if existing_requirements_txt:
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1\nsmall-fake-b==0.1")

out = runner.invoke(cli, ["-P", "small-fake-b", "-f", MINIMAL_WHEELS_PATH])

assert out.exit_code == 0
assert "small-fake-a==0.1" in out.output
if existing_requirements_txt:
assert "small-fake-a==0.1" in out.output
assert "small-fake-b==0.3" in out.output


def test_upgrade_packages_version_option(runner):
@pytest.mark.parametrize("existing_requirements_txt", [True, False])
def test_upgrade_packages_version_option(runner, existing_requirements_txt):
"""
piptools respects --upgrade-package/-P inline list with specified versions.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\nsmall-fake-b")
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1\nsmall-fake-b==0.1")
if existing_requirements_txt:
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1\nsmall-fake-b==0.1")

out = runner.invoke(cli, ["-P", "small-fake-b==0.2", "-f", MINIMAL_WHEELS_PATH])

assert out.exit_code == 0
assert "small-fake-a==0.1" in out.output
if existing_requirements_txt:
assert "small-fake-a==0.1" in out.output
assert "small-fake-b==0.2" in out.output


@pytest.mark.parametrize("existing_requirements_txt", [True, False])
def test_upgrade_packages_version_option_and_upgrade(runner, existing_requirements_txt):
"""
piptools respects --upgrade-package/-P inline list with specified versions
whilst also doing --upgrade.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\nsmall-fake-b")
if existing_requirements_txt:
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1\nsmall-fake-b==0.1")

out = runner.invoke(
cli, ["--upgrade", "-P", "small-fake-b==0.1", "-f", MINIMAL_WHEELS_PATH]
)

assert out.exit_code == 0
assert "small-fake-a==0.2" in out.output
assert "small-fake-b==0.1" in out.output


def test_quiet_option(runner):
with open("requirements", "w"):
pass
Expand Down Expand Up @@ -554,22 +581,6 @@ def test_multiple_input_files_without_output_file(runner):
assert out.exit_code == 2


def test_mutually_exclusive_upgrade_options(runner):
"""
The options --upgrade and --upgrade-package should be mutual exclusive.
"""
with open("requirements.in", "w") as req_in:
req_in.write("six==1.10.0")

out = runner.invoke(cli, ["--upgrade", "--upgrade-package", "six"])

assert (
"Only one of --upgrade or --upgrade-package can be provided as an argument"
in out.output
)
assert out.exit_code == 2


@pytest.mark.parametrize(
"option, expected",
[
Expand Down

0 comments on commit 1a78dd2

Please sign in to comment.