test_new_resolver.py 57.1 KB
Newer Older
T
Tzu-ping Chung 已提交
1
import os
2
import pathlib
3
import sys
4
import textwrap
T
Tzu-ping Chung 已提交
5

6 7
import pytest

P
Paul Moore 已提交
8 9 10
from tests.lib import (
    create_basic_sdist_for_package,
    create_basic_wheel_for_package,
T
Tzu-ping Chung 已提交
11
    create_test_package_with_setup,
12
    path_to_url,
P
Paul Moore 已提交
13
)
T
Tzu-ping Chung 已提交
14
from tests.lib.direct_url import get_created_direct_url
15
from tests.lib.path import Path
16
from tests.lib.wheel import make_wheel
17 18


T
Tzu-ping Chung 已提交
19 20 21 22
def assert_editable(script, *args):
    # This simply checks whether all of the listed packages have a
    # corresponding .egg-link file installed.
    # TODO: Implement a more rigorous way to test for editable installations.
A
Andrey Bienkowski 已提交
23
    egg_links = {f"{arg}.egg-link" for arg in args}
24 25 26
    assert egg_links <= set(
        os.listdir(script.site_packages_path)
    ), f"{args!r} not all found in {script.site_packages_path!r}"
T
Tzu-ping Chung 已提交
27 28


29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
@pytest.fixture()
def make_fake_wheel(script):
    def _make_fake_wheel(name, version, wheel_tag):
        wheel_house = script.scratch_path.joinpath("wheelhouse")
        wheel_house.mkdir()
        wheel_builder = make_wheel(
            name=name,
            version=version,
            wheel_metadata_updates={"Tag": []},
        )
        wheel_path = wheel_house.joinpath(f"{name}-{version}-{wheel_tag}.whl")
        wheel_builder.save_to(wheel_path)
        return wheel_path

    return _make_fake_wheel


46
def test_new_resolver_can_install(script):
47
    create_basic_wheel_for_package(
48 49 50 51 52
        script,
        "simple",
        "0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
53
        "install",
54 55 56 57 58
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
59
    )
60
    script.assert_installed(simple="0.1.0")
61 62 63


def test_new_resolver_can_install_with_version(script):
64
    create_basic_wheel_for_package(
65 66 67 68 69
        script,
        "simple",
        "0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
70
        "install",
71 72 73 74 75
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple==0.1.0",
76
    )
77
    script.assert_installed(simple="0.1.0")
78 79 80


def test_new_resolver_picks_latest_version(script):
81
    create_basic_wheel_for_package(
82 83 84 85
        script,
        "simple",
        "0.1.0",
    )
86
    create_basic_wheel_for_package(
87 88 89 90 91
        script,
        "simple",
        "0.2.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
92
        "install",
93 94 95 96 97
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
98
    )
99
    script.assert_installed(simple="0.2.0")
100

101

102 103 104 105 106 107 108 109 110 111 112 113
def test_new_resolver_picks_installed_version(script):
    create_basic_wheel_for_package(
        script,
        "simple",
        "0.1.0",
    )
    create_basic_wheel_for_package(
        script,
        "simple",
        "0.2.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
114
        "install",
115 116 117 118 119
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple==0.1.0",
120
    )
121
    script.assert_installed(simple="0.1.0")
122 123

    result = script.pip(
P
Pradyun Gedam 已提交
124
        "install",
125 126 127 128 129
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
130 131
    )
    assert "Collecting" not in result.stdout, "Should not fetch new version"
132
    script.assert_installed(simple="0.1.0")
133 134 135 136 137 138 139 140 141 142 143 144 145 146


def test_new_resolver_picks_installed_version_if_no_match_found(script):
    create_basic_wheel_for_package(
        script,
        "simple",
        "0.1.0",
    )
    create_basic_wheel_for_package(
        script,
        "simple",
        "0.2.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
147
        "install",
148 149 150 151 152
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple==0.1.0",
153
    )
154
    script.assert_installed(simple="0.1.0")
155

156
    result = script.pip("install", "--no-cache-dir", "--no-index", "simple")
157
    assert "Collecting" not in result.stdout, "Should not fetch new version"
158
    script.assert_installed(simple="0.1.0")
159 160


161
def test_new_resolver_installs_dependencies(script):
162
    create_basic_wheel_for_package(
163 164 165 166 167
        script,
        "base",
        "0.1.0",
        depends=["dep"],
    )
168
    create_basic_wheel_for_package(
169 170 171 172 173
        script,
        "dep",
        "0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
174
        "install",
175 176 177 178 179
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "base",
180
    )
181
    script.assert_installed(base="0.1.0", dep="0.1.0")
182 183


184 185 186 187 188 189 190 191 192 193 194 195 196
def test_new_resolver_ignore_dependencies(script):
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        depends=["dep"],
    )
    create_basic_wheel_for_package(
        script,
        "dep",
        "0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
197
        "install",
198 199 200 201 202 203
        "--no-cache-dir",
        "--no-index",
        "--no-deps",
        "--find-links",
        script.scratch_path,
        "base",
204
    )
205 206
    script.assert_installed(base="0.1.0")
    script.assert_not_installed("dep")
207 208


209 210 211 212 213 214 215
@pytest.mark.parametrize(
    "root_dep",
    [
        "base[add]",
        "base[add] >= 0.1.0",
    ],
)
216 217 218 219
def test_new_resolver_installs_extras(tmpdir, script, root_dep):
    req_file = tmpdir.joinpath("requirements.txt")
    req_file.write_text(root_dep)

220 221 222 223 224 225 226 227 228 229 230 231
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        extras={"add": ["dep"]},
    )
    create_basic_wheel_for_package(
        script,
        "dep",
        "0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
232
        "install",
233 234 235 236 237 238
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-r",
        req_file,
239
    )
240
    script.assert_installed(base="0.1.0", dep="0.1.0")
241 242 243


def test_new_resolver_installs_extras_warn_missing(script):
244 245 246 247 248 249 250 251 252 253 254
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        extras={"add": ["dep"]},
    )
    create_basic_wheel_for_package(
        script,
        "dep",
        "0.1.0",
    )
P
Paul Moore 已提交
255
    result = script.pip(
P
Pradyun Gedam 已提交
256
        "install",
257 258 259 260
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
P
Paul Moore 已提交
261 262
        "base[add,missing]",
        expect_stderr=True,
263
    )
264 265
    assert "does not provide the extra" in result.stderr, str(result)
    assert "missing" in result.stderr, str(result)
266
    script.assert_installed(base="0.1.0", dep="0.1.0")
267 268


269 270 271
def test_new_resolver_installed_message(script):
    create_basic_wheel_for_package(script, "A", "1.0")
    result = script.pip(
P
Pradyun Gedam 已提交
272
        "install",
273 274 275 276
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
277 278 279 280 281 282 283 284 285
        "A",
        expect_stderr=False,
    )
    assert "Successfully installed A-1.0" in result.stdout, str(result)


def test_new_resolver_no_dist_message(script):
    create_basic_wheel_for_package(script, "A", "1.0")
    result = script.pip(
P
Pradyun Gedam 已提交
286
        "install",
287 288 289 290
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
291 292 293 294 295 296 297 298 299 300
        "B",
        expect_error=True,
        expect_stderr=True,
    )

    # Full messages from old resolver:
    # ERROR: Could not find a version that satisfies the
    #        requirement xxx (from versions: none)
    # ERROR: No matching distribution found for xxx

301 302 303
    assert (
        "Could not find a version that satisfies the requirement B" in result.stderr
    ), str(result)
304
    assert "No matching distribution found for B" in result.stderr, str(result)
305 306


T
Tzu-ping Chung 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319
def test_new_resolver_installs_editable(script):
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        depends=["dep"],
    )
    source_dir = create_test_package_with_setup(
        script,
        name="dep",
        version="0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
320
        "install",
321 322 323 324
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
325
        "base",
326 327
        "--editable",
        source_dir,
T
Tzu-ping Chung 已提交
328
    )
329
    script.assert_installed(base="0.1.0", dep="0.1.0")
T
Tzu-ping Chung 已提交
330 331 332
    assert_editable(script, "dep")


333 334 335 336 337 338
@pytest.mark.parametrize(
    "requires_python, ignore_requires_python, dep_version",
    [
        # Something impossible to satisfy.
        ("<2", False, "0.1.0"),
        ("<2", True, "0.2.0"),
T
Tzu-ping Chung 已提交
339
        # Something guaranteed to satisfy.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        (">=2", False, "0.2.0"),
        (">=2", True, "0.2.0"),
    ],
)
def test_new_resolver_requires_python(
    script,
    requires_python,
    ignore_requires_python,
    dep_version,
):
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        depends=["dep"],
    )
356 357
    create_basic_wheel_for_package(
        script,
358 359
        "dep",
        "0.1.0",
360 361 362
    )
    create_basic_wheel_for_package(
        script,
363 364
        "dep",
        "0.2.0",
365 366
        requires_python=requires_python,
    )
367 368 369 370 371

    args = [
        "install",
        "--no-cache-dir",
        "--no-index",
372 373
        "--find-links",
        script.scratch_path,
374 375 376 377 378 379 380
    ]
    if ignore_requires_python:
        args.append("--ignore-requires-python")
    args.append("base")

    script.pip(*args)

381
    script.assert_installed(base="0.1.0", dep=dep_version)
382 383


384 385 386 387 388 389 390 391
def test_new_resolver_requires_python_error(script):
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        requires_python="<2",
    )
    result = script.pip(
P
Pradyun Gedam 已提交
392
        "install",
393 394 395 396
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
397 398 399 400 401 402 403 404 405 406 407
        "base",
        expect_error=True,
    )

    message = (
        "Package 'base' requires a different Python: "
        "{}.{}.{} not in '<2'".format(*sys.version_info[:3])
    )
    assert message in result.stderr, str(result)


408 409 410 411 412 413 414 415 416 417 418 419 420 421
def test_new_resolver_installed(script):
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        depends=["dep"],
    )
    create_basic_wheel_for_package(
        script,
        "dep",
        "0.1.0",
    )

    result = script.pip(
P
Pradyun Gedam 已提交
422
        "install",
423 424 425 426
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
427 428
        "base",
    )
429
    assert "Requirement already satisfied" not in result.stdout, str(result)
430 431

    result = script.pip(
P
Pradyun Gedam 已提交
432
        "install",
433 434 435 436
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
437
        "base~=0.1.0",
438
    )
439
    assert "Requirement already satisfied: base~=0.1.0" in result.stdout, str(result)
440
    result.did_not_update(
441
        script.site_packages / "base", message="base 0.1.0 reinstalled"
442 443 444 445 446 447 448 449 450
    )


def test_new_resolver_ignore_installed(script):
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
    )
451
    satisfied_output = "Requirement already satisfied"
452 453

    result = script.pip(
P
Pradyun Gedam 已提交
454
        "install",
455 456 457 458
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
459 460 461 462 463
        "base",
    )
    assert satisfied_output not in result.stdout, str(result)

    result = script.pip(
P
Pradyun Gedam 已提交
464
        "install",
465 466 467 468 469
        "--no-cache-dir",
        "--no-index",
        "--ignore-installed",
        "--find-links",
        script.scratch_path,
470 471 472
        "base",
    )
    assert satisfied_output not in result.stdout, str(result)
473
    result.did_update(
474
        script.site_packages / "base", message="base 0.1.0 not reinstalled"
475
    )
P
Paul Moore 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498


def test_new_resolver_only_builds_sdists_when_needed(script):
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        depends=["dep"],
    )
    create_basic_sdist_for_package(
        script,
        "dep",
        "0.1.0",
        # Replace setup.py with something that fails
        extra_files={"setup.py": "assert False"},
    )
    create_basic_sdist_for_package(
        script,
        "dep",
        "0.2.0",
    )
    # We only ever need to check dep 0.2.0 as it's the latest version
    script.pip(
P
Pradyun Gedam 已提交
499
        "install",
500 501 502 503 504
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "base",
P
Paul Moore 已提交
505
    )
506
    script.assert_installed(base="0.1.0", dep="0.2.0")
P
Paul Moore 已提交
507 508 509

    # We merge criteria here, as we have two "dep" requirements
    script.pip(
P
Pradyun Gedam 已提交
510
        "install",
511 512 513 514 515 516
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "base",
        "dep",
P
Paul Moore 已提交
517
    )
518
    script.assert_installed(base="0.1.0", dep="0.2.0")
519 520 521 522 523 524 525


def test_new_resolver_install_different_version(script):
    create_basic_wheel_for_package(script, "base", "0.1.0")
    create_basic_wheel_for_package(script, "base", "0.2.0")

    script.pip(
P
Pradyun Gedam 已提交
526
        "install",
527 528 529 530
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
531 532 533 534 535
        "base==0.1.0",
    )

    # This should trigger an uninstallation of base.
    result = script.pip(
P
Pradyun Gedam 已提交
536
        "install",
537 538 539 540
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
541 542 543 544 545
        "base==0.2.0",
    )

    assert "Uninstalling base-0.1.0" in result.stdout, str(result)
    assert "Successfully uninstalled base-0.1.0" in result.stdout, str(result)
546
    result.did_update(script.site_packages / "base", message="base not upgraded")
547
    script.assert_installed(base="0.2.0")
548 549 550 551 552 553


def test_new_resolver_force_reinstall(script):
    create_basic_wheel_for_package(script, "base", "0.1.0")

    script.pip(
P
Pradyun Gedam 已提交
554
        "install",
555 556 557 558
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
559
        "base==0.1.0",
560 561
    )

562 563
    # This should trigger an uninstallation of base due to --force-reinstall,
    # even though the installed version matches.
564
    result = script.pip(
P
Pradyun Gedam 已提交
565
        "install",
566 567 568 569
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
570
        "--force-reinstall",
571
        "base==0.1.0",
572 573
    )

574 575
    assert "Uninstalling base-0.1.0" in result.stdout, str(result)
    assert "Successfully uninstalled base-0.1.0" in result.stdout, str(result)
576
    result.did_update(script.site_packages / "base", message="base not reinstalled")
577
    script.assert_installed(base="0.1.0")
T
Tzu-ping Chung 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602


@pytest.mark.parametrize(
    "available_versions, pip_args, expected_version",
    [
        # Choose the latest non-prerelease by default.
        (["1.0", "2.0a1"], ["pkg"], "1.0"),
        # Choose the prerelease if the specifier spells out a prerelease.
        (["1.0", "2.0a1"], ["pkg==2.0a1"], "2.0a1"),
        # Choose the prerelease if explicitly allowed by the user.
        (["1.0", "2.0a1"], ["pkg", "--pre"], "2.0a1"),
        # Choose the prerelease if no stable releases are available.
        (["2.0a1"], ["pkg"], "2.0a1"),
    ],
    ids=["default", "exact-pre", "explicit-pre", "no-stable"],
)
def test_new_resolver_handles_prerelease(
    script,
    available_versions,
    pip_args,
    expected_version,
):
    for version in available_versions:
        create_basic_wheel_for_package(script, "pkg", version)
    script.pip(
P
Pradyun Gedam 已提交
603
        "install",
604 605 606 607 608
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        *pip_args,
T
Tzu-ping Chung 已提交
609
    )
610
    script.assert_installed(pkg=expected_version)
P
Paul Moore 已提交
611 612


613 614 615 616 617 618 619
@pytest.mark.parametrize(
    "pkg_deps, root_deps",
    [
        # This tests the marker is picked up from a transitive dependency.
        (["dep; os_name == 'nonexist_os'"], ["pkg"]),
        # This tests the marker is picked up from a root dependency.
        ([], ["pkg", "dep; os_name == 'nonexist_os'"]),
620
    ],
621
)
A
Alex Hedges 已提交
622
def test_new_resolver_skips_marker(script, pkg_deps, root_deps):
623 624 625 626
    create_basic_wheel_for_package(script, "pkg", "1.0", depends=pkg_deps)
    create_basic_wheel_for_package(script, "dep", "1.0")

    script.pip(
P
Pradyun Gedam 已提交
627
        "install",
628 629 630 631 632
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        *root_deps,
633
    )
634 635
    script.assert_installed(pkg="1.0")
    script.assert_not_installed("dep")
636 637


T
Tzu-ping Chung 已提交
638 639 640 641 642 643 644
@pytest.mark.parametrize(
    "constraints",
    [
        ["pkg<2.0", "constraint_only<1.0"],
        # This also tests the pkg constraint don't get merged with the
        # requirement prematurely. (pypa/pip#8134)
        ["pkg<2.0"],
645
    ],
T
Tzu-ping Chung 已提交
646 647
)
def test_new_resolver_constraints(script, constraints):
P
Paul Moore 已提交
648 649 650 651
    create_basic_wheel_for_package(script, "pkg", "1.0")
    create_basic_wheel_for_package(script, "pkg", "2.0")
    create_basic_wheel_for_package(script, "pkg", "3.0")
    constraints_file = script.scratch_path / "constraints.txt"
T
Tzu-ping Chung 已提交
652
    constraints_file.write_text("\n".join(constraints))
P
Paul Moore 已提交
653
    script.pip(
P
Pradyun Gedam 已提交
654
        "install",
655 656 657 658 659 660 661
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
        "pkg",
P
Paul Moore 已提交
662
    )
663 664
    script.assert_installed(pkg="1.0")
    script.assert_not_installed("constraint_only")
P
Paul Moore 已提交
665 666


667 668 669 670 671 672
def test_new_resolver_constraint_no_specifier(script):
    "It's allowed (but useless...) for a constraint to have no specifier"
    create_basic_wheel_for_package(script, "pkg", "1.0")
    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("pkg")
    script.pip(
P
Pradyun Gedam 已提交
673
        "install",
674 675 676 677 678 679 680
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
        "pkg",
681
    )
682
    script.assert_installed(pkg="1.0")
683 684 685 686 687 688 689 690 691 692


@pytest.mark.parametrize(
    "constraint, error",
    [
        (
            "dist.zip",
            "Unnamed requirements are not allowed as constraints",
        ),
        (
693 694
            "-e git+https://example.com/dist.git#egg=req",
            "Editable requirements are not allowed as constraints",
695 696 697 698 699 700 701 702 703 704 705 706
        ),
        (
            "pkg[extra]",
            "Constraints cannot have extras",
        ),
    ],
)
def test_new_resolver_constraint_reject_invalid(script, constraint, error):
    create_basic_wheel_for_package(script, "pkg", "1.0")
    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text(constraint)
    result = script.pip(
P
Pradyun Gedam 已提交
707
        "install",
708 709 710 711 712 713
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
714 715 716 717 718 719 720
        "pkg",
        expect_error=True,
        expect_stderr=True,
    )
    assert error in result.stderr, str(result)


P
Paul Moore 已提交
721 722 723
def test_new_resolver_constraint_on_dependency(script):
    create_basic_wheel_for_package(script, "base", "1.0", depends=["dep"])
    create_basic_wheel_for_package(script, "dep", "1.0")
P
Paul Moore 已提交
724 725
    create_basic_wheel_for_package(script, "dep", "2.0")
    create_basic_wheel_for_package(script, "dep", "3.0")
P
Paul Moore 已提交
726
    constraints_file = script.scratch_path / "constraints.txt"
P
Paul Moore 已提交
727
    constraints_file.write_text("dep==2.0")
P
Paul Moore 已提交
728
    script.pip(
P
Pradyun Gedam 已提交
729
        "install",
730 731 732 733 734 735 736
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
        "base",
P
Paul Moore 已提交
737
    )
738 739
    script.assert_installed(base="1.0")
    script.assert_installed(dep="2.0")
P
Paul Moore 已提交
740 741


742 743 744
@pytest.mark.parametrize(
    "constraint_version, expect_error, message",
    [
745
        ("1.0", True, "Cannot install foo 2.0"),
746 747 748 749 750 751 752 753 754
        ("2.0", False, "Successfully installed foo-2.0"),
    ],
)
def test_new_resolver_constraint_on_path_empty(
    script,
    constraint_version,
    expect_error,
    message,
):
755
    """A path requirement can be filtered by a constraint."""
P
Paul Moore 已提交
756 757 758
    setup_py = script.scratch_path / "setup.py"
    text = "from setuptools import setup\nsetup(name='foo', version='2.0')"
    setup_py.write_text(text)
759

P
Paul Moore 已提交
760
    constraints_txt = script.scratch_path / "constraints.txt"
761
    constraints_txt.write_text(f"foo=={constraint_version}")
762

P
Paul Moore 已提交
763
    result = script.pip(
P
Pradyun Gedam 已提交
764
        "install",
765 766 767 768
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_txt,
P
Paul Moore 已提交
769
        str(script.scratch_path),
770
        expect_error=expect_error,
P
Paul Moore 已提交
771 772
    )

773 774 775 776
    if expect_error:
        assert message in result.stderr, str(result)
    else:
        assert message in result.stdout, str(result)
777 778


779 780 781 782 783
def test_new_resolver_constraint_only_marker_match(script):
    create_basic_wheel_for_package(script, "pkg", "1.0")
    create_basic_wheel_for_package(script, "pkg", "2.0")
    create_basic_wheel_for_package(script, "pkg", "3.0")

A
Alex Hedges 已提交
784
    constraints_content = textwrap.dedent(
785 786 787 788 789 790
        """
        pkg==1.0; python_version == "{ver[0]}.{ver[1]}"  # Always satisfies.
        pkg==2.0; python_version < "0"  # Never satisfies.
        """
    ).format(ver=sys.version_info)
    constraints_txt = script.scratch_path / "constraints.txt"
A
Alex Hedges 已提交
791
    constraints_txt.write_text(constraints_content)
792 793

    script.pip(
P
Pradyun Gedam 已提交
794
        "install",
795 796 797 798 799 800
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_txt,
        "--find-links",
        script.scratch_path,
801 802
        "pkg",
    )
803
    script.assert_installed(pkg="1.0")
804 805


806 807 808 809
def test_new_resolver_upgrade_needs_option(script):
    # Install pkg 1.0.0
    create_basic_wheel_for_package(script, "pkg", "1.0.0")
    script.pip(
P
Pradyun Gedam 已提交
810
        "install",
811 812 813 814
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
815 816 817 818 819 820 821 822
        "pkg",
    )

    # Now release a new version
    create_basic_wheel_for_package(script, "pkg", "2.0.0")

    # This should not upgrade because we don't specify --upgrade
    result = script.pip(
P
Pradyun Gedam 已提交
823
        "install",
824 825 826 827
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
828 829 830 831
        "pkg",
    )

    assert "Requirement already satisfied" in result.stdout, str(result)
832
    script.assert_installed(pkg="1.0.0")
833 834 835

    # This should upgrade
    result = script.pip(
P
Pradyun Gedam 已提交
836
        "install",
837 838 839 840
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
841
        "--upgrade",
842
        "PKG",  # Deliberately uppercase to check canonicalization
843 844 845 846
    )

    assert "Uninstalling pkg-1.0.0" in result.stdout, str(result)
    assert "Successfully uninstalled pkg-1.0.0" in result.stdout, str(result)
847
    result.did_update(script.site_packages / "pkg", message="pkg not upgraded")
848
    script.assert_installed(pkg="2.0.0")
849 850 851 852 853 854


def test_new_resolver_upgrade_strategy(script):
    create_basic_wheel_for_package(script, "base", "1.0.0", depends=["dep"])
    create_basic_wheel_for_package(script, "dep", "1.0.0")
    script.pip(
P
Pradyun Gedam 已提交
855
        "install",
856 857 858 859
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
860 861 862
        "base",
    )

863 864
    script.assert_installed(base="1.0.0")
    script.assert_installed(dep="1.0.0")
865 866 867 868 869 870

    # Now release new versions
    create_basic_wheel_for_package(script, "base", "2.0.0", depends=["dep"])
    create_basic_wheel_for_package(script, "dep", "2.0.0")

    script.pip(
P
Pradyun Gedam 已提交
871
        "install",
872 873 874 875
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
876 877 878 879 880 881
        "--upgrade",
        "base",
    )

    # With upgrade strategy "only-if-needed" (the default), dep should not
    # be upgraded.
882 883
    script.assert_installed(base="2.0.0")
    script.assert_installed(dep="1.0.0")
884 885 886

    create_basic_wheel_for_package(script, "base", "3.0.0", depends=["dep"])
    script.pip(
P
Pradyun Gedam 已提交
887
        "install",
888 889 890 891 892 893
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "--upgrade",
        "--upgrade-strategy=eager",
894 895 896 897
        "base",
    )

    # With upgrade strategy "eager", dep should be upgraded.
898 899
    script.assert_installed(base="3.0.0")
    script.assert_installed(dep="2.0.0")
900 901


902
class TestExtraMerge:
903 904 905 906 907 908
    """
    Test installing a package that depends the same package with different
    extras, one listed as required and the other as in extra.
    """

    def _local_with_setup(script, name, version, requires, extras):
909
        """Create the package as a local source directory to install from path."""
910 911 912 913 914 915 916 917 918
        return create_test_package_with_setup(
            script,
            name=name,
            version=version,
            install_requires=requires,
            extras_require=extras,
        )

    def _direct_wheel(script, name, version, requires, extras):
919
        """Create the package as a wheel to install from path directly."""
920 921 922 923 924 925 926 927 928
        return create_basic_wheel_for_package(
            script,
            name=name,
            version=version,
            depends=requires,
            extras=extras,
        )

    def _wheel_from_index(script, name, version, requires, extras):
929
        """Create the package as a wheel to install from index."""
930 931 932 933 934 935 936 937 938 939 940
        create_basic_wheel_for_package(
            script,
            name=name,
            version=version,
            depends=requires,
            extras=extras,
        )
        return name

    @pytest.mark.parametrize(
        "pkg_builder",
T
Tzu-ping Chung 已提交
941
        [
P
Pradyun Gedam 已提交
942
            _local_with_setup,
T
Tzu-ping Chung 已提交
943 944 945
            _direct_wheel,
            _wheel_from_index,
        ],
946
    )
947
    def test_new_resolver_extra_merge_in_package(self, script, pkg_builder):
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
        create_basic_wheel_for_package(script, "depdev", "1.0.0")
        create_basic_wheel_for_package(
            script,
            "dep",
            "1.0.0",
            extras={"dev": ["depdev"]},
        )
        requirement = pkg_builder(
            script,
            name="pkg",
            version="1.0.0",
            requires=["dep"],
            extras={"dev": ["dep[dev]"]},
        )

        script.pip(
P
Pradyun Gedam 已提交
964
            "install",
965 966 967 968
            "--no-cache-dir",
            "--no-index",
            "--find-links",
            script.scratch_path,
969 970
            requirement + "[dev]",
        )
971
        script.assert_installed(pkg="1.0.0", dep="1.0.0", depdev="1.0.0")
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995


def test_new_resolver_build_directory_error_zazo_19(script):
    """https://github.com/pradyunsg/zazo/issues/19#issuecomment-631615674

    This will first resolve like this:

    1. Pin pkg-b==2.0.0 (since pkg-b has fewer choices)
    2. Pin pkg-a==3.0.0 -> Conflict due to dependency pkg-b<2
    3. Pin pkg-b==1.0.0

    Since pkg-b is only available as sdist, both the first and third steps
    would trigger building from source. This ensures the preparer can build
    different versions of a package for the resolver.

    The preparer would fail with the following message if the different
    versions end up using the same build directory::

        ERROR: pip can't proceed with requirements 'pkg-b ...' due to a
        pre-existing build directory (...). This is likely due to a previous
        installation that failed. pip is being responsible and not assuming it
        can delete this. Please delete it and try again.
    """
    create_basic_wheel_for_package(
996 997 998 999
        script,
        "pkg_a",
        "3.0.0",
        depends=["pkg-b<2"],
1000 1001 1002 1003 1004 1005 1006 1007
    )
    create_basic_wheel_for_package(script, "pkg_a", "2.0.0")
    create_basic_wheel_for_package(script, "pkg_a", "1.0.0")

    create_basic_sdist_for_package(script, "pkg_b", "2.0.0")
    create_basic_sdist_for_package(script, "pkg_b", "1.0.0")

    script.pip(
P
Pradyun Gedam 已提交
1008
        "install",
1009 1010 1011 1012 1013 1014
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "pkg-a",
        "pkg-b",
1015
    )
1016
    script.assert_installed(pkg_a="3.0.0", pkg_b="1.0.0")
T
Tzu-ping Chung 已提交
1017 1018 1019 1020 1021 1022 1023


def test_new_resolver_upgrade_same_version(script):
    create_basic_wheel_for_package(script, "pkg", "2")
    create_basic_wheel_for_package(script, "pkg", "1")

    script.pip(
P
Pradyun Gedam 已提交
1024
        "install",
1025 1026 1027 1028
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1029 1030
        "pkg",
    )
1031
    script.assert_installed(pkg="2")
T
Tzu-ping Chung 已提交
1032 1033

    script.pip(
P
Pradyun Gedam 已提交
1034
        "install",
1035 1036 1037 1038
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1039 1040 1041
        "--upgrade",
        "pkg",
    )
1042
    script.assert_installed(pkg="2")
1043 1044 1045 1046 1047 1048 1049 1050 1051


def test_new_resolver_local_and_req(script):
    source_dir = create_test_package_with_setup(
        script,
        name="pkg",
        version="0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
1052
        "install",
1053 1054 1055 1056
        "--no-cache-dir",
        "--no-index",
        source_dir,
        "pkg!=0.1.0",
1057 1058
        expect_error=True,
    )
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079


def test_new_resolver_no_deps_checks_requires_python(script):
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        depends=["dep"],
        requires_python="<2",  # Something that always fails.
    )
    create_basic_wheel_for_package(
        script,
        "dep",
        "0.2.0",
    )

    result = script.pip(
        "install",
        "--no-cache-dir",
        "--no-index",
        "--no-deps",
1080 1081
        "--find-links",
        script.scratch_path,
1082 1083 1084 1085 1086 1087 1088 1089 1090
        "base",
        expect_error=True,
    )

    message = (
        "Package 'base' requires a different Python: "
        "{}.{}.{} not in '<2'".format(*sys.version_info[:3])
    )
    assert message in result.stderr
T
Tzu-ping Chung 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109


def test_new_resolver_prefers_installed_in_upgrade_if_latest(script):
    create_basic_wheel_for_package(script, "pkg", "1")
    local_pkg = create_test_package_with_setup(script, name="pkg", version="2")

    # Install the version that's not on the index.
    script.pip(
        "install",
        "--no-cache-dir",
        "--no-index",
        local_pkg,
    )

    # Now --upgrade should still pick the local version because it's "better".
    script.pip(
        "install",
        "--no-cache-dir",
        "--no-index",
1110 1111
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1112 1113 1114
        "--upgrade",
        "pkg",
    )
1115
    script.assert_installed(pkg="2")
1116 1117


1118
@pytest.mark.parametrize("N", [2, 10, 20])
1119 1120
def test_new_resolver_presents_messages_when_backtracking_a_lot(script, N):
    # Generate a set of wheels that will definitely cause backtracking.
1121
    for index in range(1, N + 1):
1122 1123
        A_version = f"{index}.0.0"
        B_version = f"{index}.0.0"
1124 1125 1126 1127 1128 1129 1130 1131 1132
        C_version = "{index_minus_one}.0.0".format(index_minus_one=index - 1)

        depends = ["B == " + B_version]
        if index != 1:
            depends.append("C == " + C_version)

        print("A", A_version, "B", B_version, "C", C_version)
        create_basic_wheel_for_package(script, "A", A_version, depends=depends)

1133
    for index in range(1, N + 1):
1134 1135
        B_version = f"{index}.0.0"
        C_version = f"{index}.0.0"
1136 1137 1138 1139 1140
        depends = ["C == " + C_version]

        print("B", B_version, "C", C_version)
        create_basic_wheel_for_package(script, "B", B_version, depends=depends)

1141
    for index in range(1, N + 1):
1142
        C_version = f"{index}.0.0"
1143 1144 1145 1146 1147 1148 1149 1150
        print("C", C_version)
        create_basic_wheel_for_package(script, "C", C_version)

    # Install A
    result = script.pip(
        "install",
        "--no-cache-dir",
        "--no-index",
1151 1152 1153
        "--find-links",
        script.scratch_path,
        "A",
1154 1155
    )

1156
    script.assert_installed(A="1.0.0", B="1.0.0", C="1.0.0")
1157 1158
    # These numbers are hard-coded in the code.
    if N >= 1:
1159
        assert "This could take a while." in result.stdout
1160 1161 1162
    if N >= 8:
        assert result.stdout.count("This could take a while.") >= 2
    if N >= 13:
1163
        assert "press Ctrl + C" in result.stdout
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192


@pytest.mark.parametrize(
    "metadata_version",
    [
        "0.1.0+local.1",  # Normalized form.
        "0.1.0+local_1",  # Non-normalized form containing an underscore.
        # Non-normalized form containing a dash. This is allowed, installation
        # works correctly, but assert_installed() fails because pkg_resources
        # cannot handle it correctly. Nobody is complaining about it right now,
        # we're probably dropping it for importlib.metadata soon(tm), so let's
        # ignore it for the time being.
        pytest.param("0.1.0+local-1", marks=pytest.mark.xfail),
    ],
    ids=["meta_dot", "meta_underscore", "meta_dash"],
)
@pytest.mark.parametrize(
    "filename_version",
    [
        ("0.1.0+local.1"),  # Tools are encouraged to use this.
        ("0.1.0+local_1"),  # But this is allowed (version not normalized).
    ],
    ids=["file_dot", "file_underscore"],
)
def test_new_resolver_check_wheel_version_normalized(
    script,
    metadata_version,
    filename_version,
):
1193
    filename = f"simple-{filename_version}-py2.py3-none-any.whl"
1194 1195 1196 1197 1198 1199

    wheel_builder = make_wheel(name="simple", version=metadata_version)
    wheel_builder.save_to(script.scratch_path / filename)

    script.pip(
        "install",
1200 1201 1202 1203 1204
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
1205
    )
1206
    script.assert_installed(simple="0.1.0+local.1")
T
Tzu-ping Chung 已提交
1207 1208


1209 1210
def test_new_resolver_does_reinstall_local_sdists(script):
    archive_path = create_basic_sdist_for_package(
1211 1212 1213 1214 1215
        script,
        "pkg",
        "1.0",
    )
    script.pip(
1216 1217 1218
        "install",
        "--no-cache-dir",
        "--no-index",
1219 1220
        archive_path,
    )
1221
    script.assert_installed(pkg="1.0")
1222 1223

    result = script.pip(
1224 1225 1226
        "install",
        "--no-cache-dir",
        "--no-index",
1227
        archive_path,
1228
        expect_stderr=True,
1229 1230
    )
    assert "Installing collected packages: pkg" in result.stdout, str(result)
1231
    script.assert_installed(pkg="1.0")
1232 1233 1234


def test_new_resolver_does_reinstall_local_paths(script):
1235
    pkg = create_test_package_with_setup(script, name="pkg", version="1.0")
1236
    script.pip(
1237 1238 1239
        "install",
        "--no-cache-dir",
        "--no-index",
1240 1241
        pkg,
    )
1242
    script.assert_installed(pkg="1.0")
1243 1244

    result = script.pip(
1245 1246 1247
        "install",
        "--no-cache-dir",
        "--no-index",
1248 1249 1250
        pkg,
    )
    assert "Installing collected packages: pkg" in result.stdout, str(result)
1251
    script.assert_installed(pkg="1.0")
1252 1253 1254


def test_new_resolver_does_not_reinstall_when_from_a_local_index(script):
1255
    create_basic_sdist_for_package(
1256 1257 1258 1259 1260 1261
        script,
        "simple",
        "0.1.0",
    )
    script.pip(
        "install",
1262 1263 1264 1265 1266
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
1267
    )
1268
    script.assert_installed(simple="0.1.0")
1269 1270 1271

    result = script.pip(
        "install",
1272 1273 1274 1275 1276
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
1277 1278 1279 1280
    )
    # Should not reinstall!
    assert "Installing collected packages: simple" not in result.stdout, str(result)
    assert "Requirement already satisfied: simple" in result.stdout, str(result)
1281
    script.assert_installed(simple="0.1.0")
T
Tzu-ping Chung 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291


def test_new_resolver_skip_inconsistent_metadata(script):
    create_basic_wheel_for_package(script, "A", "1")

    a_2 = create_basic_wheel_for_package(script, "A", "2")
    a_2.rename(a_2.parent.joinpath("a-3-py2.py3-none-any.whl"))

    result = script.pip(
        "install",
1292 1293 1294 1295
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1296 1297 1298 1299 1300
        "--verbose",
        "A",
        allow_stderr_warning=True,
    )

1301 1302 1303
    assert (
        " inconsistent version: filename has '3', but metadata has '2'"
    ) in result.stderr, str(result)
1304
    script.assert_installed(a="1")
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319


@pytest.mark.parametrize(
    "upgrade",
    [True, False],
    ids=["upgrade", "no-upgrade"],
)
def test_new_resolver_lazy_fetch_candidates(script, upgrade):
    create_basic_wheel_for_package(script, "myuberpkg", "1")
    create_basic_wheel_for_package(script, "myuberpkg", "2")
    create_basic_wheel_for_package(script, "myuberpkg", "3")

    # Install an old version first.
    script.pip(
        "install",
1320 1321 1322 1323
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
        "myuberpkg==1",
    )

    # Now install the same package again, maybe with the upgrade flag.
    if upgrade:
        pip_upgrade_args = ["--upgrade"]
    else:
        pip_upgrade_args = []
    result = script.pip(
        "install",
1334 1335 1336 1337
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
1338
        "myuberpkg",
1339
        *pip_upgrade_args,  # Trailing comma fails on Python 2.
1340 1341 1342 1343
    )

    # pip should install the version preferred by the strategy...
    if upgrade:
1344
        script.assert_installed(myuberpkg="3")
1345
    else:
1346
        script.assert_installed(myuberpkg="1")
1347 1348 1349 1350

    # But should reach there in the best route possible, without trying
    # candidates it does not need to.
    assert "myuberpkg-2" not in result.stdout, str(result)
T
Tzu-ping Chung 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359


def test_new_resolver_no_fetch_no_satisfying(script):
    create_basic_wheel_for_package(script, "myuberpkg", "1")

    # Install the package. This should emit a "Processing" message for
    # fetching the distribution from the --find-links page.
    result = script.pip(
        "install",
1360 1361 1362 1363
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1364 1365
        "myuberpkg",
    )
T
Tzu-ping Chung 已提交
1366
    assert "Processing " in result.stdout, str(result)
T
Tzu-ping Chung 已提交
1367 1368 1369 1370 1371

    # Try to upgrade the package. This should NOT emit the "Processing"
    # message because the currently installed version is latest.
    result = script.pip(
        "install",
1372 1373 1374 1375
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1376 1377 1378
        "--upgrade",
        "myuberpkg",
    )
T
Tzu-ping Chung 已提交
1379
    assert "Processing " not in result.stdout, str(result)
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401


def test_new_resolver_does_not_install_unneeded_packages_with_url_constraint(script):
    archive_path = create_basic_wheel_for_package(
        script,
        "installed",
        "0.1.0",
    )
    not_installed_path = create_basic_wheel_for_package(
        script,
        "not_installed",
        "0.1.0",
    )

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("not_installed @ " + path_to_url(not_installed_path))

    (script.scratch_path / "index").mkdir()
    archive_path.rename(script.scratch_path / "index" / archive_path.name)

    script.pip(
        "install",
1402 1403 1404 1405 1406 1407 1408
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path / "index",
        "-c",
        constraints_file,
        "installed",
1409 1410
    )

1411 1412
    script.assert_installed(installed="0.1.0")
    script.assert_not_installed("not_installed")
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425


def test_new_resolver_installs_packages_with_url_constraint(script):
    installed_path = create_basic_wheel_for_package(
        script,
        "installed",
        "0.1.0",
    )

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("installed @ " + path_to_url(installed_path))

    script.pip(
1426
        "install", "--no-cache-dir", "--no-index", "-c", constraints_file, "installed"
1427 1428
    )

1429
    script.assert_installed(installed="0.1.0")
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443


def test_new_resolver_reinstall_link_requirement_with_constraint(script):
    installed_path = create_basic_wheel_for_package(
        script,
        "installed",
        "0.1.0",
    )

    cr_file = script.scratch_path / "constraints.txt"
    cr_file.write_text("installed @ " + path_to_url(installed_path))

    script.pip(
        "install",
1444 1445 1446 1447
        "--no-cache-dir",
        "--no-index",
        "-r",
        cr_file,
1448 1449 1450 1451
    )

    script.pip(
        "install",
1452 1453 1454 1455 1456 1457
        "--no-cache-dir",
        "--no-index",
        "-c",
        cr_file,
        "-r",
        cr_file,
1458 1459 1460 1461
    )
    # TODO: strengthen assertion to "second invocation does no work"
    # I don't think this is true yet, but it should be in the future.

1462
    script.assert_installed(installed="0.1.0")
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484


def test_new_resolver_prefers_url_constraint(script):
    installed_path = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.1.0",
    )
    not_installed_path = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.2.0",
    )

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("test_pkg @ " + path_to_url(installed_path))

    (script.scratch_path / "index").mkdir()
    not_installed_path.rename(script.scratch_path / "index" / not_installed_path.name)

    script.pip(
        "install",
1485 1486 1487 1488 1489 1490 1491
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path / "index",
        "-c",
        constraints_file,
        "test_pkg",
1492 1493
    )

1494
    script.assert_installed(test_pkg="0.1.0")
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516


def test_new_resolver_prefers_url_constraint_on_update(script):
    installed_path = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.1.0",
    )
    not_installed_path = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.2.0",
    )

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("test_pkg @ " + path_to_url(installed_path))

    (script.scratch_path / "index").mkdir()
    not_installed_path.rename(script.scratch_path / "index" / not_installed_path.name)

    script.pip(
        "install",
1517 1518 1519 1520 1521
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path / "index",
        "test_pkg",
1522 1523
    )

1524
    script.assert_installed(test_pkg="0.2.0")
1525 1526 1527

    script.pip(
        "install",
1528 1529 1530 1531 1532 1533 1534
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path / "index",
        "-c",
        constraints_file,
        "test_pkg",
1535 1536
    )

1537
    script.assert_installed(test_pkg="0.1.0")
1538 1539 1540 1541


@pytest.mark.parametrize("version_option", ["--constraint", "--requirement"])
def test_new_resolver_fails_with_url_constraint_and_incompatible_version(
1542 1543
    script,
    version_option,
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
):
    not_installed_path = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.1.0",
    )
    not_installed_path = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.2.0",
    )

    url_constraint = script.scratch_path / "constraints.txt"
    url_constraint.write_text("test_pkg @ " + path_to_url(not_installed_path))

    version_req = script.scratch_path / "requirements.txt"
    version_req.write_text("test_pkg<0.2.0")

    result = script.pip(
        "install",
1564 1565 1566 1567 1568 1569 1570 1571
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "--constraint",
        url_constraint,
        version_option,
        version_req,
1572 1573 1574 1575 1576 1577 1578 1579 1580
        "test_pkg",
        expect_error=True,
    )

    assert "Cannot install test_pkg" in result.stderr, str(result)
    assert (
        "because these package versions have conflicting dependencies."
    ) in result.stderr, str(result)

1581
    script.assert_not_installed("test_pkg")
1582 1583 1584 1585

    # Assert that pip works properly in the absence of the constraints file.
    script.pip(
        "install",
1586 1587 1588 1589 1590 1591 1592
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        version_option,
        version_req,
        "test_pkg",
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
    )


def test_new_resolver_ignores_unneeded_conflicting_constraints(script):
    version_1 = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.1.0",
    )
    version_2 = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.2.0",
    )
    create_basic_wheel_for_package(
        script,
        "installed",
        "0.1.0",
    )

    constraints = [
        "test_pkg @ " + path_to_url(version_1),
        "test_pkg @ " + path_to_url(version_2),
    ]

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("\n".join(constraints))

    script.pip(
        "install",
1623 1624 1625 1626 1627 1628 1629
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
        "installed",
1630 1631
    )

1632 1633
    script.assert_not_installed("test_pkg")
    script.assert_installed(installed="0.1.0")
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657


def test_new_resolver_fails_on_needed_conflicting_constraints(script):
    version_1 = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.1.0",
    )
    version_2 = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.2.0",
    )

    constraints = [
        "test_pkg @ " + path_to_url(version_1),
        "test_pkg @ " + path_to_url(version_2),
    ]

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("\n".join(constraints))

    result = script.pip(
        "install",
1658 1659 1660 1661 1662 1663
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
1664 1665 1666 1667 1668 1669 1670 1671 1672
        "test_pkg",
        expect_error=True,
    )

    assert (
        "Cannot install test_pkg because these package versions have conflicting "
        "dependencies."
    ) in result.stderr, str(result)

1673
    script.assert_not_installed("test_pkg")
1674 1675 1676 1677

    # Assert that pip works properly in the absence of the constraints file.
    script.pip(
        "install",
1678 1679 1680 1681
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
        "test_pkg",
    )


def test_new_resolver_fails_on_conflicting_constraint_and_requirement(script):
    version_1 = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.1.0",
    )
    version_2 = create_basic_wheel_for_package(
        script,
        "test_pkg",
        "0.2.0",
    )

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("test_pkg @ " + path_to_url(version_1))

    result = script.pip(
        "install",
1703 1704 1705 1706 1707 1708
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
1709 1710 1711 1712 1713 1714 1715 1716 1717
        "test_pkg @ " + path_to_url(version_2),
        expect_error=True,
    )

    assert "Cannot install test-pkg 0.2.0" in result.stderr, str(result)
    assert (
        "because these package versions have conflicting dependencies."
    ) in result.stderr, str(result)

1718
    script.assert_not_installed("test_pkg")
1719 1720 1721 1722

    # Assert that pip works properly in the absence of the constraints file.
    script.pip(
        "install",
1723 1724 1725 1726
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
1727 1728 1729 1730 1731 1732 1733 1734
        "test_pkg @ " + path_to_url(version_2),
    )


@pytest.mark.parametrize("editable", [False, True])
def test_new_resolver_succeeds_on_matching_constraint_and_requirement(script, editable):
    if editable:
        source_dir = create_test_package_with_setup(
1735
            script, name="test_pkg", version="0.1.0"
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
        )
    else:
        source_dir = create_basic_wheel_for_package(
            script,
            "test_pkg",
            "0.1.0",
        )

    req_line = "test_pkg @ " + path_to_url(source_dir)

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text(req_line)

    if editable:
        last_args = ("-e", source_dir)
    else:
        last_args = (req_line,)

    script.pip(
        "install",
1756 1757 1758 1759
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_file,
1760 1761 1762
        *last_args,
    )

1763
    script.assert_installed(test_pkg="0.1.0")
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
    if editable:
        assert_editable(script, "test-pkg")


def test_new_resolver_applies_url_constraint_to_dep(script):
    version_1 = create_basic_wheel_for_package(
        script,
        "dep",
        "0.1.0",
    )
    version_2 = create_basic_wheel_for_package(
        script,
        "dep",
        "0.2.0",
    )

    base = create_basic_wheel_for_package(script, "base", "0.1.0", depends=["dep"])

    (script.scratch_path / "index").mkdir()
    base.rename(script.scratch_path / "index" / base.name)
    version_2.rename(script.scratch_path / "index" / version_2.name)

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("dep @ " + path_to_url(version_1))

    script.pip(
        "install",
1791 1792 1793 1794 1795 1796
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_file,
        "--find-links",
        script.scratch_path / "index",
1797 1798 1799
        "base",
    )

1800
    script.assert_installed(dep="0.1.0")
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819


def test_new_resolver_handles_compatible_wheel_tags_in_constraint_url(
    script, make_fake_wheel
):
    initial_path = make_fake_wheel("base", "0.1.0", "fakepy1-fakeabi-fakeplat")

    constrained = script.scratch_path / "constrained"
    constrained.mkdir()

    final_path = constrained / initial_path.name

    initial_path.rename(final_path)

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("base @ " + path_to_url(final_path))

    result = script.pip(
        "install",
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
        "--implementation",
        "fakepy",
        "--only-binary=:all:",
        "--python-version",
        "1",
        "--abi",
        "fakeabi",
        "--platform",
        "fakeplat",
        "--target",
        script.scratch_path / "target",
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_file,
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
        "base",
    )

    dist_info = Path("scratch", "target", "base-0.1.0.dist-info")
    result.did_create(dist_info)


def test_new_resolver_handles_incompatible_wheel_tags_in_constraint_url(
    script, make_fake_wheel
):
    initial_path = make_fake_wheel("base", "0.1.0", "fakepy1-fakeabi-fakeplat")

    constrained = script.scratch_path / "constrained"
    constrained.mkdir()

    final_path = constrained / initial_path.name

    initial_path.rename(final_path)

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("base @ " + path_to_url(final_path))

    result = script.pip(
        "install",
1859 1860 1861 1862
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_file,
1863 1864 1865 1866 1867 1868 1869 1870 1871
        "base",
        expect_error=True,
    )

    assert (
        "Cannot install base because these package versions have conflicting "
        "dependencies."
    ) in result.stderr, str(result)

1872
    script.assert_not_installed("base")
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894


def test_new_resolver_avoids_incompatible_wheel_tags_in_constraint_url(
    script, make_fake_wheel
):
    initial_path = make_fake_wheel("dep", "0.1.0", "fakepy1-fakeabi-fakeplat")

    constrained = script.scratch_path / "constrained"
    constrained.mkdir()

    final_path = constrained / initial_path.name

    initial_path.rename(final_path)

    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("dep @ " + path_to_url(final_path))

    index = script.scratch_path / "index"
    index.mkdir()

    index_dep = create_basic_wheel_for_package(script, "dep", "0.2.0")

1895 1896
    base = create_basic_wheel_for_package(script, "base", "0.1.0")
    base_2 = create_basic_wheel_for_package(script, "base", "0.2.0", depends=["dep"])
1897 1898 1899 1900 1901 1902 1903

    index_dep.rename(index / index_dep.name)
    base.rename(index / base.name)
    base_2.rename(index / base_2.name)

    script.pip(
        "install",
1904 1905 1906 1907 1908 1909
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_file,
        "--find-links",
        script.scratch_path / "index",
1910 1911 1912
        "base",
    )

1913 1914
    script.assert_installed(base="0.1.0")
    script.assert_not_installed("dep")
T
Tzu-ping Chung 已提交
1915 1916


1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
@pytest.mark.parametrize(
    "suffixes_equivalent, depend_suffix, request_suffix",
    [
        pytest.param(
            True,
            "#egg=foo",
            "",
            id="drop-depend-egg",
        ),
        pytest.param(
            True,
            "",
            "#egg=foo",
            id="drop-request-egg",
        ),
        pytest.param(
            True,
            "#subdirectory=bar&egg=foo",
            "#subdirectory=bar&egg=bar",
            id="drop-egg-only",
        ),
        pytest.param(
            True,
            "#subdirectory=bar&egg=foo",
            "#egg=foo&subdirectory=bar",
            id="fragment-ordering",
        ),
        pytest.param(
            True,
            "?a=1&b=2",
            "?b=2&a=1",
            id="query-opordering",
        ),
        pytest.param(
            False,
            "#sha512=1234567890abcdef",
            "#sha512=abcdef1234567890",
            id="different-keys",
        ),
        pytest.param(
            False,
            "#sha512=1234567890abcdef",
            "#md5=1234567890abcdef",
            id="different-values",
        ),
        pytest.param(
            False,
            "#subdirectory=bar&egg=foo",
            "#subdirectory=rex",
            id="drop-egg-still-different",
        ),
    ],
)
def test_new_resolver_direct_url_equivalent(
    tmp_path,
    script,
    suffixes_equivalent,
    depend_suffix,
    request_suffix,
):
    pkga = create_basic_wheel_for_package(script, name="pkga", version="1")
    pkgb = create_basic_wheel_for_package(
        script,
        name="pkgb",
        version="1",
        depends=[f"pkga@{path_to_url(pkga)}{depend_suffix}"],
    )

    # Make pkgb visible via --find-links, but not pkga.
    find_links = tmp_path.joinpath("find_links")
    find_links.mkdir()
    with open(pkgb, "rb") as f:
        find_links.joinpath(pkgb.name).write_bytes(f.read())

    # Install pkgb from --find-links, and pkga directly but from a different
    # URL suffix as specified in pkgb. This should work!
    script.pip(
        "install",
1995 1996 1997 1998 1999 2000
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        str(find_links),
        f"{path_to_url(pkga)}{request_suffix}",
        "pkgb",
2001 2002 2003 2004
        expect_error=(not suffixes_equivalent),
    )

    if suffixes_equivalent:
2005
        script.assert_installed(pkga="1", pkgb="1")
2006
    else:
2007
        script.assert_not_installed("pkga", "pkgb")
2008 2009


T
Tzu-ping Chung 已提交
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
def test_new_resolver_direct_url_with_extras(tmp_path, script):
    pkg1 = create_basic_wheel_for_package(script, name="pkg1", version="1")
    pkg2 = create_basic_wheel_for_package(
        script,
        name="pkg2",
        version="1",
        extras={"ext": ["pkg1"]},
    )
    pkg3 = create_basic_wheel_for_package(
        script,
        name="pkg3",
        version="1",
        depends=["pkg2[ext]"],
    )

    # Make pkg1 and pkg3 visible via --find-links, but not pkg2.
    find_links = tmp_path.joinpath("find_links")
    find_links.mkdir()
    with open(pkg1, "rb") as f:
        find_links.joinpath(pkg1.name).write_bytes(f.read())
    with open(pkg3, "rb") as f:
        find_links.joinpath(pkg3.name).write_bytes(f.read())

    # Install with pkg2 only available with direct URL. The extra-ed direct
    # URL pkg2 should be able to provide pkg2[ext] required by pkg3.
    result = script.pip(
        "install",
2037 2038 2039 2040 2041 2042
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        str(find_links),
        pkg2,
        "pkg3",
T
Tzu-ping Chung 已提交
2043 2044
    )

2045
    script.assert_installed(pkg1="1", pkg2="1", pkg3="1")
T
Tzu-ping Chung 已提交
2046 2047 2048
    assert not get_created_direct_url(result, "pkg1")
    assert get_created_direct_url(result, "pkg2")
    assert not get_created_direct_url(result, "pkg3")
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062


def test_new_resolver_modifies_installed_incompatible(script):
    create_basic_wheel_for_package(script, name="a", version="1")
    create_basic_wheel_for_package(script, name="a", version="2")
    create_basic_wheel_for_package(script, name="a", version="3")
    create_basic_wheel_for_package(script, name="b", version="1", depends=["a==1"])
    create_basic_wheel_for_package(script, name="b", version="2", depends=["a==2"])
    create_basic_wheel_for_package(script, name="c", version="1", depends=["a!=1"])
    create_basic_wheel_for_package(script, name="c", version="2", depends=["a!=1"])
    create_basic_wheel_for_package(script, name="d", version="1", depends=["b", "c"])

    script.pip(
        "install",
2063 2064 2065 2066
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
2067 2068 2069 2070 2071 2072 2073 2074 2075
        "b==1",
    )

    # d-1 depends on b and c. b-1 is already installed and therefore first
    # pinned, but later found to be incompatible since the "a==1" dependency
    # makes all c versions impossible to satisfy. The resolver should be able to
    # discard b-1 and backtrack, so b-2 is selected instead.
    script.pip(
        "install",
2076 2077 2078 2079
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
2080 2081
        "d==1",
    )
2082
    script.assert_installed(d="1", c="2", b="2", a="2")
2083 2084 2085 2086 2087 2088 2089 2090


def test_new_resolver_transitively_depends_on_unnamed_local(script):
    create_basic_wheel_for_package(script, name="certbot-docs", version="1")
    certbot = create_test_package_with_setup(
        script,
        name="certbot",
        version="99.99.0.dev0",
2091
        extras_require={"docs": ["certbot-docs"]},
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
    )
    certbot_apache = create_test_package_with_setup(
        script,
        name="certbot-apache",
        version="99.99.0.dev0",
        install_requires=["certbot>=99.99.0.dev0"],
    )

    script.pip(
        "install",
2102 2103 2104 2105 2106 2107
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        f"{certbot}[docs]",
        certbot_apache,
2108
    )
2109
    script.assert_installed(
2110 2111 2112 2113
        certbot="99.99.0.dev0",
        certbot_apache="99.99.0.dev0",
        certbot_docs="1",
    )
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155


def _to_uri(path):
    # Something like file:///path/to/package
    return pathlib.Path(path).as_uri()


def _to_localhost_uri(path):
    # Something like file://localhost/path/to/package
    return pathlib.Path(path).as_uri().replace("///", "//localhost/")


@pytest.mark.parametrize(
    "format_dep",
    [
        pytest.param(_to_uri, id="emptyhost"),
        pytest.param(_to_localhost_uri, id="localhost"),
    ],
)
@pytest.mark.parametrize(
    "format_input",
    [
        pytest.param(lambda path: path, id="path"),
        pytest.param(_to_uri, id="emptyhost"),
        pytest.param(_to_localhost_uri, id="localhost"),
    ],
)
def test_new_resolver_file_url_normalize(script, format_dep, format_input):
    lib_a = create_test_package_with_setup(
        script,
        name="lib_a",
        version="1",
    )
    lib_b = create_test_package_with_setup(
        script,
        name="lib_b",
        version="1",
        install_requires=[f"lib_a @ {format_dep(lib_a)}"],
    )

    script.pip(
        "install",
2156 2157 2158 2159
        "--no-cache-dir",
        "--no-index",
        format_input(lib_a),
        lib_b,
2160 2161
    )
    script.assert_installed(lib_a="1", lib_b="1")
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172


def test_new_resolver_dont_backtrack_on_extra_if_base_constrained(script):
    create_basic_wheel_for_package(script, "dep", "1.0")
    create_basic_wheel_for_package(script, "pkg", "1.0", extras={"ext": ["dep"]})
    create_basic_wheel_for_package(script, "pkg", "2.0", extras={"ext": ["dep"]})
    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text("pkg==1.0")

    result = script.pip(
        "install",
2173 2174 2175 2176 2177 2178
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "--constraint",
        constraints_file,
2179 2180 2181 2182
        "pkg[ext]",
    )
    assert "pkg-2.0" not in result.stdout, "Should not try 2.0 due to constraint"
    script.assert_installed(pkg="1.0", dep="1.0")