summaryrefslogtreecommitdiff
path: root/tests/functional/test_uninstall.py
blob: b0e12f6af59640c44d721a7eae767146f49dbaef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
import logging
import os
import sys
import textwrap
from os.path import join, normpath
from pathlib import Path
from tempfile import mkdtemp
from typing import Any, Iterator
from unittest.mock import Mock

import pytest

from pip._internal.req.constructors import install_req_from_line
from pip._internal.utils.misc import rmtree
from tests.lib import (
    PipTestEnvironment,
    TestData,
    assert_all_changes,
    create_test_package_with_setup,
    need_svn,
)
from tests.lib.local_repos import local_checkout, local_repo


@pytest.mark.network
def test_basic_uninstall(script: PipTestEnvironment) -> None:
    """
    Test basic install and uninstall.

    """
    result = script.pip("install", "INITools==0.2")
    result.did_create(join(script.site_packages, "initools"))
    # the import forces the generation of __pycache__ if the version of python
    # supports it
    script.run("python", "-c", "import initools")
    result2 = script.pip("uninstall", "INITools", "-y")
    assert_all_changes(result, result2, [script.venv / "build", "cache"])


def test_basic_uninstall_distutils(script: PipTestEnvironment) -> None:
    """
    Test basic install and uninstall.

    """
    script.scratch_path.joinpath("distutils_install").mkdir()
    pkg_path = script.scratch_path / "distutils_install"
    pkg_path.joinpath("setup.py").write_text(
        textwrap.dedent(
            """
        from distutils.core import setup
        setup(
            name='distutils-install',
            version='0.1',
        )
    """
        )
    )
    result = script.run("python", os.fspath(pkg_path / "setup.py"), "install")
    result = script.pip("list", "--format=json")
    script.assert_installed(distutils_install="0.1")
    result = script.pip(
        "uninstall", "distutils_install", "-y", expect_stderr=True, expect_error=True
    )
    assert (
        "Cannot uninstall 'distutils-install'. It is a distutils installed "
        "project and thus we cannot accurately determine which files belong "
        "to it which would lead to only a partial uninstall."
    ) in result.stderr


@pytest.mark.network
def test_basic_uninstall_with_scripts(script: PipTestEnvironment) -> None:
    """
    Uninstall an easy_installed package with scripts.

    """
    # setuptools 52 removed easy_install.
    script.pip("install", "setuptools==51.3.3", use_module=True)

    result = script.easy_install("PyLogo", expect_stderr=True)
    easy_install_pth = script.site_packages / "easy-install.pth"
    pylogo = sys.platform == "win32" and "pylogo" or "PyLogo"
    assert pylogo in result.files_updated[os.fspath(easy_install_pth)].bytes
    result2 = script.pip("uninstall", "pylogo", "-y")
    assert_all_changes(
        result,
        result2,
        [script.venv / "build", "cache", easy_install_pth],
    )


@pytest.mark.parametrize("name", ["GTrolls.tar.gz", "https://guyto.com/archives/"])
def test_uninstall_invalid_parameter(
    script: PipTestEnvironment, caplog: pytest.LogCaptureFixture, name: str
) -> None:
    result = script.pip("uninstall", name, "-y", expect_error=True)
    expected_message = (
        f"Invalid requirement: '{name}' ignored -"
        f" the uninstall command expects named requirements."
    )
    assert expected_message in result.stderr


@pytest.mark.network
def test_uninstall_easy_install_after_import(script: PipTestEnvironment) -> None:
    """
    Uninstall an easy_installed package after it's been imported

    """
    # setuptools 52 removed easy_install.
    script.pip("install", "setuptools==51.3.3", use_module=True)

    result = script.easy_install("INITools==0.2", expect_stderr=True)
    # the import forces the generation of __pycache__ if the version of python
    # supports it
    script.run("python", "-c", "import initools")
    result2 = script.pip("uninstall", "INITools", "-y")
    assert_all_changes(
        result,
        result2,
        [
            script.venv / "build",
            "cache",
            script.site_packages / "easy-install.pth",
        ],
    )


@pytest.mark.network
def test_uninstall_trailing_newline(script: PipTestEnvironment) -> None:
    """
    Uninstall behaves appropriately if easy-install.pth
    lacks a trailing newline

    """
    # setuptools 52 removed easy_install.
    script.pip("install", "setuptools==51.3.3", use_module=True)

    script.easy_install("INITools==0.2", expect_stderr=True)
    script.easy_install("PyLogo", expect_stderr=True)
    easy_install_pth = script.site_packages_path / "easy-install.pth"

    # trim trailing newline from easy-install.pth
    with open(easy_install_pth) as f:
        pth_before = f.read()

    with open(easy_install_pth, "w") as f:
        f.write(pth_before.rstrip())

    # uninstall initools
    script.pip("uninstall", "INITools", "-y")
    with open(easy_install_pth) as f:
        pth_after = f.read()

    # verify that only initools is removed
    before_without_initools = [
        line for line in pth_before.splitlines() if "initools" not in line.lower()
    ]
    lines_after = pth_after.splitlines()

    assert lines_after == before_without_initools


@pytest.mark.network
def test_basic_uninstall_namespace_package(script: PipTestEnvironment) -> None:
    """
    Uninstall a distribution with a namespace package without clobbering
    the namespace and everything in it.

    """
    result = script.pip("install", "pd.requires==0.0.3")
    result.did_create(join(script.site_packages, "pd"))
    result2 = script.pip("uninstall", "pd.find", "-y")
    assert join(script.site_packages, "pd") not in result2.files_deleted, sorted(
        result2.files_deleted.keys()
    )
    assert join(script.site_packages, "pd", "find") in result2.files_deleted, sorted(
        result2.files_deleted.keys()
    )


def test_uninstall_overlapping_package(
    script: PipTestEnvironment, data: TestData
) -> None:
    """
    Uninstalling a distribution that adds modules to a pre-existing package
    should only remove those added modules, not the rest of the existing
    package.

    See: GitHub issue #355 (pip uninstall removes things it didn't install)
    """
    parent_pkg = data.packages.joinpath("parent-0.1.tar.gz")
    child_pkg = data.packages.joinpath("child-0.1.tar.gz")

    result1 = script.pip("install", parent_pkg)
    result1.did_create(join(script.site_packages, "parent"))
    result2 = script.pip("install", child_pkg)
    result2.did_create(join(script.site_packages, "child"))
    result2.did_create(
        normpath(join(script.site_packages, "parent/plugins/child_plugin.py"))
    )
    # The import forces the generation of __pycache__ if the version of python
    #  supports it
    script.run("python", "-c", "import parent.plugins.child_plugin, child")
    result3 = script.pip("uninstall", "-y", "child")
    assert join(script.site_packages, "child") in result3.files_deleted, sorted(
        result3.files_created.keys()
    )
    assert (
        normpath(join(script.site_packages, "parent/plugins/child_plugin.py"))
        in result3.files_deleted
    ), sorted(result3.files_deleted.keys())
    assert join(script.site_packages, "parent") not in result3.files_deleted, sorted(
        result3.files_deleted.keys()
    )
    # Additional check: uninstalling 'child' should return things to the
    # previous state, without unintended side effects.
    assert_all_changes(result2, result3, [])


@pytest.mark.parametrize(
    "console_scripts", ["test_ = distutils_install", "test_:test_ = distutils_install"]
)
def test_uninstall_entry_point_colon_in_name(
    script: PipTestEnvironment, console_scripts: str
) -> None:
    """
    Test uninstall package with two or more entry points in the same section,
    whose name contain a colon.
    """
    pkg_name = "ep_install"
    pkg_path = create_test_package_with_setup(
        script,
        name=pkg_name,
        version="0.1",
        entry_points={
            "console_scripts": [
                console_scripts,
            ],
            "pip_test.ep": [
                "ep:name1 = distutils_install",
                "ep:name2 = distutils_install",
            ],
        },
    )
    script_name = script.bin_path.joinpath(console_scripts.split("=")[0].strip())
    if sys.platform == "win32":
        script_name = script_name.with_suffix(".exe")
    script.pip("install", pkg_path)
    assert script_name.exists()
    script.assert_installed(ep_install="0.1")

    script.pip("uninstall", "ep_install", "-y")
    assert not script_name.exists()
    script.assert_not_installed("ep-install")


def test_uninstall_gui_scripts(script: PipTestEnvironment) -> None:
    """
    Make sure that uninstall removes gui scripts
    """
    pkg_name = "gui_pkg"
    pkg_path = create_test_package_with_setup(
        script,
        name=pkg_name,
        version="0.1",
        entry_points={
            "gui_scripts": [
                "test_ = distutils_install",
            ],
        },
    )
    script_name = script.bin_path.joinpath("test_")
    if sys.platform == "win32":
        script_name = script_name.with_suffix(".exe")
    script.pip("install", pkg_path)
    assert script_name.exists()
    script.pip("uninstall", pkg_name, "-y")
    assert not script_name.exists()


def test_uninstall_console_scripts(script: PipTestEnvironment) -> None:
    """
    Test uninstalling a package with more files (console_script entry points,
    extra directories).
    """
    pkg_path = create_test_package_with_setup(
        script,
        name="discover",
        version="0.1",
        entry_points={"console_scripts": ["discover = discover:main"]},
    )
    result = script.pip("install", pkg_path)
    result.did_create(script.bin / f"discover{script.exe}")
    result2 = script.pip("uninstall", "discover", "-y")
    assert_all_changes(
        result,
        result2,
        [
            os.path.join(script.venv, "build"),
            "cache",
            os.path.join("scratch", "discover", "discover.egg-info"),
        ],
    )


def test_uninstall_console_scripts_uppercase_name(script: PipTestEnvironment) -> None:
    """
    Test uninstalling console script with uppercase character.
    """
    pkg_path = create_test_package_with_setup(
        script,
        name="ep_install",
        version="0.1",
        entry_points={
            "console_scripts": [
                "Test = distutils_install",
            ],
        },
    )
    script_name = script.bin_path.joinpath("Test" + script.exe)

    script.pip("install", pkg_path)
    assert script_name.exists()

    script.pip("uninstall", "ep_install", "-y")
    assert not script_name.exists()


@pytest.mark.network
def test_uninstall_easy_installed_console_scripts(script: PipTestEnvironment) -> None:
    """
    Test uninstalling package with console_scripts that is easy_installed.
    """
    # setuptools 52 removed easy_install and prints a warning after 42 when
    # the command is used.
    script.pip("install", "setuptools==51.3.3", use_module=True)

    result = script.easy_install("discover", allow_stderr_warning=True)
    result.did_create(script.bin / f"discover{script.exe}")
    result2 = script.pip("uninstall", "discover", "-y")
    assert_all_changes(
        result,
        result2,
        [
            script.venv / "build",
            "cache",
            script.site_packages / "easy-install.pth",
        ],
    )


@pytest.mark.xfail
@pytest.mark.network
@need_svn
def test_uninstall_editable_from_svn(script: PipTestEnvironment, tmpdir: Path) -> None:
    """
    Test uninstalling an editable installation from svn.
    """
    result = script.pip(
        "install",
        "-e",
        "{checkout}#egg=initools".format(
            checkout=local_checkout("svn+http://svn.colorstudy.com/INITools", tmpdir)
        ),
    )
    result.assert_installed("INITools")
    result2 = script.pip("uninstall", "-y", "initools")
    assert script.venv / "src" / "initools" in result2.files_after
    assert_all_changes(
        result,
        result2,
        [
            script.venv / "src",
            script.venv / "build",
            script.site_packages / "easy-install.pth",
        ],
    )


@pytest.mark.network
def test_uninstall_editable_with_source_outside_venv(
    script: PipTestEnvironment, tmpdir: Path
) -> None:
    """
    Test uninstalling editable install from existing source outside the venv.
    """
    try:
        temp = mkdtemp()
        temp_pkg_dir = join(temp, "pip-test-package")
        _test_uninstall_editable_with_source_outside_venv(
            script,
            tmpdir,
            temp_pkg_dir,
        )
    finally:
        rmtree(temp)


def _test_uninstall_editable_with_source_outside_venv(
    script: PipTestEnvironment,
    tmpdir: Path,
    temp_pkg_dir: str,
) -> None:
    result = script.run(
        "git",
        "clone",
        local_repo("git+https://github.com/pypa/pip-test-package", tmpdir),
        temp_pkg_dir,
        expect_stderr=True,
    )
    result2 = script.pip("install", "-e", temp_pkg_dir)
    result2.did_create(join(script.site_packages, "pip-test-package.egg-link"))
    result3 = script.pip("uninstall", "-y", "pip-test-package")
    assert_all_changes(
        result,
        result3,
        [script.venv / "build", script.site_packages / "easy-install.pth"],
    )


@pytest.mark.xfail
@pytest.mark.network
@need_svn
def test_uninstall_from_reqs_file(script: PipTestEnvironment, tmpdir: Path) -> None:
    """
    Test uninstall from a requirements file.

    """
    local_svn_url = local_checkout(
        "svn+http://svn.colorstudy.com/INITools",
        tmpdir,
    )
    script.scratch_path.joinpath("test-req.txt").write_text(
        textwrap.dedent(
            """
            -e {url}#egg=initools
            # and something else to test out:
            PyLogo<0.4
        """
        ).format(url=local_svn_url)
    )
    result = script.pip("install", "-r", "test-req.txt")
    script.scratch_path.joinpath("test-req.txt").write_text(
        textwrap.dedent(
            """
            # -f, -i, and --extra-index-url should all be ignored by uninstall
            -f http://www.example.com
            -i http://www.example.com
            --extra-index-url http://www.example.com

            -e {url}#egg=initools
            # and something else to test out:
            PyLogo<0.4
        """
        ).format(url=local_svn_url)
    )
    result2 = script.pip("uninstall", "-r", "test-req.txt", "-y")
    assert_all_changes(
        result,
        result2,
        [
            script.venv / "build",
            script.venv / "src",
            script.scratch / "test-req.txt",
            script.site_packages / "easy-install.pth",
        ],
    )


def test_uninstallpathset_no_paths(caplog: pytest.LogCaptureFixture) -> None:
    """
    Test UninstallPathSet logs notification when there are no paths to
    uninstall
    """
    from pip._internal.metadata import get_default_environment
    from pip._internal.req.req_uninstall import UninstallPathSet

    caplog.set_level(logging.INFO)

    test_dist = get_default_environment().get_distribution("pip")
    assert test_dist is not None, "pip not installed"

    uninstall_set = UninstallPathSet(test_dist)
    uninstall_set.remove()  # with no files added to set

    assert "Can't uninstall 'pip'. No files were found to uninstall." in caplog.text


def test_uninstall_non_local_distutils(
    caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch, tmpdir: Path
) -> None:
    einfo = tmpdir.joinpath("thing-1.0.egg-info")
    with open(einfo, "wb"):
        pass

    get_dist = Mock()
    get_dist.return_value = Mock(
        key="thing",
        project_name="thing",
        egg_info=einfo,
        location=einfo,
    )
    monkeypatch.setattr("pip._vendor.pkg_resources.get_distribution", get_dist)

    req = install_req_from_line("thing")
    req.uninstall()

    assert os.path.exists(einfo)


def test_uninstall_wheel(script: PipTestEnvironment, data: TestData) -> None:
    """
    Test uninstalling a wheel
    """
    package = data.packages.joinpath("simple.dist-0.1-py2.py3-none-any.whl")
    result = script.pip("install", package, "--no-index")
    dist_info_folder = script.site_packages / "simple.dist-0.1.dist-info"
    result.did_create(dist_info_folder)
    result2 = script.pip("uninstall", "simple.dist", "-y")
    assert_all_changes(result, result2, [])


@pytest.mark.parametrize(
    "installer",
    [
        FileNotFoundError,
        IsADirectoryError,
        "",
        os.linesep,
        b"\xc0\xff\xee",
        "pip",
        "MegaCorp Cloud Install-O-Matic",
    ],
)
def test_uninstall_without_record_fails(
    script: PipTestEnvironment, data: TestData, installer: Any
) -> None:
    """
    Test uninstalling a package installed without RECORD
    """
    package = data.packages.joinpath("simple.dist-0.1-py2.py3-none-any.whl")
    result = script.pip("install", package, "--no-index")
    dist_info_folder = script.site_packages / "simple.dist-0.1.dist-info"
    result.did_create(dist_info_folder)

    # Remove RECORD
    record_path = dist_info_folder / "RECORD"
    (script.base_path / record_path).unlink()
    ignore_changes = [record_path]

    # Populate, remove or otherwise break INSTALLER
    installer_path = dist_info_folder / "INSTALLER"
    ignore_changes += [installer_path]
    installer_path = script.base_path / installer_path
    if installer in (FileNotFoundError, IsADirectoryError):
        installer_path.unlink()
        if installer is IsADirectoryError:
            installer_path.mkdir()
    else:
        if isinstance(installer, bytes):
            installer_path.write_bytes(installer)
        else:
            installer_path.write_text(installer + os.linesep)

    result2 = script.pip("uninstall", "simple.dist", "-y", expect_error=True)
    expected_error_message = (
        "ERROR: Cannot uninstall simple.dist 0.1, RECORD file not found."
    )
    if not isinstance(installer, str) or not installer.strip() or installer == "pip":
        expected_error_message += (
            " You might be able to recover from this via: "
            "'pip install --force-reinstall --no-deps "
            "simple.dist==0.1'."
        )
    elif installer:
        expected_error_message += " Hint: The package was installed by {}.".format(
            installer
        )
    assert result2.stderr.rstrip() == expected_error_message
    assert_all_changes(result.files_after, result2, ignore_changes)


@pytest.mark.skipif("sys.platform == 'win32'")
def test_uninstall_with_symlink(
    script: PipTestEnvironment, data: TestData, tmpdir: Path
) -> None:
    """
    Test uninstalling a wheel, with an additional symlink
    https://github.com/pypa/pip/issues/6892
    """
    package = data.packages.joinpath("simple.dist-0.1-py2.py3-none-any.whl")
    script.pip("install", package, "--no-index")
    symlink_target = tmpdir / "target"
    symlink_target.mkdir()
    symlink_source = script.site_packages / "symlink"
    (script.base_path / symlink_source).symlink_to(symlink_target)
    st_mode = symlink_target.stat().st_mode
    distinfo_path = script.site_packages_path / "simple.dist-0.1.dist-info"
    record_path = distinfo_path / "RECORD"
    with open(record_path, "a") as f:
        f.write("symlink,,\n")
    uninstall_result = script.pip("uninstall", "simple.dist", "-y")
    assert symlink_source in uninstall_result.files_deleted
    assert symlink_target.stat().st_mode == st_mode


def test_uninstall_setuptools_develop_install(
    script: PipTestEnvironment, data: TestData
) -> None:
    """Try uninstall after setup.py develop followed of setup.py install"""
    pkg_path = data.packages.joinpath("FSPkg")
    script.run("python", "setup.py", "develop", expect_stderr=True, cwd=pkg_path)
    script.run("python", "setup.py", "install", expect_stderr=True, cwd=pkg_path)
    script.assert_installed(FSPkg="0.1.dev0")
    # Uninstall both develop and install
    uninstall = script.pip("uninstall", "FSPkg", "-y")
    assert any(p.suffix == ".egg" for p in uninstall.files_deleted), str(uninstall)
    uninstall2 = script.pip("uninstall", "FSPkg", "-y")
    assert (
        join(script.site_packages, "FSPkg.egg-link") in uninstall2.files_deleted
    ), str(uninstall2)
    script.assert_not_installed("FSPkg")


def test_uninstall_editable_and_pip_install(
    script: PipTestEnvironment, data: TestData
) -> None:
    """Try uninstall after pip install -e after pip install"""
    # SETUPTOOLS_SYS_PATH_TECHNIQUE=raw removes the assumption that `-e`
    # installs are always higher priority than regular installs.
    # This becomes the default behavior in setuptools 25.
    script.environ["SETUPTOOLS_SYS_PATH_TECHNIQUE"] = "raw"

    pkg_path = data.packages.joinpath("FSPkg")
    script.pip("install", "-e", ".", expect_stderr=True, cwd=pkg_path)
    # ensure both are installed with --ignore-installed:
    script.pip("install", "--ignore-installed", ".", expect_stderr=True, cwd=pkg_path)
    script.assert_installed(FSPkg="0.1.dev0")
    # Uninstall both develop and install
    uninstall = script.pip("uninstall", "FSPkg", "-y")
    assert not any(p.suffix == ".egg-link" for p in uninstall.files_deleted)
    uninstall2 = script.pip("uninstall", "FSPkg", "-y")
    assert (
        join(script.site_packages, "FSPkg.egg-link") in uninstall2.files_deleted
    ), list(uninstall2.files_deleted.keys())
    script.assert_not_installed("FSPkg")


@pytest.fixture()
def move_easy_install_pth(script: PipTestEnvironment) -> Iterator[None]:
    """Move easy-install.pth out of the way for testing easy_install."""
    easy_install_pth = join(script.site_packages_path, "easy-install.pth")
    pip_test_pth = join(script.site_packages_path, "pip-test.pth")
    os.rename(easy_install_pth, pip_test_pth)
    yield
    os.rename(pip_test_pth, easy_install_pth)


@pytest.mark.usefixtures("move_easy_install_pth")
def test_uninstall_editable_and_pip_install_easy_install_remove(
    script: PipTestEnvironment, data: TestData
) -> None:
    """Try uninstall after pip install -e after pip install
    and removing easy-install.pth"""
    # SETUPTOOLS_SYS_PATH_TECHNIQUE=raw removes the assumption that `-e`
    # installs are always higher priority than regular installs.
    # This becomes the default behavior in setuptools 25.
    script.environ["SETUPTOOLS_SYS_PATH_TECHNIQUE"] = "raw"

    # Install FSPkg
    pkg_path = data.packages.joinpath("FSPkg")
    script.pip("install", "-e", ".", expect_stderr=True, cwd=pkg_path)

    # Rename easy-install.pth to pip-test-fspkg.pth
    easy_install_pth = join(script.site_packages_path, "easy-install.pth")
    pip_test_fspkg_pth = join(script.site_packages_path, "pip-test-fspkg.pth")
    os.rename(easy_install_pth, pip_test_fspkg_pth)

    # Confirm that FSPkg is installed
    script.assert_installed(FSPkg="0.1.dev0")

    # Remove pip-test-fspkg.pth
    os.remove(pip_test_fspkg_pth)

    # Uninstall will fail with given warning
    uninstall = script.pip("uninstall", "FSPkg", "-y", allow_stderr_warning=True)
    assert "Cannot remove entries from nonexistent file" in uninstall.stderr

    assert (
        join(script.site_packages, "FSPkg.egg-link") in uninstall.files_deleted
    ), list(uninstall.files_deleted.keys())

    # Confirm that FSPkg is uninstalled
    script.assert_not_installed("FSPkg")


def test_uninstall_ignores_missing_packages(
    script: PipTestEnvironment, data: TestData
) -> None:
    """Uninstall of a non existent package prints a warning and exits cleanly"""
    result = script.pip(
        "uninstall",
        "-y",
        "non-existent-pkg",
        expect_stderr=True,
    )

    assert "Skipping non-existent-pkg as it is not installed." in result.stderr
    assert result.returncode == 0, "Expected clean exit"


def test_uninstall_ignores_missing_packages_and_uninstalls_rest(
    script: PipTestEnvironment, data: TestData
) -> None:
    script.pip_install_local("simple")
    result = script.pip(
        "uninstall",
        "-y",
        "non-existent-pkg",
        "simple",
        expect_stderr=True,
    )

    assert "Skipping non-existent-pkg as it is not installed." in result.stderr
    assert "Successfully uninstalled simple" in result.stdout
    assert result.returncode == 0, "Expected clean exit"