test_new_resolver.py 61.2 KB
Newer Older
T
Tzu-ping Chung 已提交
1
import os
2
import pathlib
3
import sys
4
import textwrap
5
from typing import TYPE_CHECKING, Callable, Dict, List, Tuple
T
Tzu-ping Chung 已提交
6

7 8
import pytest

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

20 21
if TYPE_CHECKING:
    from typing import Protocol
22

23 24

def assert_editable(script: PipTestEnvironment, *args: str) -> None:
T
Tzu-ping Chung 已提交
25 26 27
    # 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 已提交
28
    egg_links = {f"{arg}.egg-link" for arg in args}
29 30 31
    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 已提交
32 33


34
@pytest.fixture()
35 36
def make_fake_wheel(script: PipTestEnvironment) -> Callable[[str, str, str], Path]:
    def _make_fake_wheel(name: str, version: str, wheel_tag: str) -> Path:
37 38 39 40 41 42 43 44 45 46 47 48 49 50
        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


51
def test_new_resolver_can_install(script: PipTestEnvironment) -> None:
52
    create_basic_wheel_for_package(
53 54 55 56 57
        script,
        "simple",
        "0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
58
        "install",
59 60 61 62 63
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
64
    )
65
    script.assert_installed(simple="0.1.0")
66 67


68
def test_new_resolver_can_install_with_version(script: PipTestEnvironment) -> None:
69
    create_basic_wheel_for_package(
70 71 72 73 74
        script,
        "simple",
        "0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
75
        "install",
76 77 78 79 80
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple==0.1.0",
81
    )
82
    script.assert_installed(simple="0.1.0")
83 84


85
def test_new_resolver_picks_latest_version(script: PipTestEnvironment) -> None:
86
    create_basic_wheel_for_package(
87 88 89 90
        script,
        "simple",
        "0.1.0",
    )
91
    create_basic_wheel_for_package(
92 93 94 95 96
        script,
        "simple",
        "0.2.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
97
        "install",
98 99 100 101 102
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
103
    )
104
    script.assert_installed(simple="0.2.0")
105

106

107
def test_new_resolver_picks_installed_version(script: PipTestEnvironment) -> None:
108 109 110 111 112 113 114 115 116 117 118
    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 已提交
119
        "install",
120 121 122 123 124
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple==0.1.0",
125
    )
126
    script.assert_installed(simple="0.1.0")
127 128

    result = script.pip(
P
Pradyun Gedam 已提交
129
        "install",
130 131 132 133 134
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
135 136
    )
    assert "Collecting" not in result.stdout, "Should not fetch new version"
137
    script.assert_installed(simple="0.1.0")
138 139


140 141 142
def test_new_resolver_picks_installed_version_if_no_match_found(
    script: PipTestEnvironment,
) -> None:
143 144 145 146 147 148 149 150 151 152 153
    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 已提交
154
        "install",
155 156 157 158 159
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple==0.1.0",
160
    )
161
    script.assert_installed(simple="0.1.0")
162

163
    result = script.pip("install", "--no-cache-dir", "--no-index", "simple")
164
    assert "Collecting" not in result.stdout, "Should not fetch new version"
165
    script.assert_installed(simple="0.1.0")
166 167


168
def test_new_resolver_installs_dependencies(script: PipTestEnvironment) -> None:
169
    create_basic_wheel_for_package(
170 171 172 173 174
        script,
        "base",
        "0.1.0",
        depends=["dep"],
    )
175
    create_basic_wheel_for_package(
176 177 178 179 180
        script,
        "dep",
        "0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
181
        "install",
182 183 184 185 186
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "base",
187
    )
188
    script.assert_installed(base="0.1.0", dep="0.1.0")
189 190


191
def test_new_resolver_ignore_dependencies(script: PipTestEnvironment) -> None:
192 193 194 195 196 197 198 199 200 201 202 203
    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 已提交
204
        "install",
205 206 207 208 209 210
        "--no-cache-dir",
        "--no-index",
        "--no-deps",
        "--find-links",
        script.scratch_path,
        "base",
211
    )
212 213
    script.assert_installed(base="0.1.0")
    script.assert_not_installed("dep")
214 215


216 217 218 219 220 221 222
@pytest.mark.parametrize(
    "root_dep",
    [
        "base[add]",
        "base[add] >= 0.1.0",
    ],
)
223 224 225
def test_new_resolver_installs_extras(
    tmpdir: Path, script: PipTestEnvironment, root_dep: str
) -> None:
226 227 228
    req_file = tmpdir.joinpath("requirements.txt")
    req_file.write_text(root_dep)

229 230 231 232 233 234 235 236 237 238 239 240
    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 已提交
241
        "install",
242 243 244 245 246 247
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-r",
        req_file,
248
    )
249
    script.assert_installed(base="0.1.0", dep="0.1.0")
250 251


252
def test_new_resolver_installs_extras_warn_missing(script: PipTestEnvironment) -> None:
253 254 255 256 257 258 259 260 261 262 263
    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 已提交
264
    result = script.pip(
P
Pradyun Gedam 已提交
265
        "install",
266 267 268 269
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
P
Paul Moore 已提交
270 271
        "base[add,missing]",
        expect_stderr=True,
272
    )
273 274
    assert "does not provide the extra" in result.stderr, str(result)
    assert "missing" in result.stderr, str(result)
275
    script.assert_installed(base="0.1.0", dep="0.1.0")
276 277


278
def test_new_resolver_installed_message(script: PipTestEnvironment) -> None:
279 280
    create_basic_wheel_for_package(script, "A", "1.0")
    result = script.pip(
P
Pradyun Gedam 已提交
281
        "install",
282 283 284 285
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
286 287 288 289 290 291
        "A",
        expect_stderr=False,
    )
    assert "Successfully installed A-1.0" in result.stdout, str(result)


292
def test_new_resolver_no_dist_message(script: PipTestEnvironment) -> None:
293 294
    create_basic_wheel_for_package(script, "A", "1.0")
    result = script.pip(
P
Pradyun Gedam 已提交
295
        "install",
296 297 298 299
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
300 301 302 303 304 305 306 307 308 309
        "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

310 311 312
    assert (
        "Could not find a version that satisfies the requirement B" in result.stderr
    ), str(result)
313
    assert "No matching distribution found for B" in result.stderr, str(result)
314 315


316
def test_new_resolver_installs_editable(script: PipTestEnvironment) -> None:
T
Tzu-ping Chung 已提交
317 318 319 320 321 322 323 324 325 326 327 328
    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 已提交
329
        "install",
330 331 332 333
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
334
        "base",
335 336
        "--editable",
        source_dir,
T
Tzu-ping Chung 已提交
337
    )
338
    script.assert_installed(base="0.1.0", dep="0.1.0")
T
Tzu-ping Chung 已提交
339 340 341
    assert_editable(script, "dep")


342 343 344 345 346 347
@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 已提交
348
        # Something guaranteed to satisfy.
349 350 351 352 353
        (">=2", False, "0.2.0"),
        (">=2", True, "0.2.0"),
    ],
)
def test_new_resolver_requires_python(
354 355 356 357 358
    script: PipTestEnvironment,
    requires_python: str,
    ignore_requires_python: bool,
    dep_version: str,
) -> None:
359 360 361 362 363 364
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        depends=["dep"],
    )
365 366
    create_basic_wheel_for_package(
        script,
367 368
        "dep",
        "0.1.0",
369 370 371
    )
    create_basic_wheel_for_package(
        script,
372 373
        "dep",
        "0.2.0",
374 375
        requires_python=requires_python,
    )
376 377 378 379 380

    args = [
        "install",
        "--no-cache-dir",
        "--no-index",
381 382
        "--find-links",
        script.scratch_path,
383 384 385 386 387 388 389
    ]
    if ignore_requires_python:
        args.append("--ignore-requires-python")
    args.append("base")

    script.pip(*args)

390
    script.assert_installed(base="0.1.0", dep=dep_version)
391 392


393
def test_new_resolver_requires_python_error(script: PipTestEnvironment) -> None:
394 395 396 397 398 399 400
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
        requires_python="<2",
    )
    result = script.pip(
P
Pradyun Gedam 已提交
401
        "install",
402 403 404 405
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
406 407 408 409 410 411 412 413 414 415 416
        "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)


417
def test_new_resolver_installed(script: PipTestEnvironment) -> None:
418 419 420 421 422 423 424 425 426 427 428 429 430
    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 已提交
431
        "install",
432 433 434 435
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
436 437
        "base",
    )
438
    assert "Requirement already satisfied" not in result.stdout, str(result)
439 440

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


454
def test_new_resolver_ignore_installed(script: PipTestEnvironment) -> None:
455 456 457 458 459
    create_basic_wheel_for_package(
        script,
        "base",
        "0.1.0",
    )
460
    satisfied_output = "Requirement already satisfied"
461 462

    result = script.pip(
P
Pradyun Gedam 已提交
463
        "install",
464 465 466 467
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
468 469 470 471 472
        "base",
    )
    assert satisfied_output not in result.stdout, str(result)

    result = script.pip(
P
Pradyun Gedam 已提交
473
        "install",
474 475 476 477 478
        "--no-cache-dir",
        "--no-index",
        "--ignore-installed",
        "--find-links",
        script.scratch_path,
479 480 481
        "base",
    )
    assert satisfied_output not in result.stdout, str(result)
482
    result.did_update(
483
        script.site_packages / "base", message="base 0.1.0 not reinstalled"
484
    )
P
Paul Moore 已提交
485 486


487 488 489
def test_new_resolver_only_builds_sdists_when_needed(
    script: PipTestEnvironment,
) -> None:
P
Paul Moore 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    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 已提交
510
        "install",
511 512 513 514 515
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "base",
P
Paul Moore 已提交
516
    )
517
    script.assert_installed(base="0.1.0", dep="0.2.0")
P
Paul Moore 已提交
518 519 520

    # We merge criteria here, as we have two "dep" requirements
    script.pip(
P
Pradyun Gedam 已提交
521
        "install",
522 523 524 525 526 527
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "base",
        "dep",
P
Paul Moore 已提交
528
    )
529
    script.assert_installed(base="0.1.0", dep="0.2.0")
530 531


532
def test_new_resolver_install_different_version(script: PipTestEnvironment) -> None:
533 534 535 536
    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 已提交
537
        "install",
538 539 540 541
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
542 543 544 545 546
        "base==0.1.0",
    )

    # This should trigger an uninstallation of base.
    result = script.pip(
P
Pradyun Gedam 已提交
547
        "install",
548 549 550 551
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
552 553 554 555 556
        "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)
557
    result.did_update(script.site_packages / "base", message="base not upgraded")
558
    script.assert_installed(base="0.2.0")
559 560


561
def test_new_resolver_force_reinstall(script: PipTestEnvironment) -> None:
562 563 564
    create_basic_wheel_for_package(script, "base", "0.1.0")

    script.pip(
P
Pradyun Gedam 已提交
565
        "install",
566 567 568 569
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
570
        "base==0.1.0",
571 572
    )

573 574
    # This should trigger an uninstallation of base due to --force-reinstall,
    # even though the installed version matches.
575
    result = script.pip(
P
Pradyun Gedam 已提交
576
        "install",
577 578 579 580
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
581
        "--force-reinstall",
582
        "base==0.1.0",
583 584
    )

585 586
    assert "Uninstalling base-0.1.0" in result.stdout, str(result)
    assert "Successfully uninstalled base-0.1.0" in result.stdout, str(result)
587
    result.did_update(script.site_packages / "base", message="base not reinstalled")
588
    script.assert_installed(base="0.1.0")
T
Tzu-ping Chung 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605


@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(
606 607 608 609 610
    script: PipTestEnvironment,
    available_versions: List[str],
    pip_args: List[str],
    expected_version: str,
) -> None:
T
Tzu-ping Chung 已提交
611 612 613
    for version in available_versions:
        create_basic_wheel_for_package(script, "pkg", version)
    script.pip(
P
Pradyun Gedam 已提交
614
        "install",
615 616 617 618 619
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        *pip_args,
T
Tzu-ping Chung 已提交
620
    )
621
    script.assert_installed(pkg=expected_version)
P
Paul Moore 已提交
622 623


624 625 626 627 628 629 630
@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'"]),
631
    ],
632
)
633 634 635
def test_new_resolver_skips_marker(
    script: PipTestEnvironment, pkg_deps: List[str], root_deps: List[str]
) -> None:
636 637 638 639
    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 已提交
640
        "install",
641 642 643 644 645
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        *root_deps,
646
    )
647 648
    script.assert_installed(pkg="1.0")
    script.assert_not_installed("dep")
649 650


T
Tzu-ping Chung 已提交
651 652 653 654 655 656 657
@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"],
658
    ],
T
Tzu-ping Chung 已提交
659
)
660 661 662
def test_new_resolver_constraints(
    script: PipTestEnvironment, constraints: List[str]
) -> None:
P
Paul Moore 已提交
663 664 665 666
    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 已提交
667
    constraints_file.write_text("\n".join(constraints))
P
Paul Moore 已提交
668
    script.pip(
P
Pradyun Gedam 已提交
669
        "install",
670 671 672 673 674 675 676
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
        "pkg",
P
Paul Moore 已提交
677
    )
678 679
    script.assert_installed(pkg="1.0")
    script.assert_not_installed("constraint_only")
P
Paul Moore 已提交
680 681


682
def test_new_resolver_constraint_no_specifier(script: PipTestEnvironment) -> None:
683 684 685 686 687
    "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 已提交
688
        "install",
689 690 691 692 693 694 695
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
        "pkg",
696
    )
697
    script.assert_installed(pkg="1.0")
698 699 700 701 702 703 704 705 706 707


@pytest.mark.parametrize(
    "constraint, error",
    [
        (
            "dist.zip",
            "Unnamed requirements are not allowed as constraints",
        ),
        (
708 709
            "-e git+https://example.com/dist.git#egg=req",
            "Editable requirements are not allowed as constraints",
710 711 712 713 714 715 716
        ),
        (
            "pkg[extra]",
            "Constraints cannot have extras",
        ),
    ],
)
717 718 719
def test_new_resolver_constraint_reject_invalid(
    script: PipTestEnvironment, constraint: str, error: str
) -> None:
720 721 722 723
    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 已提交
724
        "install",
725 726 727 728 729 730
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
731 732 733 734 735 736 737
        "pkg",
        expect_error=True,
        expect_stderr=True,
    )
    assert error in result.stderr, str(result)


738
def test_new_resolver_constraint_on_dependency(script: PipTestEnvironment) -> None:
P
Paul Moore 已提交
739 740
    create_basic_wheel_for_package(script, "base", "1.0", depends=["dep"])
    create_basic_wheel_for_package(script, "dep", "1.0")
P
Paul Moore 已提交
741 742
    create_basic_wheel_for_package(script, "dep", "2.0")
    create_basic_wheel_for_package(script, "dep", "3.0")
P
Paul Moore 已提交
743
    constraints_file = script.scratch_path / "constraints.txt"
P
Paul Moore 已提交
744
    constraints_file.write_text("dep==2.0")
P
Paul Moore 已提交
745
    script.pip(
P
Pradyun Gedam 已提交
746
        "install",
747 748 749 750 751 752 753
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
        "base",
P
Paul Moore 已提交
754
    )
755 756
    script.assert_installed(base="1.0")
    script.assert_installed(dep="2.0")
P
Paul Moore 已提交
757 758


759 760 761
@pytest.mark.parametrize(
    "constraint_version, expect_error, message",
    [
762
        ("1.0", True, "Cannot install foo 2.0"),
763 764 765 766
        ("2.0", False, "Successfully installed foo-2.0"),
    ],
)
def test_new_resolver_constraint_on_path_empty(
767 768 769 770 771
    script: PipTestEnvironment,
    constraint_version: str,
    expect_error: bool,
    message: str,
) -> None:
772
    """A path requirement can be filtered by a constraint."""
P
Paul Moore 已提交
773 774 775
    setup_py = script.scratch_path / "setup.py"
    text = "from setuptools import setup\nsetup(name='foo', version='2.0')"
    setup_py.write_text(text)
776

P
Paul Moore 已提交
777
    constraints_txt = script.scratch_path / "constraints.txt"
778
    constraints_txt.write_text(f"foo=={constraint_version}")
779

P
Paul Moore 已提交
780
    result = script.pip(
P
Pradyun Gedam 已提交
781
        "install",
782 783 784 785
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_txt,
P
Paul Moore 已提交
786
        str(script.scratch_path),
787
        expect_error=expect_error,
P
Paul Moore 已提交
788 789
    )

790 791 792 793
    if expect_error:
        assert message in result.stderr, str(result)
    else:
        assert message in result.stdout, str(result)
794 795


796
def test_new_resolver_constraint_only_marker_match(script: PipTestEnvironment) -> None:
797 798 799 800
    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 已提交
801
    constraints_content = textwrap.dedent(
802 803 804 805 806 807
        """
        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 已提交
808
    constraints_txt.write_text(constraints_content)
809 810

    script.pip(
P
Pradyun Gedam 已提交
811
        "install",
812 813 814 815 816 817
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_txt,
        "--find-links",
        script.scratch_path,
818 819
        "pkg",
    )
820
    script.assert_installed(pkg="1.0")
821 822


823
def test_new_resolver_upgrade_needs_option(script: PipTestEnvironment) -> None:
824 825 826
    # Install pkg 1.0.0
    create_basic_wheel_for_package(script, "pkg", "1.0.0")
    script.pip(
P
Pradyun Gedam 已提交
827
        "install",
828 829 830 831
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
832 833 834 835 836 837 838 839
        "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 已提交
840
        "install",
841 842 843 844
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
845 846 847 848
        "pkg",
    )

    assert "Requirement already satisfied" in result.stdout, str(result)
849
    script.assert_installed(pkg="1.0.0")
850 851 852

    # This should upgrade
    result = script.pip(
P
Pradyun Gedam 已提交
853
        "install",
854 855 856 857
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
858
        "--upgrade",
859
        "PKG",  # Deliberately uppercase to check canonicalization
860 861 862 863
    )

    assert "Uninstalling pkg-1.0.0" in result.stdout, str(result)
    assert "Successfully uninstalled pkg-1.0.0" in result.stdout, str(result)
864
    result.did_update(script.site_packages / "pkg", message="pkg not upgraded")
865
    script.assert_installed(pkg="2.0.0")
866 867


868
def test_new_resolver_upgrade_strategy(script: PipTestEnvironment) -> None:
869 870 871
    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 已提交
872
        "install",
873 874 875 876
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
877 878 879
        "base",
    )

880 881
    script.assert_installed(base="1.0.0")
    script.assert_installed(dep="1.0.0")
882 883 884 885 886 887

    # 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 已提交
888
        "install",
889 890 891 892
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
893 894 895 896 897 898
        "--upgrade",
        "base",
    )

    # With upgrade strategy "only-if-needed" (the default), dep should not
    # be upgraded.
899 900
    script.assert_installed(base="2.0.0")
    script.assert_installed(dep="1.0.0")
901 902 903

    create_basic_wheel_for_package(script, "base", "3.0.0", depends=["dep"])
    script.pip(
P
Pradyun Gedam 已提交
904
        "install",
905 906 907 908 909 910
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "--upgrade",
        "--upgrade-strategy=eager",
911 912 913 914
        "base",
    )

    # With upgrade strategy "eager", dep should be upgraded.
915 916
    script.assert_installed(base="3.0.0")
    script.assert_installed(dep="2.0.0")
917 918


919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
if TYPE_CHECKING:

    class PackageBuilder(Protocol):
        def __call__(
            self,
            script: PipTestEnvironment,
            name: str,
            version: str,
            requires: List[str],
            extras: Dict[str, List[str]],
        ) -> str:
            ...


def _local_with_setup(
    script: PipTestEnvironment,
    name: str,
    version: str,
    requires: List[str],
    extras: Dict[str, List[str]],
) -> str:
    """Create the package as a local source directory to install from path."""
    return create_test_package_with_setup(
        script,
        name=name,
        version=version,
        install_requires=requires,
        extras_require=extras,
    )


def _direct_wheel(
    script: PipTestEnvironment,
    name: str,
    version: str,
    requires: List[str],
    extras: Dict[str, List[str]],
) -> str:
    """Create the package as a wheel to install from path directly."""
    return create_basic_wheel_for_package(
        script,
        name=name,
        version=version,
        depends=requires,
        extras=extras,
    )


def _wheel_from_index(
    script: PipTestEnvironment,
    name: str,
    version: str,
    requires: List[str],
    extras: Dict[str, List[str]],
) -> str:
    """Create the package as a wheel to install from index."""
    create_basic_wheel_for_package(
        script,
        name=name,
        version=version,
        depends=requires,
        extras=extras,
    )
    return name


985
class TestExtraMerge:
986 987 988 989 990 991 992
    """
    Test installing a package that depends the same package with different
    extras, one listed as required and the other as in extra.
    """

    @pytest.mark.parametrize(
        "pkg_builder",
T
Tzu-ping Chung 已提交
993
        [
P
Pradyun Gedam 已提交
994
            _local_with_setup,
T
Tzu-ping Chung 已提交
995 996 997
            _direct_wheel,
            _wheel_from_index,
        ],
998
    )
999 1000 1001
    def test_new_resolver_extra_merge_in_package(
        self, script: PipTestEnvironment, pkg_builder: "PackageBuilder"
    ) -> None:
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
        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 已提交
1018
            "install",
1019 1020 1021 1022
            "--no-cache-dir",
            "--no-index",
            "--find-links",
            script.scratch_path,
1023 1024
            requirement + "[dev]",
        )
1025
        script.assert_installed(pkg="1.0.0", dep="1.0.0", depdev="1.0.0")
1026 1027


1028
def test_new_resolver_build_directory_error_zazo_19(script: PipTestEnvironment) -> None:
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    """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(
1050 1051 1052 1053
        script,
        "pkg_a",
        "3.0.0",
        depends=["pkg-b<2"],
1054 1055 1056 1057 1058 1059 1060 1061
    )
    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 已提交
1062
        "install",
1063 1064 1065 1066 1067 1068
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "pkg-a",
        "pkg-b",
1069
    )
1070
    script.assert_installed(pkg_a="3.0.0", pkg_b="1.0.0")
T
Tzu-ping Chung 已提交
1071 1072


1073
def test_new_resolver_upgrade_same_version(script: PipTestEnvironment) -> None:
T
Tzu-ping Chung 已提交
1074 1075 1076 1077
    create_basic_wheel_for_package(script, "pkg", "2")
    create_basic_wheel_for_package(script, "pkg", "1")

    script.pip(
P
Pradyun Gedam 已提交
1078
        "install",
1079 1080 1081 1082
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1083 1084
        "pkg",
    )
1085
    script.assert_installed(pkg="2")
T
Tzu-ping Chung 已提交
1086 1087

    script.pip(
P
Pradyun Gedam 已提交
1088
        "install",
1089 1090 1091 1092
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1093 1094 1095
        "--upgrade",
        "pkg",
    )
1096
    script.assert_installed(pkg="2")
1097 1098


1099
def test_new_resolver_local_and_req(script: PipTestEnvironment) -> None:
1100 1101 1102 1103 1104 1105
    source_dir = create_test_package_with_setup(
        script,
        name="pkg",
        version="0.1.0",
    )
    script.pip(
P
Pradyun Gedam 已提交
1106
        "install",
1107 1108 1109 1110
        "--no-cache-dir",
        "--no-index",
        source_dir,
        "pkg!=0.1.0",
1111 1112
        expect_error=True,
    )
1113 1114


1115 1116 1117
def test_new_resolver_no_deps_checks_requires_python(
    script: PipTestEnvironment,
) -> None:
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
    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",
1136 1137
        "--find-links",
        script.scratch_path,
1138 1139 1140 1141 1142 1143 1144 1145 1146
        "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 已提交
1147 1148


1149 1150 1151
def test_new_resolver_prefers_installed_in_upgrade_if_latest(
    script: PipTestEnvironment,
) -> None:
T
Tzu-ping Chung 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
    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",
1168 1169
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1170 1171 1172
        "--upgrade",
        "pkg",
    )
1173
    script.assert_installed(pkg="2")
1174 1175


1176
@pytest.mark.parametrize("N", [2, 10, 20])
1177 1178 1179
def test_new_resolver_presents_messages_when_backtracking_a_lot(
    script: PipTestEnvironment, N: int
) -> None:
1180
    # Generate a set of wheels that will definitely cause backtracking.
1181
    for index in range(1, N + 1):
1182 1183
        A_version = f"{index}.0.0"
        B_version = f"{index}.0.0"
1184 1185 1186 1187 1188 1189 1190 1191 1192
        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)

1193
    for index in range(1, N + 1):
1194 1195
        B_version = f"{index}.0.0"
        C_version = f"{index}.0.0"
1196 1197 1198 1199 1200
        depends = ["C == " + C_version]

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

1201
    for index in range(1, N + 1):
1202
        C_version = f"{index}.0.0"
1203 1204 1205 1206 1207 1208 1209 1210
        print("C", C_version)
        create_basic_wheel_for_package(script, "C", C_version)

    # Install A
    result = script.pip(
        "install",
        "--no-cache-dir",
        "--no-index",
1211 1212 1213
        "--find-links",
        script.scratch_path,
        "A",
1214 1215
    )

1216
    script.assert_installed(A="1.0.0", B="1.0.0", C="1.0.0")
1217 1218
    # These numbers are hard-coded in the code.
    if N >= 1:
1219
        assert "This could take a while." in result.stdout
1220 1221 1222
    if N >= 8:
        assert result.stdout.count("This could take a while.") >= 2
    if N >= 13:
1223
        assert "press Ctrl + C" in result.stdout
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248


@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(
1249 1250 1251 1252
    script: PipTestEnvironment,
    metadata_version: str,
    filename_version: str,
) -> None:
1253
    filename = f"simple-{filename_version}-py2.py3-none-any.whl"
1254 1255 1256 1257 1258 1259

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

    script.pip(
        "install",
1260 1261 1262 1263 1264
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
1265
    )
1266
    script.assert_installed(simple="0.1.0+local.1")
T
Tzu-ping Chung 已提交
1267 1268


1269
def test_new_resolver_does_reinstall_local_sdists(script: PipTestEnvironment) -> None:
1270
    archive_path = create_basic_sdist_for_package(
1271 1272 1273 1274 1275
        script,
        "pkg",
        "1.0",
    )
    script.pip(
1276 1277 1278
        "install",
        "--no-cache-dir",
        "--no-index",
1279 1280
        archive_path,
    )
1281
    script.assert_installed(pkg="1.0")
1282 1283

    result = script.pip(
1284 1285 1286
        "install",
        "--no-cache-dir",
        "--no-index",
1287
        archive_path,
1288
        expect_stderr=True,
1289 1290
    )
    assert "Installing collected packages: pkg" in result.stdout, str(result)
1291
    script.assert_installed(pkg="1.0")
1292 1293


1294
def test_new_resolver_does_reinstall_local_paths(script: PipTestEnvironment) -> None:
1295
    pkg = create_test_package_with_setup(script, name="pkg", version="1.0")
1296
    script.pip(
1297 1298 1299
        "install",
        "--no-cache-dir",
        "--no-index",
1300 1301
        pkg,
    )
1302
    script.assert_installed(pkg="1.0")
1303 1304

    result = script.pip(
1305 1306 1307
        "install",
        "--no-cache-dir",
        "--no-index",
1308 1309 1310
        pkg,
    )
    assert "Installing collected packages: pkg" in result.stdout, str(result)
1311
    script.assert_installed(pkg="1.0")
1312 1313


1314 1315 1316
def test_new_resolver_does_not_reinstall_when_from_a_local_index(
    script: PipTestEnvironment,
) -> None:
1317
    create_basic_sdist_for_package(
1318 1319 1320 1321 1322 1323
        script,
        "simple",
        "0.1.0",
    )
    script.pip(
        "install",
1324 1325 1326 1327 1328
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
1329
    )
1330
    script.assert_installed(simple="0.1.0")
1331 1332 1333

    result = script.pip(
        "install",
1334 1335 1336 1337 1338
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "simple",
1339 1340 1341 1342
    )
    # Should not reinstall!
    assert "Installing collected packages: simple" not in result.stdout, str(result)
    assert "Requirement already satisfied: simple" in result.stdout, str(result)
1343
    script.assert_installed(simple="0.1.0")
T
Tzu-ping Chung 已提交
1344 1345


1346
def test_new_resolver_skip_inconsistent_metadata(script: PipTestEnvironment) -> None:
T
Tzu-ping Chung 已提交
1347 1348 1349 1350 1351 1352 1353
    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",
1354 1355 1356 1357
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1358 1359 1360 1361 1362
        "--verbose",
        "A",
        allow_stderr_warning=True,
    )

1363 1364
    assert (
        " inconsistent version: filename has '3', but metadata has '2'"
1365
    ) in result.stdout, str(result)
1366
    script.assert_installed(a="1")
1367 1368 1369 1370 1371 1372 1373


@pytest.mark.parametrize(
    "upgrade",
    [True, False],
    ids=["upgrade", "no-upgrade"],
)
1374 1375 1376
def test_new_resolver_lazy_fetch_candidates(
    script: PipTestEnvironment, upgrade: bool
) -> None:
1377 1378 1379 1380 1381 1382 1383
    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",
1384 1385 1386 1387
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
        "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",
1398 1399 1400 1401
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
1402
        "myuberpkg",
1403
        *pip_upgrade_args,  # Trailing comma fails on Python 2.
1404 1405 1406 1407
    )

    # pip should install the version preferred by the strategy...
    if upgrade:
1408
        script.assert_installed(myuberpkg="3")
1409
    else:
1410
        script.assert_installed(myuberpkg="1")
1411 1412 1413 1414

    # 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 已提交
1415 1416


1417
def test_new_resolver_no_fetch_no_satisfying(script: PipTestEnvironment) -> None:
T
Tzu-ping Chung 已提交
1418 1419 1420 1421 1422 1423
    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",
1424 1425 1426 1427
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1428 1429
        "myuberpkg",
    )
T
Tzu-ping Chung 已提交
1430
    assert "Processing " in result.stdout, str(result)
T
Tzu-ping Chung 已提交
1431 1432 1433 1434 1435

    # Try to upgrade the package. This should NOT emit the "Processing"
    # message because the currently installed version is latest.
    result = script.pip(
        "install",
1436 1437 1438 1439
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
T
Tzu-ping Chung 已提交
1440 1441 1442
        "--upgrade",
        "myuberpkg",
    )
T
Tzu-ping Chung 已提交
1443
    assert "Processing " not in result.stdout, str(result)
1444 1445


1446 1447 1448
def test_new_resolver_does_not_install_unneeded_packages_with_url_constraint(
    script: PipTestEnvironment,
) -> None:
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
    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",
1468 1469 1470 1471 1472 1473 1474
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path / "index",
        "-c",
        constraints_file,
        "installed",
1475 1476
    )

1477 1478
    script.assert_installed(installed="0.1.0")
    script.assert_not_installed("not_installed")
1479 1480


1481 1482 1483
def test_new_resolver_installs_packages_with_url_constraint(
    script: PipTestEnvironment,
) -> None:
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
    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(
1494
        "install", "--no-cache-dir", "--no-index", "-c", constraints_file, "installed"
1495 1496
    )

1497
    script.assert_installed(installed="0.1.0")
1498 1499


1500 1501 1502
def test_new_resolver_reinstall_link_requirement_with_constraint(
    script: PipTestEnvironment,
) -> None:
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
    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",
1514 1515 1516 1517
        "--no-cache-dir",
        "--no-index",
        "-r",
        cr_file,
1518 1519 1520 1521
    )

    script.pip(
        "install",
1522 1523 1524 1525 1526 1527
        "--no-cache-dir",
        "--no-index",
        "-c",
        cr_file,
        "-r",
        cr_file,
1528 1529 1530 1531
    )
    # TODO: strengthen assertion to "second invocation does no work"
    # I don't think this is true yet, but it should be in the future.

1532
    script.assert_installed(installed="0.1.0")
1533 1534


1535
def test_new_resolver_prefers_url_constraint(script: PipTestEnvironment) -> None:
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
    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",
1555 1556 1557 1558 1559 1560 1561
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path / "index",
        "-c",
        constraints_file,
        "test_pkg",
1562 1563
    )

1564
    script.assert_installed(test_pkg="0.1.0")
1565 1566


1567 1568 1569
def test_new_resolver_prefers_url_constraint_on_update(
    script: PipTestEnvironment,
) -> None:
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
    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",
1589 1590 1591 1592 1593
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path / "index",
        "test_pkg",
1594 1595
    )

1596
    script.assert_installed(test_pkg="0.2.0")
1597 1598 1599

    script.pip(
        "install",
1600 1601 1602 1603 1604 1605 1606
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path / "index",
        "-c",
        constraints_file,
        "test_pkg",
1607 1608
    )

1609
    script.assert_installed(test_pkg="0.1.0")
1610 1611 1612 1613


@pytest.mark.parametrize("version_option", ["--constraint", "--requirement"])
def test_new_resolver_fails_with_url_constraint_and_incompatible_version(
1614 1615 1616
    script: PipTestEnvironment,
    version_option: str,
) -> None:
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
    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",
1636 1637 1638 1639 1640 1641 1642 1643
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "--constraint",
        url_constraint,
        version_option,
        version_req,
1644 1645 1646 1647 1648 1649 1650 1651 1652
        "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)

1653
    script.assert_not_installed("test_pkg")
1654 1655 1656 1657

    # Assert that pip works properly in the absence of the constraints file.
    script.pip(
        "install",
1658 1659 1660 1661 1662 1663 1664
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        version_option,
        version_req,
        "test_pkg",
1665 1666 1667
    )


1668 1669 1670
def test_new_resolver_ignores_unneeded_conflicting_constraints(
    script: PipTestEnvironment,
) -> None:
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
    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",
1697 1698 1699 1700 1701 1702 1703
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
        "installed",
1704 1705
    )

1706 1707
    script.assert_not_installed("test_pkg")
    script.assert_installed(installed="0.1.0")
1708 1709


1710 1711 1712
def test_new_resolver_fails_on_needed_conflicting_constraints(
    script: PipTestEnvironment,
) -> None:
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
    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",
1734 1735 1736 1737 1738 1739
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
1740 1741 1742 1743 1744 1745 1746 1747 1748
        "test_pkg",
        expect_error=True,
    )

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

1749
    script.assert_not_installed("test_pkg")
1750 1751 1752 1753

    # Assert that pip works properly in the absence of the constraints file.
    script.pip(
        "install",
1754 1755 1756 1757
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
1758 1759 1760 1761
        "test_pkg",
    )


1762 1763 1764
def test_new_resolver_fails_on_conflicting_constraint_and_requirement(
    script: PipTestEnvironment,
) -> None:
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
    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",
1781 1782 1783 1784 1785 1786
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "-c",
        constraints_file,
1787 1788 1789 1790 1791 1792 1793 1794 1795
        "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)

1796
    script.assert_not_installed("test_pkg")
1797 1798 1799 1800

    # Assert that pip works properly in the absence of the constraints file.
    script.pip(
        "install",
1801 1802 1803 1804
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
1805 1806 1807 1808 1809
        "test_pkg @ " + path_to_url(version_2),
    )


@pytest.mark.parametrize("editable", [False, True])
1810 1811 1812
def test_new_resolver_succeeds_on_matching_constraint_and_requirement(
    script: PipTestEnvironment, editable: bool
) -> None:
1813 1814
    if editable:
        source_dir = create_test_package_with_setup(
1815
            script, name="test_pkg", version="0.1.0"
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
        )
    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)

1829
    last_args: Tuple[str, ...]
1830 1831 1832 1833 1834 1835 1836
    if editable:
        last_args = ("-e", source_dir)
    else:
        last_args = (req_line,)

    script.pip(
        "install",
1837 1838 1839 1840
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_file,
1841 1842 1843
        *last_args,
    )

1844
    script.assert_installed(test_pkg="0.1.0")
1845 1846 1847 1848
    if editable:
        assert_editable(script, "test-pkg")


1849
def test_new_resolver_applies_url_constraint_to_dep(script: PipTestEnvironment) -> None:
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
    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",
1872 1873 1874 1875 1876 1877
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_file,
        "--find-links",
        script.scratch_path / "index",
1878 1879 1880
        "base",
    )

1881
    script.assert_installed(dep="0.1.0")
1882 1883 1884


def test_new_resolver_handles_compatible_wheel_tags_in_constraint_url(
1885 1886
    script: PipTestEnvironment, make_fake_wheel: Callable[[str, str, str], Path]
) -> None:
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
    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",
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
        "--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,
1916 1917 1918 1919 1920 1921 1922 1923
        "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(
1924 1925
    script: PipTestEnvironment, make_fake_wheel: Callable[[str, str, str], Path]
) -> None:
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
    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",
1940 1941 1942 1943
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_file,
1944 1945 1946 1947 1948 1949 1950 1951 1952
        "base",
        expect_error=True,
    )

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

1953
    script.assert_not_installed("base")
1954 1955 1956


def test_new_resolver_avoids_incompatible_wheel_tags_in_constraint_url(
1957 1958
    script: PipTestEnvironment, make_fake_wheel: Callable[[str, str, str], Path]
) -> None:
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
    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")

1976 1977
    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"])
1978 1979 1980 1981 1982 1983 1984

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

    script.pip(
        "install",
1985 1986 1987 1988 1989 1990
        "--no-cache-dir",
        "--no-index",
        "-c",
        constraints_file,
        "--find-links",
        script.scratch_path / "index",
1991 1992 1993
        "base",
    )

1994 1995
    script.assert_installed(base="0.1.0")
    script.assert_not_installed("dep")
T
Tzu-ping Chung 已提交
1996 1997


1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 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 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
@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(
2052 2053 2054 2055 2056 2057
    tmp_path: pathlib.Path,
    script: PipTestEnvironment,
    suffixes_equivalent: bool,
    depend_suffix: str,
    request_suffix: str,
) -> None:
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
    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",
2076 2077 2078 2079 2080 2081
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        str(find_links),
        f"{path_to_url(pkga)}{request_suffix}",
        "pkgb",
2082 2083 2084 2085
        expect_error=(not suffixes_equivalent),
    )

    if suffixes_equivalent:
2086
        script.assert_installed(pkga="1", pkgb="1")
2087
    else:
2088
        script.assert_not_installed("pkga", "pkgb")
2089 2090


2091 2092 2093
def test_new_resolver_direct_url_with_extras(
    tmp_path: pathlib.Path, script: PipTestEnvironment
) -> None:
T
Tzu-ping Chung 已提交
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
    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",
2120 2121 2122 2123 2124 2125
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        str(find_links),
        pkg2,
        "pkg3",
T
Tzu-ping Chung 已提交
2126 2127
    )

2128
    script.assert_installed(pkg1="1", pkg2="1", pkg3="1")
T
Tzu-ping Chung 已提交
2129 2130 2131
    assert not get_created_direct_url(result, "pkg1")
    assert get_created_direct_url(result, "pkg2")
    assert not get_created_direct_url(result, "pkg3")
2132 2133


2134 2135 2136
def test_new_resolver_modifies_installed_incompatible(
    script: PipTestEnvironment,
) -> None:
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
    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",
2148 2149 2150 2151
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
2152 2153 2154 2155 2156 2157 2158 2159 2160
        "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",
2161 2162 2163 2164
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
2165 2166
        "d==1",
    )
2167
    script.assert_installed(d="1", c="2", b="2", a="2")
2168 2169


2170 2171 2172
def test_new_resolver_transitively_depends_on_unnamed_local(
    script: PipTestEnvironment,
) -> None:
2173 2174 2175 2176 2177
    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",
2178
        extras_require={"docs": ["certbot-docs"]},
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
    )
    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",
2189 2190 2191 2192 2193 2194
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        f"{certbot}[docs]",
        certbot_apache,
2195
    )
2196
    script.assert_installed(
2197 2198 2199 2200
        certbot="99.99.0.dev0",
        certbot_apache="99.99.0.dev0",
        certbot_docs="1",
    )
2201 2202


2203
def _to_uri(path: str) -> str:
2204 2205 2206 2207
    # Something like file:///path/to/package
    return pathlib.Path(path).as_uri()


2208
def _to_localhost_uri(path: str) -> str:
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
    # 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"),
    ],
)
2228 2229 2230 2231 2232
def test_new_resolver_file_url_normalize(
    script: PipTestEnvironment,
    format_dep: Callable[[str], str],
    format_input: Callable[[str], str],
) -> None:
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
    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",
2247 2248 2249 2250
        "--no-cache-dir",
        "--no-index",
        format_input(lib_a),
        lib_b,
2251 2252
    )
    script.assert_installed(lib_a="1", lib_b="1")
2253 2254


2255 2256 2257
def test_new_resolver_dont_backtrack_on_extra_if_base_constrained(
    script: PipTestEnvironment,
) -> None:
2258 2259 2260 2261 2262 2263 2264 2265
    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",
2266 2267 2268 2269 2270 2271
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "--constraint",
        constraints_file,
2272 2273 2274 2275
        "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")
2276 2277


2278 2279 2280
def test_new_resolver_respect_user_requested_if_extra_is_installed(
    script: PipTestEnvironment,
) -> None:
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
    create_basic_wheel_for_package(script, "pkg1", "1.0")
    create_basic_wheel_for_package(script, "pkg2", "1.0", extras={"ext": ["pkg1"]})
    create_basic_wheel_for_package(script, "pkg2", "2.0", extras={"ext": ["pkg1"]})
    create_basic_wheel_for_package(script, "pkg3", "1.0", depends=["pkg2[ext]"])

    # Install pkg3 with an older pkg2.
    script.pip(
        "install",
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "pkg3",
        "pkg2==1.0",
    )
    script.assert_installed(pkg3="1.0", pkg2="1.0", pkg1="1.0")

    # Now upgrade both pkg3 and pkg2. pkg2 should be upgraded although pkg2[ext]
    # is not requested by the user.
    script.pip(
        "install",
        "--no-cache-dir",
        "--no-index",
        "--find-links",
        script.scratch_path,
        "--upgrade",
        "pkg3",
        "pkg2",
    )
    script.assert_installed(pkg3="1.0", pkg2="2.0", pkg1="1.0")