Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pip-compile -P PKGSPEC ignores pkgs not already parsed as constraints #1031

Merged
merged 4 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = set()

# 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.add(key)
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 | 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
69 changes: 64 additions & 5 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 All @@ -385,20 +401,30 @@ def test_upgrade_packages_option_no_existing_file(pip_conf, runner):
assert "small-fake-b==0.3" in out.stderr


def test_upgrade_packages_version_option(pip_conf, runner):
@pytest.mark.parametrize(
"current_package, upgraded_package",
(
pytest.param("small-fake-b==0.1", "small-fake-b==0.3", id="upgrade"),
pytest.param("small-fake-b==0.3", "small-fake-b==0.1", id="downgrade"),
),
)
def test_upgrade_packages_version_option(
pip_conf, runner, current_package, upgraded_package
):
"""
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")
req_in.write("small-fake-a==0.1\n" + current_package)

out = runner.invoke(cli, ["-P", "small-fake-b==0.2"])
out = runner.invoke(cli, ["--no-annotate", "--upgrade-package", upgraded_package])

assert out.exit_code == 0
assert "small-fake-a==0.1" in out.stderr
assert "small-fake-b==0.2" in out.stderr
stderr_lines = out.stderr.splitlines()
assert "small-fake-a==0.1" in stderr_lines
assert upgraded_package in stderr_lines


def test_upgrade_packages_version_option_no_existing_file(pip_conf, runner):
Expand Down Expand Up @@ -870,3 +896,36 @@ def test_options_in_requirements_file(runner, options):

with open("requirements.txt") as reqs_txt:
assert options in reqs_txt.read().splitlines()


@pytest.mark.parametrize(
"current_package, upgraded_package",
(
pytest.param("small-fake-b==0.1", "small-fake-b==0.2", id="upgrade"),
pytest.param("small-fake-b==0.2", "small-fake-b==0.1", id="downgrade"),
),
)
def test_upgrade_packages_option_subdependency(
pip_conf, runner, current_package, upgraded_package
):
"""
Test that pip-compile --upgrade-package/-P upgrades/dpwngrades subdependencies.
"""

with open("requirements.in", "w") as reqs:
reqs.write("small-fake-with-unpinned-deps\n")

with open("requirements.txt", "w") as reqs:
reqs.write("small-fake-a==0.1\n")
reqs.write(current_package + "\n")
reqs.write("small-fake-with-unpinned-deps==0.1\n")

out = runner.invoke(
cli, ["--no-annotate", "--dry-run", "--upgrade-package", upgraded_package]
)

stderr_lines = out.stderr.splitlines()
assert "small-fake-a==0.1" in stderr_lines, "small-fake-a must keep its version"
assert (
upgraded_package in stderr_lines
), "{} must be upgraded/downgraded to {}".format(current_package, upgraded_package)
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from setuptools import setup

setup(
name="small_fake_with_unpinned_deps",
version=0.1,
install_requires=["small-fake-a", "small-fake-b"],
)